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());
}
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.NOTE: may be useful to comment the mail call on the first run to avoid sending a mail ( just to initiate the script properties)