如何通过GAS自动接受Google Sheet所有权转移请求?
自动接受Google Sheet所有权转移方案
核心思路
所有权转移请求本质是Drive权限变更的待处理请求,无需解析邮件内的按钮链接,直接通过Drive API查询并处理待接受的所有权请求更可靠。
步骤与代码实现
启用Drive API服务
在Google Apps Script编辑器中,点击左侧「服务」->「添加服务」,找到「Drive API」并添加(版本选v3)。编写处理脚本
以下脚本会查询当前用户所有待接受的所有权转移请求,筛选出Google Sheet类型的文件并自动接受:function acceptPendingOwnershipRequests() { // 查询条件:当前用户是被请求者,权限类型为所有者,状态待处理 const query = 'permissionType="owner" and status="pending" and emailAddress="' + Session.getActiveUser().getEmail() + '"'; const response = Drive.Permissions.list({ q: query, fields: 'permissions(id, fileId, emailAddress, role)', supportsAllDrives: true // 涉及共享驱动器时启用此选项 }); if (!response.permissions || response.permissions.length === 0) { console.log('无待处理的所有权转移请求'); return; } response.permissions.forEach(perm => { try { // 获取文件详情,确认是Google Sheet const file = Drive.Files.get(perm.fileId, {fields: 'mimeType'}); if (file.mimeType !== 'application/vnd.google-apps.spreadsheet') { console.log('跳过非Sheet文件:' + perm.fileId); return; } // 接受所有权转移 Drive.Permissions.update( {role: 'owner', status: 'accepted'}, perm.fileId, perm.id, {transferOwnership: true} ); console.log('已接受文件' + perm.fileId + '的所有权'); } catch (e) { console.error('处理请求失败:' + e.message); } }); }设置定时触发
在脚本编辑器中,点击左侧「触发器」->「添加触发器」,配置:- 选择函数:
acceptPendingOwnershipRequests - 事件源:「时间驱动」
- 触发类型:根据需求选择「分钟计时器」「小时计时器」等(比如每小时运行一次)
- 选择函数:
注意事项
- 确保脚本运行账号为收到所有权转移请求的账号
- 涉及共享驱动器时,需确保账号具备接受所有权的权限
- 首次运行脚本会要求授权,需授予Drive相关操作权限
内容的提问来源于stack exchange,提问作者Mozrin
相关产品推荐
相关产品推荐

