2

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!

wulfae
  • 63
  • 1
  • 9
  • Are you the owner or an editor of the file? – Rubén Oct 18 '16 at 19:16
  • I am the owner of the file. Thank you for pointing that out, I will try to find some code that lets me add whoever is running it as an editor, just in case they are not the owner. Edited my question to reflect this. – wulfae Oct 18 '16 at 20:21
  • Kindly check this [SO thread](http://stackoverflow.com/questions/18804649/google-apps-script-were-sorry-a-server-error-occurred-please-wait-a-bit-and) if the answer applies to your situation. – ReyAnthonyRenacia Oct 19 '16 at 13:01
  • ARGH. I had commented everything else out except for the removing editors function, and I still got the server error. I just tried again, and I didn't get it! So this is a frustrating, intermittent error that comes and goes at will. I'm trying the entire script again, to see if I get it, but... I guess people will just have to manually add in new people to the protections if this error pops up. – wulfae Oct 19 '16 at 14:56
  • @noogui: Commented everything out, and it was still the 'getEditors' that is the problem. Unfortunately I need to both get and then remove editors for the rest of the script to work, so I don't know where to go from here. But at least I know for sure that it is this line of code that is the problem! - my problem has reappeared, that's why the update. – wulfae Oct 24 '16 at 02:30

3 Answers3

0

Posting an answer because my problem has disappeared? I didn't change anything, but I tried it this morning and it is working. Same sheet, same code, no server error.

This exact same code ran on different copies of this sheet (of which I was also the owner) at the same time as this stupid server error popped up on one, so I really don't know what happened.

I will just pray to the Google Gods and hope that nothing goes wrong. (Best plan, y/y?)

Thank you to Rubén and noogui for their input.

wulfae
  • 63
  • 1
  • 9
0

Had the same thing happen to me. Had this Gscript that i couldnt get to run for the life of me. Restarted computer. Signed out of everything. Reuploaded the script. Went to bed. Next morning everything worked fine.

Zac Henson
  • 59
  • 1
  • 7
  • It's not working again... OMG I just need this to work so I can run it on all of my sheets and wash my hands of it! But the fact that it worked for you the next day gives me hope that tomorrow it will work??? – wulfae Oct 24 '16 at 02:12
0

I had a similar problem, but here is what I found ... basically I was linking to a library that was either removed or I lost permissions to. Since I wasn't using this particular library, I removed the library and now all my sheets work again.