Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

triggers - Google App script onEdit?

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);
};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...