You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 03:04:53