简体   繁体   中英

Google Sheets API V4 and google apps script

I have been using the Sheets API v4 to allow authorised users enter data into a spreadsheet from a form. What I want to do is have a google apps script trigger a function when ever new data is added. I cant seem to get the on edit trigger to work using

function createSpreadsheetEditTrigger() {
  var ss = SpreadsheetApp.getActive();
  ScriptApp.newTrigger('makeLog')
      .forSpreadsheet(ss)
      .onEdit()
      .create();
}

function makeLog(){
Logger.log("New Entry")
}

Is it possible to do that?

How about following sample script? When spreadsheet was edited from outside by Sheet API v4, onChange() becomes a trigger for it. So please change from onEdit() to onChange() for your script. I prepared a sample script I used for testing.

This sample script send an e-mail, when spreadsheet was edited by Sheet API v4. I confirmed that this works fine. When you use this sample, please input your e-mail and run firstly createSpreadsheetEditTrigger() . By this, a trigger is installed. After this, edit spreadsheet by Sheet API v4.

When when spreadsheet was edited from outside by Sheet API v4, I used sendEmail() as a sample, because script editor is closed.

Sample script :

function createSpreadsheetEditTrigger(){
  var ss = SpreadsheetApp.getActive();
  ScriptApp.newTrigger('makeLog')
      .forSpreadsheet(ss)
      .onChange()
      .create();
}

function makeLog() {
  MailApp.sendEmail("### Your e-mail address ###", "test mail", "Spreadsheet was edited.");
}

If I misunderstand your question, I'm sorry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM