So I have (what I assume is) a fairly long script to automate giving permissions in Google Sheets. To make it work, I have to first remove all the editors, otherwise Google just puts them in every category. I can't figure out how to stop that, short of removing them completely.
In my test file, I am the owner. Though I assume I need to add something that will add the script runner's account as an editor, just in case they are not. (edited to add: Apparently you can not remove yourself as editor, so even if you are not the owner, you can still run the rest of the script. Thank you, google! EDITED AGAIN: THAT IS A FILTHY LIE. I wish I had linked to the person who said this, because they are WRONG. You can remove yourself, I'm now waiting for the owner to add me back in. 9__6)
So I'm using this:
//removes all Editors
var SpreadSheet = SpreadsheetApp.getActiveSpreadsheet();
var editors = SpreadSheet.getEditors();
for (var i = 0; i < editors.length; i++) {
SpreadSheet.removeEditor(editors[i]);
};
Which has been working, in this exact file, every time I test it. Yesterday, however, I got this error:
"We're sorry, a server error occurred. Please wait a bit and try again."
referencing this line specifically:
var editors = SpreadSheet.getEditors();
But it works in the other test sheets I've made. I'm worried that it is just going to randomly not work when I deploy it on the floor, and I will have no idea why, or how to fix it.
Is there a better way to accomplish this? These sheets are live already, and have been shared with tonnes of people, so I need to either remove their edit permissions or switch their edit type to 'view'.
(I know you can do it for folders, but I would like it specifically for a file, since I am ultimately not the one who owns these files and who knows how they have been organised.)
Thank you for any help!
....
October 23rd, 2016 - It's happening again, on multiple different sheets. I commented everything out, and it is definitely this bit that is causing problems:
//removes all Editors
var SpreadSheet = SpreadsheetApp.getActiveSpreadsheet();
var editors = SpreadSheet.getEditors();
for (var i = 0; i < editors.length; i++) {
SpreadSheet.removeEditor(editors[i]);
};
The getEditors is too much, man. Is there another way to do this? Is there another way to remove editors, or set them to 'view only' and then back to 'edit' when I'm done?
(If google would just put only the emails in each protection range as I've defined it instead of everyone, I would not have this problem. But they will not, and I have to remove everyone for this script to work.)
I have already looked at a lot of the linked answers, and they didn't seem to reply. Is getEditors too heavy to use reliably?
I am really not a scripter, and could use any links or guidance anyone could give.
I also can't seem to unaccept my own answer as an answer...
edited 11/14/2016 - Problem persists. The linked answer talks about using the "string replace function", and how changing that made the script work. I am not using that. The rest of my script is a repeat of this type of thing:
//Sheet1
var sh3 = ss.getSheetByName("Sheet1");
var protection = sh3.protect().setDescription('Sheet1 - Wizards'); //protects whole sheet
protection.addEditors(['wizard1@test.com',
'wizard2@test.com',
'wizard3@test.com']);
//adds email addresses to WIZARD range
var unprotected = sh3.getRange('A10:T');
protection.setUnprotectedRanges([sh3.getRange("B3:B3"),
sh3.getRange("C1"),
sh3.getRange("B72:B72"),
sh3.getRange("C2:K8"),
sh3.getRange("A10:T70"),
sh3.getRange("A75:Q")]);
//unprotects everything but frozen header. (unprotects the Sheet1 Legend too.)
var range = sh3.getRange('C2:K8'); //selects the range
var protection = range.protect().setDescription('Sheet1 - Legend'); //names the range
var me = Session.getEffectiveUser();
protection.addEditors(['wizard1@test.com',
'wizard2@test.com',
'wizard3@test.com'
'supervisor1@test.com’]);
//put Sheet1 supervisor emails here
var range = sh3.getRange('C1'); //selects the range
var protection = range.protect().setDescription('Sheet1 - Part Number'); //names the range
var me = Session.getEffectiveUser();
protection.addEditors(['wizard1@test.com',
'wizard2@test.com',
'wizard3@test.com'
'supervisor1@test.com’]);
//put Sheet1 supervisor emails here
var range = sh3.getRange('A10:T38'); //selects the range
var protection = range.protect().setDescription('Sheet1 - worker 1'); //names the range
var me = Session.getEffectiveUser();
protection.addEditors(['wizard1@test.com',
'wizard2@test.com',
'wizard3@test.com',
'lead1@test.com’,
'worker1@test.com',
'worker2@test.com']);
//put emails here - Sheet1 worker level
var range = sh3.getRange('A43:Q'); //selects the range
var protection = range.protect().setDescription('Sheet1 - Artists 2'); //names the range`
var me = Session.getEffectiveUser();
protection.addEditors(['wizard1@test.com',
'wizard2@test.com',
'wizard3@test.com'
'lead1@test.com’
'worker1@test.com',
'worker2@test.com']);
//put emails here - Sheet1 worker level
... Omg the spacing is so off, please ignore that and any commas I might have missed going through. I needed to quickly anonymise this so I can post it. The actual code is spaced right, and does have all the proper commas.
So what am I doing wrong??? Why is google freaking out over the getEditors() bit, and why only in certain sheets??
I saw somewhere else that this is just a catch-all error for "google has no idea wth just happened, but it didn't work. Try again later, and maybe it will work? Fingers crossed????"
Which is frustrating AS HECK because this is not a fancy function at all.
November 29th, 2016: Had a few spare minutes on my hands today, so I thought I'd try this script again. Still didn't work.
But oh my god I think I've cracked it! It's to do with the sharing settings! When the file was originally created, the 'anyone with a link' was selected as the option for sharing.
Well, I thought that didn't give enough security, so I switched my template to 'only specific people'. I then manually removed everyone, thinking maybe this would work.
Ran it again, it worked.
Tried it again, without manually removing, hoping that just changing the sharing settings would work. IT DID!!! So I went to fix the one far down the pipeline that was sitting there with no protections.
Turns OUT that you CAN remove yourself through scripting, even though I had read somewhere that google would not do that. So, since I am not the owner of the sheet, I am now locked out. Which will be another problem, and another question.
But as soon as I get this licked, or get the actual owner to run the script, I will update if this has actually solved it.
I will be so much happier when this works!