简体   繁体   English

当单元格为特定值时从 Google Sheet 发送电子邮件

[英]Send email from Google Sheet when cell is certain value

Here is an example of the sheet I am trying to make this work for: https://docs.google.com/spreadsheets/d/1M79ki9QVRkfkwy1uWyNAVddeyPpcvqDppi3Et-b-cLw/edit?usp=sharing以下是我正在尝试为其工作的工作表示例: https : //docs.google.com/spreadsheets/d/1M79ki9QVRkfkfkwy1uWyNAVddeyPpcvqDppi3Et-b-cLw/edit?usp=sharing

The goal is that an email is sent when column I is greater than 2 and column K is null.目标是当 I 列大于 2 且 K 列为空时发送电子邮件。 Column I is a count formula based on columns CH, which are manually filled in.第 I 列是基于 CH 列的计数公式,这些列是手动填写的。

Here is the script I have, but it doesn't seem to be working:这是我的脚本,但它似乎不起作用:

function sendEmail() {
  
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName("Workable");

  var listing_id = sheet.getRange("B:B").getValues();
  var flag_count = sheet.getRange("I:I").getValues();
  var action_type = sheet.getRange("K:K").getValues();

  var subject = 'New Listing Flagged';
  var message = 'Listing' + listing_id + 'has been flagged. Please resolve ASAP in the QA Google Sheet. Thank you!';
  var email_address = 'allyson@hipcamp.com';

  if (flag_count > 2 && action_type == ""){MailApp.sendEmail(email_address, subject, message)}
}

What do I need to change to make it work?我需要改变什么才能使它工作?

I got your latest code in the sample sheet.我在示例表中找到了您的最新代码。 Since you've decided to use onEdit trigger, you need to use the installable onEdit trigger because you are using MailApp.sendEmail() service which requires authorization.由于您已决定使用 onEdit 触发器,因此您需要使用可安装的 onEdit 触发器,因为您使用的是需要授权的MailApp.sendEmail()服务。

Pre-requisite (installable trigger):先决条件(可安装触发器):

在此处输入图片说明

在此处输入图片说明


Sample Code:示例代码:

function sendNotification1(e) {
  Logger.log(JSON.stringify(e));
  var ss = SpreadsheetApp.getActiveSpreadsheet();   
  var sheet = ss.getActiveSheet();
  var row = e.range.getRow();
  var col = e.range.getColumn();
  var startRow = 2;
  var startCol = 3; //Column C
  var endCol = 8; //Column H

  if(sheet.getName() == "Workable"
    && col >= startCol
    && col <=  endCol
    && row >= startRow
    && sheet.getRange(row,9).getValue() > 2 //flag_count column > 2
    && sheet.getRange(row,11).getValue() == "" //action column null
    )
  {
    var campground_id = sheet.getRange(row,2).getValue();
    var email = 'your@email.com';
    var subject = "New Listing Flagged for Fraud";
    var body = 'Campground ' + campground_id + ' has been flagged for fraud. Please resolve ASAP in the QA Google Sheet. Thank you!';
    MailApp.sendEmail(email, subject, body);
  }
 }

What it does?它能做什么?

  1. Your column I value depends on the value provided in column CH.您的 I 列值取决于 CH 列中提供的值。 Hence your trigger should be the change in values done in column CH (column index 3-8).因此,您的触发器应该是 CH 列(列索引 3-8)中完成的值更改。 I include an additional condition to check the current active sheet to verify that the modified cells are in sheet Workable .我添加了一个额外的条件来检查当前活动的工作表,以验证修改后的单元格在工作表Workable Check the value in column I if greater than 2. Lastly, check if column K is null/empty如果大于 2,则检查列 I 中的值。最后,检查列 K 是否为空/空
  2. Send the email发送电子邮件

Output:输出:

在此处输入图片说明

在此处输入图片说明

(Update) (更新)

Checkbox column included that will act as a checker button to trigger the email notification.包含的复选框列将用作检查器按钮以触发电子邮件通知。

Sample Code:示例代码:

function sendNotification(e) {
  Logger.log(JSON.stringify(e));
  var ss = SpreadsheetApp.getActiveSpreadsheet();   
  var sheet = ss.getActiveSheet();
  var row = e.range.getRow();
  var col = e.range.getColumn();
  var startRow = 2;

  if(sheet.getName() == "Workable"
    && col == 9
    && row >= startRow
    && sheet.getRange(row,9).isChecked()
    && sheet.getRange(row,10).getValue() > 2 //flag_count column > 2
    && sheet.getRange(row,12).getValue() == "" //action column null
    )
  {
    var campground_id = sheet.getRange(row,2).getValue();
    var email = 'your@email.com';
    var subject = "New Listing Flagged for Fraud";
    var body = 'Campground ' + campground_id + ' has been flagged for fraud. Please resolve ASAP in the QA Google Sheet. Thank you!';
    MailApp.sendEmail(email, subject, body);
  }
 }

Sample Sheet:样品表:

在此处输入图片说明

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

相关问题 从单元格值Google工作表发送电子邮件,以及电子邮件中的其他列 - Send email from cell value google sheet, with other columns in email Google Sheet 在单元格具有特定值时发送电子邮件 - Google Sheet to send an email when cell has specific value 当特定单元格的值更改时,如何使Google表格脚本发送电子邮件? - How do I make a Google Sheet script send an email when a specific cell's value changes? 如何使用多个单元格值从 Google 工作表中查找值并发送电子邮件 - How to find value from Google sheet using multiple cell values and send email G Sheet 脚本在值超过某个级别时发送电子邮件 - G Sheet Script to send email when a value is over a certain level 当某个单元格的值更改时,Google工作表onEdit - Google sheet onEdit when value of a certain cell changes 填充某些列时向某些用户发送自动电子邮件 - 谷歌表 - Send automated email to certain users when certain column has been filled - google sheet 库存-当单元格值小于2时发送电子邮件(Google电子表格) - Stock Inventory - Send email when cell value < 2 (Google Spreadsheet) 根据Google表格单元格的值发送邮件 - Send mail based on Google Sheet Cell value 当谷歌工作表单元格只有两列空白时如何发送电子邮件? - How to send an email when google sheet cell is blank for two columns only?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM