Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.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
Google apps script 谷歌工作表脚本-复制整个工作表保护_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script 谷歌工作表脚本-复制整个工作表保护

Google apps script 谷歌工作表脚本-复制整个工作表保护,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我正在使用一种解决方案来复制图纸,同时保持保护。我已经添加了带有名称表的GoogleSheetsAPI-V4 然而,它抛出了错误- GoogleJsonResponseException: API call to sheets.spreadsheets.batchUpdate failed with error: Invalid requests[0].addProtectedRange: The range you're trying to exclude must be within &qu

我正在使用一种解决方案来复制图纸,同时保持保护。我已经添加了带有名称表的GoogleSheetsAPI-V4

然而,它抛出了错误-

GoogleJsonResponseException: API call to sheets.spreadsheets.batchUpdate failed with error: Invalid requests[0].addProtectedRange: The range you're trying to exclude must be within "A".
经过一些尝试和错误,我发现,因为我的保护是整个表除了一些范围,它给出了错误。当我只保护少数范围时,代码工作正常

现在,该解决方案将使用整张保护进行复制。然而,它是单页的。我需要把这两者结合起来,但我正在努力

copyTo()和Sheets.Spreadsheets.get(id)返回的sheet对象不同,第二个对象没有protect()方法。所以我这里没有具体的错误,但需要将两个解决方案结合起来的解决方案。我需要复制多张图纸,同时在没有某些范围的情况下保持整张图纸的保护。

问题: 链接代码适用于多个保护范围,而非板材保护(例外情况除外)。如您所见,包含的代码
addProtectedRange
与您现有的保护冲突,因为代码假定您具有
范围
,但您具有彼此不兼容的
工作表
保护

请改用此代码:

代码: 参考:

谢谢你@NaziA。我偶然发现了相同的代码,并试图为批量工作表实现它。但是,我得到的protect()不是一个函数。有什么想法吗?你打算复制多少张@SurajS?你能展示你的代码来尝试上面的答案吗?这样我就可以调试你的问题了。我已经修改了上面的答案,并添加了一个循环来创建大量重复的工作表。请检查@SurajS12份副本。每个月一次,这需要做多次。我已经更新了这个问题。谢谢你,纳齐亚,如果你能找到我上面的代码中的错误,我也会很高兴,因为它会帮助我更好地理解脚本。天知道人们为什么投反对票。我在其他地方没有发现类似的问题。答案中提到的是单张纸。
function duplicateSheetWithProtections() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 

  // Template sheet
  var sheet = ss.getSheetByName("Template");
  var p = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];

  // Output sheet, and protect the sheet by default
  var sheet2 = sheet.copyTo(ss).setName("Template Copy"); 
  var p2 = sheet2.protect();
  
  // Copy protection properties
  p2.setDescription(p.getDescription());
  p2.setWarningOnly(p.isWarningOnly());  

  if (!p.isWarningOnly()) {
    // Copy editors
    p2.removeEditors(p2.getEditors());
    p2.addEditors(p.getEditors());
    // Copy domain settings if using Apps domain
    // p2.setDomainEdit(p.canDomainEdit()); 
  }

  // Get all unprotected ranges on template
  var ranges = p.getUnprotectedRanges();
  var newRanges = [];
  for (var i = 0; i < ranges.length; i++) {
    newRanges.push(sheet2.getRange(ranges[i].getA1Notation()));
  } 

  // Unprotect them in output
  p2.setUnprotectedRanges(newRanges);
}  
function main() {
  var sheetNames = ["A", "B", "C"];
  sheetNames.forEach(function (sheetName){
    duplicateSheetWithProtections(sheetName);
  });
}