I have a script to duplicate the source file. Inside the source file, there are 3 protected sheets (not protected range) where I already set the permissions. When I used the script, it duplicate the source file perfectly. However, the permissions for the 3 protected sheets in the duplicated files changed. Instead of the original permissions for protected sheet in the source file, the permissions in duplicated file changed to Only you can edit this range. May I know how should I modified my script so that the permission for the protected sheet will be duplicated as well? Below is my script:
function copyfilefromsource() {
var ui = SpreadsheetApp.getUi();
var sheet_merge = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("List");
var last_row = sheet_merge.getLastRow();
var newsheet_firstname = null;
var newsheet_surname = null;
var newsheet_email = null;
var source_folder = null;
var file_owner = null;
var dest_folder = null;
var sheets_created = 0;
var new_file = null;
var source_file = null;
var employee_id = null;
var google_domain = null;
var range = sheet_merge.getRange(1, 1, last_row, 4);
var temp = null;
source_file = DriveApp.getFileById(range.getCell(1, 2).getValue());
dest_folder = DriveApp.getFolderById(range.getCell(2, 2).getValue());
file_owner = range.getCell(3, 2).getValue();
google_domain = range.getCell(4, 2).getValue();
if (last_row <= 6) {
SpreadsheetApp.getUi().alert("No rows to process!");
return
}
for (var i = 7; i <= last_row; i++) {
if (range.getCell(i, 4).getValue() == '') {
employee_id = range.getCell(i, 1).getValue();
newsheet_email = employee_id + google_domain;
newsheet_firstname = range.getCell(i, 2).getValue();
newsheet_surname = range.getCell(i, 3).getValue();
new_file = source_file.makeCopy(employee_id, dest_folder);
new_file.setOwner(file_owner);
new_file.addViewer(newsheet_email);
new_file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.EDIT);
SpreadsheetApp.getActiveSheet().getRange(i,1).setFormula('=HYPERLINK("' + new_file.getUrl() +'/","'+employee_id+'")');
SpreadsheetApp.getActiveSheet().getRange(i,4).setValue(new_file.getId());
sheets_created++;
}
}
ui.alert(sheets_created + " files were created!")
}
function testFolder(folderName){
var exist = true;
try{var testFolder = DocsList.getFolder(folderName)}
catch(err){exist=false}
return exist;
}
Any advise will be greatly appreciated!