Google Apps Script报错TypeError: protection.remove is not a function求助
解决Google Sheets脚本中保护移除的TypeError错误
你的错误根源是调用了不存在的方法:Google Apps Script的工作表保护对象(Protection)没有remove()方法,正确的移除方法是delete()。
修改后的代码示例
方式1:创建保护时保存引用(推荐,更精准)
let s = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("My Sheet"); // 创建保护时直接保存对象引用 const protection = s.protect(); // 执行你的工作表操作 // do stuff to the sheet in the future // 移除保护 if (protection && protection.canEdit()) { protection.delete(); // 替换remove()为delete() }
方式2:后续通过getProtections获取保护对象
let s = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("My Sheet"); s.protect(); // 执行你的工作表操作 // do stuff to the sheet in the future const protection = s.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0]; if (protection?.canEdit()) { protection.delete(); // 替换remove()为delete() }
额外说明
canEdit()方法用于校验当前用户是否拥有编辑该保护规则的权限,避免因权限不足导致的执行错误。
内容的提问来源于stack exchange,提问作者Griffin
相关产品推荐
相关产品推荐

