如何撤销IMPORTRANGE中表格对目标表格的数据拉取权限?
解决IMPORTRANGE权限超限问题:撤销指定表格的访问权限
问题背景
使用IMPORTRANGE让200+表格(如Spreadsheet A、B、C)拉取两个数据库表格(如Spreadsheet 1)的数据,因连接数过多触发权限上限,需撤销指定表格(如Spreadsheet A)对数据库表格的访问权限。旧脚本报错TypeError: targetFile.getPermissions is not a function,修改后的脚本可运行但无效果,本人为所有表格唯一所有者。
旧脚本(报错版本)
function removePermission() { const ss = SpreadsheetApp.getActiveSpreadsheet(); const ss_c = ss.getSheetByName('Config'); var currentSheetId = ss.getId(); var targetId = ""; if (ss_c.getRange("B6").getValue() == 1) { targetId = ss_c.getRange("D6").getValue(); } else if (ss_c.getRange("B6").getValue() == 2) { targetId = ss_c.getRange("D7").getValue(); } var currentFile = DriveApp.getFileById(currentSheetId); var targetFile = DriveApp.getFileById(targetId); var targetPermissions = targetFile.getPermissions(); for (var i = 0; i < targetPermissions.length; i++) { var permission = targetPermissions[i]; if (permission.getType() == "user" && permission.hasAccess()) { currentFile.removeEditor(permission.getEmail()); // or the next one // targetFile.removeEditor(permission.getEmail()); } } }
修改后脚本(无效果版本)
function test() { const ss = SpreadsheetApp.getActiveSpreadsheet(); const ss_c = ss.getSheetByName('Config'); var currentSheetId = ss.getId(); var targetId = ""; if (ss_c.getRange("B6").getValue() == 1) { targetId = ss_c.getRange("D6").getValue(); } else if (ss_c.getRange("B6").getValue() == 2) { targetId = ss_c.getRange("D7").getValue(); } var currentFile = DriveApp.getFileById(currentSheetId); var targetFile = DriveApp.getFileById(targetId); var targetEditors = targetFile.getEditors(); var currentEditors = currentFile.getEditors(); var activeUserEmail = Session.getActiveUser().getEmail(); for (var i = 0; i < targetEditors.length; i++) { var editor = targetEditors[i]; var editorEmail = editor.getEmail(); // Check if the editor exists in the current file before removing if (editorEmail !== activeUserEmail && currentEditors.some(e => e.getEmail() === editorEmail)) { targetFile.removeEditor(editorEmail); } } }
问题分析
- 旧脚本报错原因:
DriveApp.File对象没有getPermissions()方法,该方法属于Drive API的权限资源,直接调用会触发类型错误。 - 修改后脚本无效原因:
IMPORTRANGE的授权依赖表格专属的服务账号(类型为serviceAccount),而非普通用户编辑器权限。脚本仅操作了用户编辑器列表,未触及IMPORTRANGE的核心授权权限,因此无效果。
解决方案
要撤销指定表格对数据库表格的访问权限,需找到并删除目标表格中对应当前表格的服务账号权限,具体步骤如下:
1. 启用Drive API
在脚本编辑器中:
- 点击菜单栏「服务」→「添加服务」
- 找到「Drive API」,选择版本v3并添加
2. 运行以下脚本
function removeImportRangePermission() { const ss = SpreadsheetApp.getActiveSpreadsheet(); const configSheet = ss.getSheetByName('Config'); // 获取当前表格ID与目标表格ID const currentSheetId = ss.getId(); let targetId = ''; if (configSheet.getRange('B6').getValue() === 1) { targetId = configSheet.getRange('D6').getValue(); } else if (configSheet.getRange('B6').getValue() === 2) { targetId = configSheet.getRange('D7').getValue(); } if (!targetId) { Logger.log('未找到目标表格ID'); return; } // 获取目标表格的所有权限记录 const permissions = Drive.Permissions.list(targetId, { fields: 'permissions(id, type, emailAddress)' }); if (!permissions.permissions) { Logger.log('目标表格无权限记录'); return; } // 筛选并删除当前表格对应的服务账号权限 const currentServiceAccount = `${currentSheetId}@docs.googleusercontent.com`; permissions.permissions.forEach(permission => { if (permission.type === 'serviceAccount' && permission.emailAddress === currentServiceAccount) { Drive.Permissions.remove(targetId, permission.id); Logger.log(`已成功撤销当前表格对目标表格的IMPORTRANGE访问权限`); } }); }
3. 授权与运行
第一次运行脚本时,需按照提示完成权限授权(需允许脚本操作Drive权限),运行后可在日志中查看操作结果。
批量处理提示
若需批量撤销多个表格的权限,可将表格ID列表存入Config表,通过循环遍历列表执行上述删除逻辑。
内容的提问来源于stack exchange,提问作者Bruno Carvalho
相关产品推荐
相关产品推荐

