我正在尝试在 Google 表格中创建一个预算文件,我的同事可以复制一份,然后将其用于他们的特定项目。有了这个副本,我想以所有者身份和唯一可以更改它们的人的身份保留我的原始受保护工作表(如果他们只是复制文件,他们将成为文件所有者,然后可以更改我被阻止的单元格)。
我找到了一个应该执行此操作的代码(见下文)并将其定制到我的项目中,但它并没有像我希望的那样工作。它在 Apps 脚本中无误地完成,然后创建一个好的复制文件,但是当在文件中创建副本时,设置规则不会。
如何在有人按下“复制文件”时让脚本运行?有人知道我哪里出错了吗?或者要更改/添加什么?
function Main() {
var template = DriveApp.getFileById('1oy-O6sjcFQbavJIC_xLByo7fPlu5mKhUW_Gh8y7_hi4');
var destination = DriveApp.getFolderById('1qrwSIV3Zijpjk0WVZb7GrZniDySjyzh5');
var curDate = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy-MM-dd");
var copyName = '[NAME FESTIVAL]' + template.getName() + curDate;
copyTemplate(template, copyName, destination);
}
//Copy google spreadsheet with all protected ranges permissions
function copyTemplate(template, copyName, destination, allowDuplicates) {
var newSheet, templateSheet, fileIterator;
//set defaults
copyName = copyName || template.getName();
destination = destination || DriveApp.getRootFolder();
allowDuplicates = allowDuplicates === false ? false : true;
templateSheet = SpreadsheetApp.open(template);
newSheet = SpreadsheetApp.open(template.makeCopy(copyName, destination));
//copy protections from the template to the new spreadsheet
copyProtections(templateSheet, newSheet);
}
//Copies protections from the origin to the target
function copyProtections(origin, target) {
var sheets = origin.getSheets();
for(var i = 0; i < sheets.length; i++) {
var newSheet = target.getSheetByName(sheets[i].getName());
var sheetProtection = sheets[i].getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
var newSheetProtection = newSheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
var rangeProtections = sheets[i].getProtections(SpreadsheetApp.ProtectionType.RANGE);
var newRangeProtections = newSheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
//Recreate sheet protection from scratch
if(sheetProtection) {
var unprotected = sheetProtection.getUnprotectedRanges();
var newUnprotected = [];
for (var j = 0; j < unprotected.length; j++) {
newUnprotected.push(newSheet.getRange(unprotected[j].getA1Notation()));
}
cloneProperties(sheetProtection, newSheetProtection);
newSheetProtection.setUnprotectedRanges(newUnprotected);
}
//Remove range protections in the new sheet
for(var j = 0; j < newRangeProtections.length; j++) {
newRangeProtections[j].remove();
}
//Recereate range protections from scratch
for (var j = 0; j < rangeProtections.length; j++) {
var protection = rangeProtections[j];
var newProtection;
if(protection.getRangeName()) {
//if protection is set to the named range
newProtection = target.getRangeByName(protection.getRangeName()).protect();
newProtection.setRangeName(protection.getRangeName());
} else {
newProtection = newSheet.getRange(protection.getRange().getA1Notation()).protect();
}
cloneProperties(protection, newProtection);
}//end of ranges
}//end of sheets
}
//Copies protection object basic properties
function cloneProperties(origin, target) {
target.setDescription(origin.getDescription());
target.setWarningOnly(origin.isWarningOnly());
if (!origin.isWarningOnly()) {
var editors = origin.getEditors();
var oldEditors = target.getEditors();
for(var i = 0; i < oldEditors.length; i++) {
target.removeEditor(oldEditors[i]);
}
for(var i = 0; i < editors.length; i++) {
target.addEditor(editors[i]);
}
if (origin.canDomainEdit()) {
target.setDomainEdit(true);
}
}
}