简体   繁体   English

Google Apps 脚本 - 根据单元格中的值发送 Email

[英]Google Apps Script - Send Email based on value in cell

I am new with Google App Scripts and have a spreadsheet with three columns: name, overdue and last contacted: https://docs.google.com/spreadsheets/d/1W04JiFZwpGCD-qddUkNJ9xQ1xiYwZnGhissudF1EvxM/edit?usp=sharing我是 Google App Scripts 的新手,并且有一个包含三列的电子表格:名称、过期和上次联系: https://docs.google.com/spreadsheets/d/1W04JiFZwpGCD-qddUkNJ9xQ1xiYwZnGhissudF1EvxM/edit?usp=sharing

I would like to send an email reminder to myself which contains the name of anyone with the value overdue in column B.我想向自己发送一个 email 提醒,其中包含 B 列中逾期值的任何人的姓名。

So far, I have only been able to write a script that sends an email to myself for row 2. I can't figure out how to get the script to loop through the values in column B.到目前为止,我只能编写一个脚本,将 email 发送给我自己的第 2 行。我不知道如何让脚本循环遍历 B 列中的值。

Here is my script:这是我的脚本:

function sendEmail() {
  
  var overdueRange = SpreadsheetApp.getActiveSpreadsheet().getRange("B2"); 
  var overdueValue = overdueRange.getValue();
  if (overdueValue === "Overdue"){
    var nameRange = SpreadsheetApp.getActiveSpreadsheet().getRange("A2");
    var name = nameRange.getValues();
    var message = 'Reach out to ' + name 
    var subject = 'Reach out to this person.'
    MailApp.sendEmail('myemail@email.com', subject, message);
    }
}

sendEmail()

Explanation:解释:

Just a few points to make the process clear:只需几点说明该过程:

  • I assume you are executing sendEmail() from the script editor.我假设您正在从脚本编辑器执行sendEmail() In this way, since you also have sendEmail() outside of the function, you will end up calling sendEmail() twice.这样,由于您在 function 之外还有sendEmail() ,因此您最终将调用sendEmail()两次。 Remove the 'global' call of the function and call only sendEmail() from the script editor.删除 function 的“全局”调用,并从脚本编辑器中仅调用sendEmail()

  • It is not recommended to apply getRange on a spreadsheet object.不建议在电子表格object 上应用getRange Define a particular sheet by its name or position.按名称或 position 定义特定工作表。 In my example script below, I define the sheet by its name ( Sheet1 ).在下面的示例脚本中,我按名称 ( Sheet1 ) 定义工作表。 Change that name to your actual case.将该名称更改为您的实际情况。

  • name in your code is a 2D array and not a single value.代码中的name是二维数组,而不是单个值。 Be careful with this as it is not the correct way to do it.请注意这一点,因为这不是正确的方法。

  • In my script below, I define an array of columns A and B .在下面的脚本中,我定义了一组AB列。 Names are defined in column A and overdue status in column B .名称在A列中定义,过期状态在B列中定义。 In this way you can iterate this array, and get single values per iteration by accessing the array itself.通过这种方式,您可以迭代此数组,并通过访问数组本身在每次迭代中获取单个值。 I used forEach but feel free to use your own iteration method.我使用了forEach ,但可以随意使用您自己的迭代方法。

Solution:解决方案:

function sendEmail() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet1'); // change Sheet1 to the name of your sheet
  const data = sh.getRange('A2:B'+sh.getLastRow()).getValues();
  data.forEach(r=>{
     let overdueValue = r[1];  
     if (overdueValue === "Overdue"){
         let name = r[0];
         let message = 'Reach out to ' + name;
         let subject = 'Reach out to this person.'
         MailApp.sendEmail('myemail@email.com', subject, message); 
     }
  });  
}

Sheet that works for the script:适用于脚本的工作表:

例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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