[英]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.