简体   繁体   中英

Spreadsheets and Google Apps Script: trigger when value in cell matches

I'm a complete beginner, but step by step I'm beginning to understand the basics of Apps Script. I found this example mail merge script on Google Dev: https://developers.google.com/apps-script/articles/mail_merge

This is the part of the script I'm talking about:

    function sendEmails() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var dataSheet = ss.getSheets()[0];
    var dataRange = dataSheet.getRange(2, 1, dataSheet.getMaxRows() - 1, 4);

    var templateSheet = ss.getSheets()[1];
    var emailTemplate = templateSheet.getRange("A1").getValue();

    // Create one JavaScript object per row of data.
    var objects = getRowsData(dataSheet, dataRange);

    // For every row object, create a personalized email from a template and send
    // it to the appropriate person.
    for (var i = 0; i < objects.length; ++i) {
    // Get a row object
    var rowData = objects[i];

    // Generate a personalized email.
    // Given a template string, replace markers (for instance ${"First         Name"}) with
    // the corresponding value in a row object (for instance rowData.firstName).
    var emailText = fillInTemplateFromObject(emailTemplate, rowData);
    var emailSubject = "Tutorial: Simple Mail Merge";

    MailApp.sendEmail(rowData.emailAddress, emailSubject, emailText);
      }
    }

I'm trying to change the script so it only runs when the value of a cell in a specific column matches the given value. I'll give you an example:

Using a formula in Spreadsheets the values of the cells in column E are 'Yes' if a condition is fulfilled. I want the mail merge script to only send an email if the value is 'Yes'. Furthermore I want to script to run autonomously.

Example Spreadsheet

I was looking into using triggers to accomplish this, but right now I'm at a loss. Can you guys give me some pointers as to where to look? A friend of mine said it probably isn't possible, but I'd love a second opinion. Thanks in advance!

I'm a total beginner, too. Probably there are better ways to achieve your goal, but here is my solution:

I named your column E in the spreadsheet "condition", this is where the "yes" or the "no"'s are placed.

The code you cited, there are only two differences:

function sendEmails() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var dataSheet = ss.getSheets()[0];

    // here is the first difference:
    // change the dataRange, last number from 4 to 5, because we want to catch five columns:
    var dataRange = dataSheet.getRange(2, 1, dataSheet.getMaxRows() - 1, 5);    

    var templateSheet = ss.getSheets()[1];
    var emailTemplate = templateSheet.getRange("A1").getValue();


    // Create one JavaScript object per row of data.
    var objects = getRowsData(dataSheet, dataRange);    

    // For every row object, create a personalized email from a template and send
    // it to the appropriate person.
    for (var i = 0; i < objects.length; ++i) {
    // Get a row object
    var rowData = objects[i];    

    // Generate a personalized email.
    // Given a template string, replace markers (for instance ${"First         Name"}) with
    // the corresponding value in a row object (for instance rowData.firstName).
    var emailText = fillInTemplateFromObject(emailTemplate, rowData);
    var emailSubject = "Tutorial: Simple Mail Merge";

    // here comes the second difference:
    // test if the rowData named "condition" is "yes", if true, the mail is send
    if (rowData.condition == "yes"){
        GmailApp.sendEmail(rowData.mail, emailSubject, emailText);
        }
      }
    }

I changed to GmailApp because MailApp isn't working for me.

The script can run autonomously, you can set a trigger: Find it under Ressources, trigger from the actual project, choose the function "sendmail", trigger "on Form Submit". When the condition comes out of a formula, you should take a look at the Addon " CopyDown ". It copies a formula from the second row to new rows, when a form is submitted.

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