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

Categories

I have a script that takes data from one sheet when it is edited and puts that recently added data into ScriptDb.

The onEdit() trigger is fired successfully when I actually open the sheet and make an edit.

However, this sheet is edited programmatically through a script. Is onEdit() able to fire based on edits made by a script? I have not been able to make it do so.

The script that is fired with the onEdit() trigger is:

function sheetWasEdited(event) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow();
  var lastRowValues = sheet.getRange(lastRow, 2, 1, 2).getValues()[0];
  CgcEmailDatabase.addEmail(now=lastRowValues[0].toString(), email=lastRowValues[1].toString());
}
See Question&Answers more detail:os

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

1 Answer

The onEdit trigger is intended to work when an actual user edits a spreadsheet, the use case you describe should be easy to implement simply by calling your sheetWasEdited() function from the other function if the latest is part of the same project.

If the changes are made from another project (another spreadsheet or a web app) then it will become quite harder to put in place. (let us know if it is your use case)


EDIT following your comments :

The idea is to monitor the length of the sheet, keep the value somewhere (in script properties for example) and call your function if a row has been added.

This small code should do the trick, you should set a time trigger to call lookatsheet() once in a while, depending on how fast you need to react... I'd say every hour or 30' shouldn't be too much, you decide.

function lookatsheet(){
  var ss = SpreadsheetApp.openById('xxxxxxxxxxxxxxxxxxxxx');// the ID of the SS you want to look at
  var sh = ss.getSheets()[0];// first sheet
  var lastrow = sh.getLastRow();
  var formertest = ScriptProperties.getProperty('lastTest');// recover the value from the last test (will need to get called once to initiate a valid value)
  if (formertest < lastrow){
    sheetWasEdited(lastrow);// call your function with lastRow as parameter
    ScriptProperties.setProperties({'lastTest': lastrow}, true);   // store for next time
}
}

function sheetWasEdited(row) {  // modified to work with the other function
  var sheet = SpreadsheetApp.openById('xxxxxxxxxxxxxxxxxxxxx').getSheets()[0]
  var lastRowValues = sheet.getRange(row, 2, 1, 2).getValues()[0];
  CgcEmailDatabase.addEmail(now=lastRowValues[0].toString(), email=lastRowValues[1].toString());
}

NOTE: may be useful to comment the mail call on the first run to avoid sending a mail ( just to initiate the script properties)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...