简体   繁体   中英

Run a function when a cell value changes (onEdit Function)

in the below sheet:

https://docs.google.com/spreadsheets/d/1F22Z19gi9WRaYbsrvlDyuiJX5VXfppAlCPzB5bjdMN8/edit?usp=sharing

From the script editor, I added the below code:

function onEdit(e) {
  var sheetName = 'Sheet2'
  var cellName = 'C2'

  var sortSheet = e.range.getSheet()

  if (sortSheet.getName() === sheetName) {
    if (e.range.getValue().length === cellName) {
      var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
      var e = sh.getRange(2, 3).getValues();
      var m = 'Test Message';
      var subject = 'Test Subject';
      MailApp.sendEmail(e, subject, m);
    }
  }
}

The purpose is to trigger the MailApp function when a "C2" cell value is changed in sheet2

I tested the same script but on sheet1 (where I add the cell values) and it worked but, however, when I try to apply on sheet2 (where the values are copied from sheet1), the script does not work. Any help, Thanks in advance

Please note the sheet is editable so you are very welcome to edit the script

How to fire a trigger on non-human edit

  • Both simple and installable Apps Script onEdit triggers are retricted to human-made changes
  • In addition, simple triggers

cannot access services that require authorization. For example, a simple trigger cannot send an email because the Gmail service requires authorization, but a simple trigger can translate a phrase with the Language service, which is anonymous.

A workaround would be to replace the onEdit trigger throguh an installable onChange trigger

  • The onchange trigger has less limitations, eg it can be triggered by the sheets formula IMPORTRANGE
  • What you need to do is to create a new, empty spreadsheet and paste the formula IMPORTRANGE("paste here the spreadsheet_url of the original spreadsheet", "Sheet2!E2") in cell E2 .
  • click on Allow access
  • Now each update of "E2" in "Sheet2" of the original spreadsheet will update the new spreadsheet - and this will fire the onChange trigger.

Just modify your code as following:

function onChange() { 
  var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
  var e = sh.getRange(2, 3).getValue();
  var m = 'Test Message';
  var subject = 'Test Subject';
  Logger.log("sending");
  MailApp.sendEmail(e, subject, m);
}

After chatting, we decided to incorporate the mailing inside the already available doPost() function.

function doPost (e) {


.... 

var doc = SpreadsheetApp.getActive();
var sheet = doc.getSheetByName(sheetName)
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]
var nextRow = sheet.getLastRow() + 1
var newRow = headers.map(function(header) {
  return header === 'Timestamp' ? new Date() : e.parameter[header]
})

sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])
var email = newRow[1];
var subject = 'XXXXXXXXX';
var body = 'XXXXXXX';
MailApp.sendEmail(email, subject, body);

...... //rest of the code

}

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