Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在多个工作表的第一个保护中添加或删除编辑器_Javascript_Google Apps Script_Google Sheets - Fatal编程技术网

Javascript 在多个工作表的第一个保护中添加或删除编辑器

Javascript 在多个工作表的第一个保护中添加或删除编辑器,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,你好!我有几个工作表,每个工作表有一个保护,我想在这些工作表之间循环,并在它们的保护中添加或删除编辑器 最初,我有以下代码: function AddOrRemove() { var spreadsheet = SpreadsheetApp.getActive(); var allProtections = spreadsheet.getActiveSheet().getProtections(SpreadsheetApp.ProtectionType.SHEET); var pro

你好!我有几个工作表,每个工作表有一个保护,我想在这些工作表之间循环,并在它们的保护中添加或删除编辑器

最初,我有以下代码:

function AddOrRemove() {
  var spreadsheet = SpreadsheetApp.getActive();
  var allProtections = spreadsheet.getActiveSheet().getProtections(SpreadsheetApp.ProtectionType.SHEET);
  var protection = allProtections[0];
  protection.removeEditors(['email1@gmail.com']);
  protection.addEditors(['email2@gmail.com']);
};
function AddOrRemove() {
  var sheets = ["Sheet1","Sheet2","Sheet3","Sheet4","Sheet5","Sheet6","Sheet7","Sheet8","Sheet9","Sheet10"];
  for (var i = 0 ; i = 15 ; i++){
    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheets[i]);
    var allProtections = spreadsheet.getProtections(SpreadsheetApp.ProtectionType.SHEET);
    var protection = allProtections[0];
    protection.removeEditors(['email1@gmail.com']);
    protection.addEditors(['email2@gmail.com']);
  }
};
这将删除
email1@gmail.com
while添加
email2@gmail.com
活动工作表上的
,这意味着我必须手动转到每个工作表并从那里运行它。但是,我想自动化图纸的循环,因此产生了以下代码:

function AddOrRemove() {
  var spreadsheet = SpreadsheetApp.getActive();
  var allProtections = spreadsheet.getActiveSheet().getProtections(SpreadsheetApp.ProtectionType.SHEET);
  var protection = allProtections[0];
  protection.removeEditors(['email1@gmail.com']);
  protection.addEditors(['email2@gmail.com']);
};
function AddOrRemove() {
  var sheets = ["Sheet1","Sheet2","Sheet3","Sheet4","Sheet5","Sheet6","Sheet7","Sheet8","Sheet9","Sheet10"];
  for (var i = 0 ; i = 15 ; i++){
    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheets[i]);
    var allProtections = spreadsheet.getProtections(SpreadsheetApp.ProtectionType.SHEET);
    var protection = allProtections[0];
    protection.removeEditors(['email1@gmail.com']);
    protection.addEditors(['email2@gmail.com']);
  }
};
其中
sheets
是我希望运行代码的所有工作表名称。但是,在尝试运行时,我遇到以下错误:

TypeError: Cannot call method "getProtections" of null. (line 5, file "AddOrRemoveEditors")
我尝试了
Logger.log(sheets[I])就在for循环中的
var电子表格
之前,它记录为“null”。然而,当我放置
Logger.log(sheets[0])在for循环之前,它正确地返回了K。我不太确定这里缺少什么。我觉得代码背后的逻辑应该是可行的,但也许我遗漏了一些东西。请求对此提供建议。谢谢

试试这个:

function AddOrRemove() {
  var sheets = ["Sheet1","Sheet2","Sheet3","Sheet4","Sheet5","Sheet6","Sheet7","Sheet8","Sheet9","Sheet10"];
  for (var i=0;i<sheets.length;i++){//modified
    var spreadsheet = SpreadsheetApp.getActive().getSheetByName(sheets[i]);//modified
    var allProtections = spreadsheet.getProtections(SpreadsheetApp.ProtectionType.SHEET);
    var protection = allProtections[0];
    protection.removeEditors(['email1@gmail.com']);
    protection.addEditors(['email2@gmail.com']);
  }
};
函数AddOrRemove(){
var表=[“表1”、“表2”、“表3”、“表4”、“表5”、“表6”、“表7”、“表8”、“表9”、“表10”];

for(var i=0;iThanks for response!我尝试了该代码,但出现了以下错误:
TypeError:在对象SpreadsheetApp中找不到函数getSheetByName。(第4行,文件“AddOrRemoveEditors”)
。可能SpreadsheetApp没有
getSheetByName
作为函数。我保留了
var spreadsheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheets[I]);
并将
修改为(var i=0;iYeh.我不应该删除它。您的列表中只有10张,最后一张是
sheets[sheets.length-1]