简体   繁体   English

当自动更新的单元格更改值时,如何让 Google 表格向您发送电子邮件

[英]How to make Google Sheets email you when an automatically updated cell changes value

I'm very new to scripting in Google Sheets so my question may be either a lot more difficult than I think, or much simpler.我对在 Google 表格中编写脚本非常陌生,所以我的问题可能比我想象的要困难得多,或者要简单得多。

Basically, I have a Google Sheet that keeps track of a list of birthdays in one column, as well as what age group that person would belong to -- Child (under 13), Teen (13-17), or Adult (18+) -- in another column.基本上,我有一个 Google 表格,可以跟踪一列中的生日列表,以及该人属于哪个年龄段 - 儿童(13 岁以下)、青少年(13-17 岁)或成人(18 岁以上) ) -- 在另一列中。 The "Age Group" column is calculated using the following formula: “年龄组”列使用以下公式计算:

=IF(DATEDIF(H2, TODAY(), "Y") >= 18, "Adult", IF(DATEDIF(H2, Today(), "Y") >= 13, "Teen", "Child"))

with column H being the Birthday column. H列是生日列。

My Google Sheet is set to recalculate the Today() function every hour, regardless of whether the file is open or not, so should a person's 13th or 18th birthday roll around, the Age Group column will update automatically.我的 Google Sheet 设置为每小时重新计算 Today() 函数,无论文件是否打开,因此如果一个人的 13 岁或 18 岁生日快到了,年龄组列将自动更新。

My goal is to create a function that sends me an email whenever somebody's Age Group changes from Child to Teen or from Teen to Adult -- without requiring the Google Sheet to be opened by the user, or modified in any way, to trigger.我的目标是创建一个函数,当某人的年龄组从儿童变为青少年或从青少年变为成人时,它会向我发送电子邮件——无需用户打开或以任何方式修改 Google 表格即可触发。

So far, I've been experimenting with "on edit" triggers, but I've noticed that this requires me to manually change an Age Group cell to trigger -- defeating the purpose of automation.到目前为止,我一直在试验“编辑时”触发器,但我注意到这需要我手动更改要触发的年龄组单元格——这违背了自动化的目的。

I've also tried changing my Trigger Event Type from "on edit" to "on change", hoping this would trigger the script whenever a cell in the Age Group column changes its resulting value, but this has not worked so far.我还尝试将我的触发事件类型从“编辑时”更改为“更改时”,希望这会在“年龄组”列中的单元格更改其结果值时触发脚本,但这到目前为止还没有奏效。

My code currently stands as follows (note that this code only makes a notice pop up rather than sending an actual email -- I haven't yet gotten to that part):我的代码目前如下(请注意,此代码只会弹出通知而不是发送实际的电子邮件——我还没有到那部分):

/*
This is the function that is triggered by an edit to
the Google Sheet
*/
function onEdit(e) {
  sendEmailOnApproval(e);  
}

/*
Primarily used for earlier debugging
*/
function showMessageOnApproval(e)
{
  var edited_row = checkStatusIsApproved(e);
  if(edited_row > 0)
  {
    SpreadsheetApp.getUi().alert("Row # "+edited_row+" approved!");
  }
}

/*
Determines if the Age Group column reads Teen or Adult.
Column 9 (or 'I' in the sheet) is the Age Group column.
*/
function checkStatusIsApproved(e)
{
  var range = e.range;

  if(range.getColumn() <= 9 && 
     range.getLastColumn() >=9 )
  {
    var edited_row = range.getRow();

    var status = SpreadsheetApp.getActiveSheet().getRange(edited_row,9).getValue();
    if(status == 'Adult' || status == 'Teen')
    {
      return edited_row;
    }
  }
  return 0;
}

function sendEmailOnApproval(e)
{
  var approved_row = checkStatusIsApproved(e);

  if(approved_row <= 0)
  {
    return;
  }

  sendEmailByRow(approved_row);
}


/*
At the moment, this function only creates a little pop-up
box containing the text of the email I would like to send.
*/
function sendEmailByRow(row)
{
  var values = SpreadsheetApp.getActiveSheet().getRange(row,1,row,9).getValues();
  var row_values = values[0];

  var mail = composeApprovedEmail(row_values);

  SpreadsheetApp.getUi().alert(" subject is "+mail.subject+"\n message "+mail.message);
}


/*
Writes the email. Column 0 (or 'A') is the first name,
1 (or 'B') is the last name.
*/
function composeApprovedEmail(row_values)
{
  var first_name = row_values[0];

  var last_name = row_values[1];

  var age = row_values[8];

  var message = "The following person should be updated on the website: "+first_name+" "+last_name+
    " Age Range: "+age;
  var subject = "UPDATE WEBSITE for "+first_name+" "+last_name;

  return({message:message,subject:subject});
}

While this code works when manually changing the values in the Age Range column, how can I adapt it to catch the automatic updates caused by daily recalculation of the Today() function the column relies on?虽然此代码在手动更改 Age Range 列中的值时有效,但我如何调整它以捕获由列所依赖的 Today() 函数的每日重新计算引起的自动更新?

According to the Apps Script Trigger documentation :根据Apps Script Trigger 文档

  • onOpen(e) runs when a user opens a spreadsheet, document, presentation, or form that the user has permission to edit onOpen(e)用户打开用户有权编辑的电子表格、文档、演示文稿或表单时运行

  • onEdit(e) runs when a user changes a value in a spreadsheet onEdit(e)用户更改电子表格中的值时运行

Therefore, what you are trying to do cannot be done by using the method that you were previously using since you were trying to use a trigger which would fire every time a change was made by a function (the Sheets formula) and not by a user.因此,您尝试执行的操作无法通过使用之前使用的方法来完成,因为您尝试使用触发器,每次函数(表格公式)而不是用户进行更改时都会触发该触发器.

Workaround解决方法

What you can do instead is to:你可以做的是:

  1. Use the Sheets formula you were previously using but change the Recalculation settings to On change instead of On change and every hour like you were previously doing.使用您以前使用的表格的公式,但改变重新计算设置在变化,而不是在变化,就像你每一个小时以前做的事情。

In this way, you will still get the ages categorized based on the dates provided.这样,您仍然会根据提供的日期获得分类的年龄。

  1. Use the following script:使用以下脚本:

function ageChanges(){
  var ss = SpreadsheetApp.getActive().getActiveSheet();
  var dateVals = ss.getRange("YOUR_RANGE_FOR_THE_DATES").getValues();
  var ageVals = ss.getRange("YOUR_RANGE_FOR_THE_AGES").getValues();
  var dateNow = new Date();
  var recipient = "EMAIL_ADDRESS";
  var subject = "Age Change in Spreadsheet";
  var body = "";

  for (var i=0; i<dateVals.length; i++){
    
    var ages = (dateNow - dateVals[i][0])/(3.154e+10);

    if (ages > 18 && ageVals[i][0]!="Adult") {
      ss.getRange(i+1, 9).setValue("Adult");
      body = "Change in column H, row"+i+".";
      MailApp.sendEmail(recipient,subject,body);
      
    }
    else if (ages > 13 && ages < 18 && ageVals[i][0]!="Teen") {
      ss.getRange(i+1, 9).setValue("Teen");
      body = "Change in column H, row"+i+".";
      MailApp.sendEmail(recipient,subject,body);
    }
      
  }
     
}

The script computes the ages based on the dates you have in your Spreadsheet.该脚本根据您在电子表格中的日期计算年龄。 If the age does not match with the corresponding category, a mail will be sent with the changes made.如果年龄与相应的类别匹配,则会发送一封包含所做更改的邮件。

In order to monitor the changes daily you will have to use the above script as an installable trigger .为了每天监视更改,您必须使用上述脚本作为可安装的触发器

  1. Create an installable trigger with the following options:使用以下选项创建可安装的触发器:

在此处输入图片说明

In this way, the script will run every day at the time of the day selected by you (in this case, Midnight to 1am ) and if any age changes occur, you will receive the email(s) with the changes.这样,脚本将每天在您选择的时间(在这种情况下,午夜到凌晨 1 点)运行,如果发生任何年龄变化,您将收到包含更改的电子邮件。

Furthermore, I suggest you take a look at these links since they might be of help in your future development:此外,我建议您查看这些链接,因为它们可能对您未来的开发有所帮助:

暂无
暂无

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

相关问题 当 Google 表格上的单元格值更改(基于公式)时,如何获取 email 警报? - How to get email alerts when a cell value changes (based on Formula) on Google sheets? 当特定单元格的值更改时,如何使Google表格脚本发送电子邮件? - How do I make a Google Sheet script send an email when a specific cell's value changes? 如何使用脚本在 Google 表格中更新单元格时添加时间戳? - How to time stamp when a cell was updated in Google Sheets using scripts? 表格中单元格颜色更改时触发电子邮件 - Email trigger when cell color changes in sheets 如何使用Google表格脚本将Google表格单元格数据添加到html电子邮件中? - How do you add google sheet cell data into an html-email using Google Scripts for Sheets? 如何在谷歌应用程序脚本的 forEach 循环中设置谷歌表格中目标单元格的值? - How can you set the value of a target cell in google sheets within a forEach loop in google apps script? 根据谷歌表格中的单元格值更改将 email 发送到特定地址 - Send email to specific addresses based on cell value change in google sheets 谷歌表格中的按钮,当点击时,改变特定单元格的背景 - Button in google sheets, when clicked, changes the background of a specific cell 当 Google 电子表格中的单元格更新时通过电子邮件通知 - Notify via email when a cell is updated in google spreadsheet Javascript:当您对 DOM 进行更改时,是否更新了整个 DOM? - Javascript: Is the Entire DOM Updated When You Make Changes to the DOM?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM