2
votes

I have a spreadsheet to track data, that every user with the link can edit. While I understand, that it is a security risk, I do not mind, because the spreadsheet is only know and of interest to the guild I am in.

Further more certain areas are protected and only administrators of the document have access to it.

What I want to do, is, have a GAS respond to "onEdit" for all (anonymous) users.

The easiest example is this:

Code.gs

function onEdit(e) { Browser.msgBox('Editing...'); }

I attached this to the table -> on edit trigger and it does display for me - as I am logged in OR because I am the administrator of this document. However: other (anonymous) users do not get the message box for some reason. I have read through the permissions sheet and it states, that apparently you HAVE to be logged in if you want to use any kind of scripts (except for those time-driven ones, that are executed under the name of the one, who added them).

Is there no workaround? My triggers just copy values from one table to another, ordering and formatting them.

Thank you very much in advance!

1

1 Answers

7
votes

There are actually 2 onEdit triggers, the "simple" one and the "installable" one.

The simple onEdit runs as the user accessing the spreadsheet without asking for any authorization, that means it is unable to execute anything that requires authorization and using the SpreadsheetApp methods requires authorization (as you noticed when you ran that script yourself for the first time).

The installable triggers (onEdit, onChange, etc...) run as the author of the script , to be more accurate, they run as the user who installed the triggers.

They (the triggered functions) have the same "rights" as the user himself and can do everything the user can do.

Anonymous (not logged in) users that use the spreadsheet will trigger this script but they won't be responsible for what the script does and everything will be done in the name of the trigger "installer".

Feel free to try this spreadsheet where I wrote this script :

function onEdit(e) {
  SpreadsheetApp.getActiveRange().setValue('simple onEdit Trigger running as sheet user without authorization');
}

function onEditInstallable(e) { 
  SpreadsheetApp.getActiveRange().setValue('Installable onEdit Trigger running as script author');
}

Please make also a copy of it after you tried and test that copy without installing any special trigger.

Then compare the results : in my sheet you'll get the message from the second function and in yours it will be the message from the first one.

When the installable trigger is active and you edit the sheet, both functions will be triggered and there is a chance that you actually see both texts successively since they generally don't take exactly the same time to run.

Anonymous user will always see the second one if you install a trigger in your sheet that calls the onEditInstallable function onEdit (from the script editor goto Ressources/current project triggers/create new trigger and select the one you need)

Documentation on that subject : simple triggers and installable ones