Google apps script 谷歌工作表脚本-复制整个工作表保护
我正在使用一种解决方案来复制图纸,同时保持保护。我已经添加了带有名称表的GoogleSheetsAPI-V4 然而,它抛出了错误-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
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
与您现有的保护冲突,因为代码假定您具有范围
,但您具有彼此不兼容的工作表
保护
请改用此代码:
代码:
参考:
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);
});
}