Web应用报错“Script function not found: events”,表格权限保护失效求助
问题描述
点击绑定了main()函数的按钮后,执行无报错,但Google Sheets的权限保护未按预期移除/重新添加,同时Web应用提示**"Script function not found: events"**错误,疑似该错误导致流程卡住。
原参考代码:
// This is the main function. Please set this function to the run button on Spreadsheet. function main() { //DriveApp.getFiles(); // This is a dummy method for detecting a scope by the script editor. const activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); const sheetName = activeSheet.getName(); if (sheetName === 'Itens_para_Costurar') { var token = ScriptApp.getOAuthToken() var url = ScriptApp.getService().getUrl(); var options = { 'method': 'post', 'headers': { 'Authorization': 'Bearer ' + token }, muteHttpExceptions: true }; UrlFetchApp.fetch(url + "&key=removeprotectListaCortadas", options); // Remove protected range listaPecasCortadas();//Atualiza a lista de peças cortadas SpreadsheetApp.flush(); // This is required to be here. UrlFetchApp.fetch(url + "&key=addprotectListaCortadas", options); // Add protected range } } function doGet(e) { if (e.parameter.key == "removeprotectListaCortadas") { var listaPecasCortadasSht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Itens_para_Costurar'); // Please set here. // Remove protected range. var protections = listaPecasCortadasSht.getProtections(SpreadsheetApp.ProtectionType.RANGE); for (var i = 0; i < protections.length; i++) { console.log('Protection Name: ' + protections[i].getDescription()) protections[i].remove(); } } else { // Add protected range. var ownersEmail = Session.getActiveUser().getEmail(); var protection = listaPecasCortadasSht.getRange('A8:J100').protect(); var editors = protection.getEditors(); for (var i = 0; i < editors.length; i++) { var email = editors[i].getEmail(); if (email != ownersEmail) protection.removeEditor(email); } } return ContentService.createTextOutput("ok"); }
解决方案
1. 修复doGet函数逻辑漏洞
原代码中添加保护的分支未定义工作表变量,且未判断key是否匹配,导致执行报错:
function doGet(e) { // 提前定义工作表变量,确保所有分支可访问 var listaPecasCortadasSht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Itens_para_Costurar'); if (e.parameter.key == "removeprotectListaCortadas") { // 移除所有范围保护 var protections = listaPecasCortadasSht.getProtections(SpreadsheetApp.ProtectionType.RANGE); for (var i = 0; i < protections.length; i++) { console.log('Protection Name: ' + protections[i].getDescription()) protections[i].remove(); } } else if (e.parameter.key == "addprotectListaCortadas") { // 仅匹配对应key时添加保护 var ownersEmail = Session.getActiveUser().getEmail(); var protection = listaPecasCortadasSht.getRange('A8:J100').protect(); var editors = protection.getEditors(); for (var i = 0; i < editors.length; i++) { var email = editors[i].getEmail(); if (email != ownersEmail) protection.removeEditor(email); } } return ContentService.createTextOutput("ok"); }
2. 调整main函数的请求方式与参数
原代码用POST请求调用doGet(仅处理GET请求),且参数拼接格式错误:
function main() { //DriveApp.getFiles(); // 用于脚本编辑器检测权限范围 const activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); const sheetName = activeSheet.getName(); if (sheetName === 'Itens_para_Costurar') { var token = ScriptApp.getOAuthToken(); var url = ScriptApp.getService().getUrl(); var options = { 'headers': { 'Authorization': 'Bearer ' + token }, muteHttpExceptions: true }; // 使用GET请求,正确拼接参数(?开头,&连接) UrlFetchApp.fetch(`${url}?key=removeprotectListaCortadas`, options); listaPecasCortadas();// 更新已裁剪零件列表 SpreadsheetApp.flush(); // 强制刷新电子表格更改 UrlFetchApp.fetch(`${url}?key=addprotectListaCortadas`, options); } }
3. 解决"Script function not found: events"错误
- 重新部署Web应用,确保执行函数选择
doGet - 进入脚本编辑器的「触发器」页面,删除所有名为
events的残留触发器 - 检查脚本代码,移除所有对
events函数的无效引用
4. 权限验证
- 手动运行一次
main函数,完成权限授权流程 - 部署Web应用时选择**"以我自己的身份执行"**,确保拥有修改保护范围的权限
内容的提问来源于stack exchange,提问作者onit
相关产品推荐
相关产品推荐

