Hàm onedit e và onchange e google sheet

Hello all,

Show

I have a bit of a technical question about Appsheets way of editing the Google Spreadsheet that it uses for it’s Database.

I’m writing some Google Script on the Spreadsheet I’m using as Database for the App I’m making in Appsheet. It’s meant to send an email when a New Row is Added.

The Script works great, but when Adding a New Row by filling in a Form through the Appshet App it doesn’t fire the script onEdit or onChange as it would if that New Row was added via a Google Form.

Am I wrong on this, or is this true in your experience as well?


My workaround, in case anyone is looking for solutions here, is that I made a Mark column (“sent” or empty). Then I run the script as TimeDriven every half hour and check for the last 10 rows with data from the bottom to the top of the spreadsheet, then check for the mark and if it’s not sent then fire the “SendEmail” function and mark the column “Sent” so that on the next search, it wont send the email again.
And it works

Hàm onedit e và onchange e google sheet

However, the thing is that if adding rows to the spreadsheet through filling or editing Forms in Appsheet is NOT recognized as an Edit or Change in the Spreadsheets Google Script, than you have to make these TimeDriven Functions and that’s really bad for two main reasons:

  • you can’t get instantaneous actions to fire on filling up the form
  • you consume a looot of resources.
    (this one function takes a total of 30 minutes to run a day. and it only runs every half hour. If it where to run every minute … and it’s just one function … and it has to take all those resources, even if there was no filling of the form done that day and thus no emails sent)

Any idea on this subject?
It would be a huuuge gain to know that you could run OnEdit functions through Google Script when filling in or editing Appsheet Forms…either directly or through some workarounds if you know any.

Thank you,
Sorin

Google Apps Script: Running Specific Function When Specific Sheet is Edited on Google Spreadsheet

This is a sample Google Apps Script for running the specific function when the specific sheet is edited.

Sample script

Please copy and paste the following script to the container-bound script of Spreadsheet and set sheets object.

// When the cells are edited, this function is run by the fire of event trigger.
function onEdit(e) {
  // Please set the sheet name and function as follows.
  const sheets = {
    Sheet1: functionForSheet1, // Sheet1 is the sheet name. functionForSheet1 is the function name of function which is run when Sheet1 is edited.
    Sheet2: functionForSheet2,
  };

  const sheetName = e.range.getSheet().getSheetName();
  if (sheets[sheetName]) {
    sheets[sheetName](e);
  }
}

// In this sample, when Sheet1 is edited, this function is run.
function functionForSheet1(e) {
  console.log("Sheet1 was edited.");

  // do something
}

// In this sample, when Sheet2 is edited, this function is run.
function functionForSheet2(e) {
  console.log("Sheet2 was edited.");

  // do something
}

  • In this sample script, when the cells of "Sheet1" and "Sheet2" are edited, functionForSheet1() and functionForSheet2() are run, respectively. When other sheets are edited, no functions are run.
  • In this sample script, onEdit of the simple trigger is used. When the functions you want to run include the methods which are required to authorize, please use the installable trigger.

Note

  • This method can be also used for other event triggers like OnChange, OnSelectionChange and so son.

References

  • Simple Triggers
  • Installable Triggers
  • Event Objects

Testing

  • October 16, 2020: I could confirm that this sample script worked. In the current stage, it seems that the specification at Google side is not changed.