我有一个脚本,我用来复制源表。对于源表,我保护了一些范围,只允许学生A和学生B编辑这些受保护的范围,源表中的其他编辑器不允许在这些范围内做任何事情。
然而,当我检查脚本创建的复制表时,学生A和学生B不再被允许编辑保护范围,尽管所有复制表中的保护范围保持不变。只有我,源/复制表的所有者才能编辑受保护的范围。
我可以知道我应该如何修改我的脚本,使脚本能够让任何有权编辑源表中的受保护范围的人也能够编辑复制表中的受保护范围吗?任何帮助将非常感激!谢谢你。
这是我的脚本:
function copyfilefromsource() {
var ui = SpreadsheetApp.getUi();
var sheet_merge = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("List");
var last_row = sheet_merge.getLastRow();
var newsheet_email = null;
var file_owner = null;
var dest_folder = null;
var new_file = null;
var source_file = null;
var student_id = null;
var google_domain = null;
var range = sheet_merge.getRange(1, 1, last_row, 4);
source_file = DriveApp.getFileById(range.getCell(1, 2).getValue()); //gets the source file to copy
dest_folder = DriveApp.getFolderById(range.getCell(2, 2).getValue()); //gets the ID of the folder to place the copied files into
file_owner = range.getCell(3, 2).getValue(); //person to own the files
google_domain = range.getCell(4, 2).getValue(); //extension of the email address
for (var i = 7; i <= last_row; i++) {
if (range.getCell(i, 4).getValue() == '') {
student_id = range.getCell(i, 1).getValue();
newsheet_email = student_id + google_domain;
newsheet_firstname = range.getCell(i, 2).getValue();
newsheet_surname = range.getCell(i, 3).getValue();
new_file = source_file.makeCopy(student_id, dest_folder)
new_file.setOwner(file_owner);
new_file.addViewer(newsheet_email);
new_file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.EDIT);
var id_ss = new_file.getId();
SpreadsheetApp.openById(id_ss).getSheets()[0].getRange(2, 2).setValue(student_id);
SpreadsheetApp.getActiveSheet().getRange(i,1).setFormula('=HYPERLINK("' + new_file.getUrl() +'/","'+student_id+'")');
SpreadsheetApp.getActiveSheet().getRange(i,4).setValue(new_file.getId());
}
}
}
请看下面的例子。
function duplicateSheetWithProtections() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
sheet = ss.getSheetByName('Template');
sheet2 = sheet.copyTo(ss).setName('My Copy');
var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
var p = protections[i];
var rangeNotation = p.getRange().getA1Notation();
var p2 = sheet2.getRange(rangeNotation).protect();
p2.setDescription(p.getDescription());
p2.setWarningOnly(p.isWarningOnly());
if (!p.isWarningOnly()) {
p2.removeEditors(p2.getEditors());
p2.addEditors(p.getEditors());
// p2.setDomainEdit(p.canDomainEdit()); // only if using an Apps domain
}
}
}
https://webapps.stackexchange.com/questions/86984/in-google-sheets-how-do-i-duplicate-a-sheet-along-with-its-permission