简体   繁体   中英

How can I trigger Google Spreadsheets script if a specific cell contains a value greater than, say, 10

So I have a script that would launch an e-mail, and I want it to be run when a specific cell (A5) contains a value over 10. Here's the script:

function onEdit() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rownum = 1; // #of rows
  var rowstart = 2; // where to start row

  // get range of cell
  // column 1 = email, column 2 is first name
  var dataRange = sheet.getRange(rowstart, 1, rownum, 2)


  var data = dataRange.getValues()
  for (i in data) {
    if(sheet.getRange(5,1).getValue()>10){      //change row and column in get range to match what you need
      var row = data[i];
      var emailadr = row[0]; // data select
      var msgcontent = "Hey " + row[1] + ",\n Just:)chilling \n    Sent from my email"; // body
      var sbjt = "You have mail!"; //subject
      MailApp.sendEmail(emailadr, sbjt, msgcontent);}
    }
  }

Under Current project's triggers, I have the following:

Run: onEdit

Events: From spreadsheet On edit

Now, when I set cell "A5" to "21", the script doesn't run. What's the issue? I can't seem to figure it out. Thanks.

Using logger it shows that it does work. You can see in example below. You may want to try to use a time based trigger and set it to check every x amount of time and send email. Google may not allow the sendemail to run "onedit"

 function onEdit() { var sheet = SpreadsheetApp.getActiveSheet(); var rownum = 1; // #of rows var rowstart = 2; // where to start row // get range of cell // column 1 = email, column 2 is first name var dataRange = sheet.getRange(rowstart, 1, rownum, 2) var data = dataRange.getValues() for (i in data) { if(sheet.getRange(5,1).getValue()>10){ //change row and column in get range to match what you need var row = data[i]; Logger.log("Yes it is larger than 10"); // var emailadr = row[0]; // data select // var msgcontent = "Hey " + row[1] + ",\\n Just:)chilling \\n Sent from my email"; // body // var sbjt = "You have mail!"; //subject // MailApp.sendEmail(emailadr, sbjt, msgcontent); } } } 

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