0

really feel like I'm missing something with the Spreadsheet object scripts.

I'm trying to automatically email collaborators onEdit. I successfully emailed when explicitly runnign the script in test, but the onEdit event never seems to get fired (not seeing log messages even). Script seems pretty straightforward.

function onEdit(e) { var sheet = e.source; var viewers = sheet.getViewers(); var ct = viewers.length; var recipients = []; for(var i=0;i<ct;i++){ recipients.push(viewers[i].getEmail()); }; var subject = 'Update to '+sheet.getName(); var body = sheet.getName() + ' has been updated. Visit ' + sheet.getUrl() + ' to view the changes ' + e.range; Logger.log('Running onedit'); MailApp.sendEmail(recipients, subject, body); }; 
1
  • one correction; the version that worked when hitting Run from the script window had SpreadsheetApp.getActiveSpreadsheet() instead of e.source, but I changed it to e.source when it didn't work, on the assumption that that's hwy the event wasn't working. Commented Nov 5, 2012 at 17:07

1 Answer 1

6

the simple onEdit function has a very limited set of possible actions since it runs without the authorization of the potential user. You have to create another function and set a specific trigger on this function.(installable trigger)

See this post as an example. It shows examples of simple onEdit and installable edit (for email send) This is explained in the documentation here.

EDIT : here is your code working :

function sendAlert() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getActiveSheet(); var cell = ss.getActiveCell().getA1Notation() var viewers = ss.getViewers(); var ct = viewers.length; var recipients = []; for(var i=0;i<ct;i++){ recipients.push(viewers[i].getEmail()); }; var subject = 'Update to '+sheet.getName(); var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on cell ' + cell; MailApp.sendEmail(recipients, subject, body); }; 
Sign up to request clarification or add additional context in comments.

6 Comments

The logger has an issue since june this year and doesn't work in triggered function nor in hangler functions... still waiting for the fix ;-) (issue 1502)
ah, ok. well, the docs say to use the Resources menu of the script itself to add the trigger as an installable trigger. When doing so, I see that it's already listed as an onEdit trigger. Is there something else that needs doing?
the trigger you see is the simple trigger that comes from the name of the function (onEdit()), I'd suggest you change the function's name to avoid confusion and set the trigger manually. Then run the script once to get authorization and then it's all set.
Have you tried, with all the changes above, changing back to SpreadsheetApp.getActive() rather than e.source? I'm not sure that the source parameter is supported with installable triggers... not able to test right at this moment.
@Adam : I think you're right, I edited my answer with a working example.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.