简体   繁体   中英

How do I make a Google Sheet script send an email when a specific cell's value changes?

I'm trying to set up a script for my Google Sheet that will send an email when a specific cell's value changes. I'm also wondering if there's a way to set up the script so that it will only get sent out once a day; for example, if multiple changes are made on a day, I'd rather it only send out one email at the end of the day as opposed to sending out an email every single time.

To provide a bit of background, the spreadsheet is a statement for a customer that is published on the web. I'd like it to notify the customer when an update has been made to their remaining balance on the statement. It'd also be cool if I could plug in the cell's new value to the email message so they can see their new balance in the email itself.

I'm currently using the following script with no luck:

function myFunction(e) {

  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName("Statement");
  var valueToCheck = sheet.getRange("J13").getValue();
  var rangeEdit = e.range.getA1Notation();

  if(rangeEdit !== "J13")

    {

      MailApp.sendEmail("*****@gmail.com", "subject", "message");

    }

}

The current script is set to the onChange event trigger.

You need to set the (installable) trigger "on edit", not "on change". The event object passed by "on change" trigger does not contain range ; it basically just tells you what kind of change happened.

With on edit trigger , you can use e.range (as you did) and also e.value (new value entered), saving you the trouble of calling getValue() .

You'll need to store values in the document's (Spreadsheet's) "Properties". The document properties is just a storage feature that Google documents have.

So, the code must store the original value that was in the spreadsheet cell, and store the date that the last email was sent.

function myFunction(e) {

  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName("Statement");
  var valueToCheck = sheet.getRange("J13").getValue();
  var rangeEdit = e.range.getA1Notation();

  if (didTheValueInCellChange(valueToCheck) === false) {return}; //The value didn't change, quit

  if (rangeEdit === "J13") {
    if (hasAnEmailBeenSentToday() === false) {
      MailApp.sendEmail("*****@gmail.com", "subject", "message");
      emailJustSent();
      setOriginalValue(valueToCheck);
    };
  };
};

function emailJustSent() {
  var docProps = PropertiesService.getDocumentProperties();
  var todaysDate = new Date();//Use date constructor, then convert date object to a string
  var scriptTimeZone = Session.getScriptTimeZone();

  var formatedDate = Utilities.formatDate(todaysDate, scriptTimeZone, "MM/dd/YYYY");

  docProps.setProperty(key, value)('dateSent');
};

function hasAnEmailBeenSentToday() {
  var docProps = PropertiesService.getDocumentProperties();
  var todaysDate = new Date();//Use date constructor, then convert date object to a string
  var scriptTimeZone = Session.getScriptTimeZone();

  var formatedDate = Utilities.formatDate(todaysDate, scriptTimeZone, "MM/dd/YYYY");

  var dateWhenEmailWasSent = docProps.getProperty('dateSent');

  if (dateWhenEmailWasSent === formatedDate) {
    return true;
  } else {
    return false;
  };
};


function didTheValueInCellChange(currentValue) {

  var docProps = PropertiesService.getDocumentProperties();  
  var theLastValueSaved = docProps.getProperty('originalValue');

  if (theLastValueSaved !== currentValue) {
    return true;
  } else {
    return false;
  };
};

function setOriginalValue(theValue) {
  docProps.setProperty('originalValue', theValue);
};

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