简体   繁体   English

Google 表格脚本 - 将 email 添加到 Google 表格时自动发送带有值的值

[英]Google Sheet script - Automatically send an email with values when they are added to a Google Sheet

I've been trying to follow the document below but have got a bit stuck.我一直在尝试遵循下面的文档,但有点卡住了。 In short, I am trying to have a Google Sheet send out an email automatically when two values are added to a Google Sheet (Name, Telephone)简而言之,当将两个值添加到 Google 表格(姓名、电话)时,我试图让 Google 表格自动发送 email

The function below works great when I run it from App script but I am looking for it to grab the values that are inserted at the time and send the email automatically rather than having to run the script each time in App Script.下面的 function 在我从 App 脚本运行时效果很好,但我正在寻找它来获取当时插入的值并自动发送 email 而不必每次都在 App Script 中运行脚本。

// Fetch the email address
var emailRange = 
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("B2");
var emailAddress = emailRange.getValue();
// Send Alert Email.
var message = 'This is your Alert email!'; // Second column
var subject = 'Your Google Spreadsheet Alert';
MailApp.sendEmail(emailAddress, subject, message);

https://www.groovypost.com/howto/google-sheets-send-email-based-on-cell-value/ https://www.groovypost.com/howto/google-sheets-send-email-based-on-cell-value/

You need to use the onEdit feature.您需要使用 onEdit 功能。 https://developers.google.com/apps-script/guides/triggers/events https://developers.google.com/apps-script/guides/triggers/events

function onEdit(e){
  sendEmailToUpdatedValue(e)
}
function sendEmailToUpdatedValue(e){
  let email_x = /\b[\w\.\-\+]+@[\w\-]+\.[a-zA-Z]{2,13}(\.[a-zA-Z]{2,13}|\b)/;
  let email = e.range.getValue();
  let is_email = email_x.test(email);
  if(is_email){
    //your email function here
  }
}

Be sure to run the onEdit function once within Apps Script in order to ensure the trigger is set up.请务必在 Apps 脚本中运行一次 onEdit function 以确保设置触发器。 That first time will get an error.第一次会出错。

Keep in mind that onEdit only fires from a user action, so if the emails are being added from a form or some other script, this will not work.请记住,onEdit 仅从用户操作触发,因此如果电子邮件是从表单或其他脚本添加的,这将不起作用。 In those scenarios you would need a time based trigger.在这些情况下,您将需要基于时间的触发器。

暂无
暂无

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

相关问题 在 Google 表格中添加一封订阅电子邮件时发送电子邮件 - Send email when one subscription email is added on Google sheet Google Sheet Auto Email 脚本 - Google Sheet Auto Email Script 当新行添加到谷歌表而不添加多个事件时,自动运行 function 以在谷歌脚本中创建新事件 - automatically run a function to create a new event in google script when a new row is added to google sheet without adding multiple events 当特定单元格的值更改时,如何使Google表格脚本发送电子邮件? - How do I make a Google Sheet script send an email when a specific cell's value changes? 满足多个列中的多个条件时发送电子邮件-Google表格脚本 - Send email when multiple conditions across multiple columns are met - Google Sheet Script BatchUpdate Google Apps 脚本正在自动清除工作表 - BatchUpdate Google Apps Script is clearing sheet automatically 如何从谷歌表格发送 email 表格作为 PDF 附件 - How to send email from google sheet with the sheet as a PDF attachment 如何使用谷歌应用程序脚本和谷歌工作表数据逐行发送 html 电子邮件 - How to send html email line by line using google apps script and google sheet data 将Google工作表转换为html并使用Google App脚本作为电子邮件发送 - Convert google sheet to html and send as email using google app scripts Google App Script + Google Sheet + HTML 模态对话框输入表单 - 获取值并构建 email - Google App Script + Google Sheet + HTML modal dialog input form - Get values and build email
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM