Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

google apps script - onEdit simple trigger never seems to be triggered

I've been using OnEdit for some time and its been working great - however just now ive came accross a problem - basically when column 6 (f) is edited I want it to check the value - if it is "In Progress" I want it to then run a function to send an email, when I debug this - it works great exactly as I want it to, however when im not debugging it never seems to run.

var summary = ss.getSheetByName("Summary")
var data = summary.getRange(4, 1, summary.getLastRow(), summary.getLastColumn());
var learnerObjects = getRowsData(summary, data);
var lastrow = summary.getLastRow();

function onEdit(e){
  var sheet = e.source.getActiveSheet();
  var CellRow = sheet.getActiveRange().getRow();
  var CellColumn = sheet.getActiveRange().getColumn();

  if (sheet.getSheetName() == "Summary"){
    if (CellColumn == 6){
      var learner = learnerObjects[CellRow-4];
      var status = learner.status;
      if (status == "In Progress"){
        var enrolmentdate = learner.enrolmentDateDdmmyyyy;
        var surname = learner.surname;
        var firstname = learner.firstName;
        var qualification = learner.qualification;
        var company = learner.company;
        messagePT3 = firstname + " " + surname + " from " + company + " doing " + qualification + " has been added to your tracker.";
        sendTrackerEmail(messagePT3);
        SpreadsheetApp.getActiveSpreadsheet().toast('Email has been sent to Assessor regarding new learner', 'Assessor Notified', 3);
      }
    }
  }
}

This is the main code for it, when debugging as I say it works fine, however when im not debugging it never seems to be triggered, is there any other ways for me to do this?

I have data validation on column F however this limits the cells to "Completed", "Withdrawn", "Suspended" or "In Progress" - this shouldnt have an effect should it?

I know that onEdit is being triggered as I use it for monitoring changes to certain parts of the spreadsheet.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

The onEdit() trigger is a so called "simple trigger" that has a limited field of action. Since it runs automatically and silently under the authority of the active user it can't do anything that requires authorization, for example it can't send emails !

The solution is simple : change your function name to something else (myOnEdit for example) and set an installable onEdit trigger to run that function.(menu ressource/current script trigger/create new trigger in the script editor.)

This time it will be executed as you (you'll be sending the emails) when anyone edits the spreadsheet.

Documentation on that subject is available here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...