I have searched the far reaches of the internet for a couple days now, but cannot seem to find a solution to my issue. I have limited knowledge of programming, but if I can get this to work, it is going to do wonderful things.
EXPLANATION: I need to make a copy of a template spreadsheet, using a script inside spreadsheet A and copy over all the permissions of the template into the copy (specifically the name and protected ranges). I am using SheetSpider.
As of right now, I can create the duplicate of the template, but SheetSpider seems to drop the permissions that the template has, and rewrites them with users that you define during the setup process. Instead, I would just like to copy the permissions of the template file. It seems to be tricky because a regular copy of the spreadsheet still does not bring over the permissions for the name and protected range settings like it would if you go to file > copy spreadsheet and check the box that says "and share with the same users" which DOES retain protected range settings.
Again, I have a very limited knowledge of programming, but I think I have pinpointed the lines that seem to drop and reset the permissions. I have tried playing with the below for days now to get it to leave the permissions alone, but to no avail.
Any help or guidance would be greatly appreciated!
Thanks!
function checkFixFileACLs(file, approvedViewers, approvedEditors) {
var viewers = file.getViewers().join(",");
viewers = viewers.split(",");
var editors = file.getEditors().join(",");
editors = editors.split(",");
viewers = arr_diff(editors, viewers);
var owner = file.getOwner().toString();
var fileKey = file.getId();
var driveFile = DriveApp.getFileById(fileKey);
var currViewers = [];
for (var k=0; k<viewers.length; k++) {
if ((viewers[k]!='')&&(approvedViewers.indexOf(viewers[k].toLowerCase())==-1)&&(approvedEditors.indexOf(viewers[k].toLowerCase())==-1)&&(viewers[k]!=owner)) {
try {
call(function() {driveFile.removeViewer(viewers[k].toLowerCase());});
} catch(err) {
Logger.log(err.message);
}
} else {
currViewers.push(viewers[k].toLowerCase());
}
}
for (var k=0; k<approvedViewers.length; k++) {
if ((approvedViewers[k]!='')&&(approvedViewers[k])) {
if (currViewers.indexOf(approvedViewers[k])==-1) {
try {
call(function() {driveFile.addViewer(approvedViewers[k].toLowerCase());});
} catch(err) {
Logger.log(err.message);
}
}
}
}
var currEditors = [];
for (var k=0; k<editors.length; k++) {
if ((editors[k]!='')&&(approvedEditors.indexOf(editors[k].toLowerCase())==-1)&&(editors[k]!=owner)) {
try {
call(function() {driveFile.removeEditor(editors[k].toLowerCase());});
} catch(err) {
Logger.log(err.message);
}
} else {
currEditors.push(editors[k].toLowerCase().replace(/\s+/g, ''));
}
}
for (var k=0; k<approvedEditors.length; k++) {
if ((approvedEditors[k]!='')&&(approvedEditors[k])) {
if (currEditors.indexOf(approvedEditors[k].toLowerCase())==-1) {
try {
call(function() {driveFile.addEditor(approvedEditors[k].toLowerCase().replace(/\s+/g, ''));});
} catch(err) {
Logger.log(err.message);
}
}
}
}
return;
}
Here a script that will copy the spreadsheet with permissions when run. However, it will turn commentators into viewers. This is avoidable with more code but is not very easy because there is no file method for getting commentators.
function myFunction() {
var file = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()).makeCopy();
var editors = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()).getEditors();
for (var i = 0; i<editors.length;i++) {
file.addEditor(editors[i])
}
var viewers = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()).getViewers();
for (var i = 0; i<viewers.length;i++) {
file.addViewer(viewers[i])
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With