我正在尝试为我的 Google 表格设置一个脚本,该脚本将在特定单元格的值发生更改时发送电子邮件。我还想知道是否有办法设置脚本,使其每天只发送一次;例如,如果一天进行多次更改,我宁愿它在一天结束时只发送一封电子邮件,而不是每次都发送一封电子邮件。
为了提供一些背景信息,电子表格是在网络上发布的客户声明。我希望它能够在对账单上的剩余余额进行更新时通知客户。如果我可以将单元格的新值插入到电子邮件中,这样他们就可以在电子邮件本身中看到新的余额,那就太酷了。
我目前正在使用以下脚本,但没有运气:
function myFunction(e) {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Statement");
var valueToCheck = sheet.getRange("J13").getValue();
var rangeEdit = e.range.getA1Notation();
if(rangeEdit !== "J13")
{
MailApp.sendEmail("*****@gmail.com", "subject", "message");
}
}
当前脚本被设置为onChange事件触发器。
您需要将值存储在文档(电子表格)的“属性”中。文档属性只是 Google 文档所具有的一项存储功能。
因此,代码必须存储电子表格单元格中的原始值,并存储最后一封电子邮件的发送日期。
function myFunction(e) {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Statement");
var valueToCheck = sheet.getRange("J13").getValue();
var rangeEdit = e.range.getA1Notation();
if (didTheValueInCellChange(valueToCheck) === false) {return}; //The value didn't change, quit
if (rangeEdit === "J13") {
if (hasAnEmailBeenSentToday() === false) {
MailApp.sendEmail("*****@gmail.com", "subject", "message");
emailJustSent();
setOriginalValue(valueToCheck);
};
};
};
function emailJustSent() {
var docProps = PropertiesService.getDocumentProperties();
var todaysDate = new Date();//Use date constructor, then convert date object to a string
var scriptTimeZone = Session.getScriptTimeZone();
var formatedDate = Utilities.formatDate(todaysDate, scriptTimeZone, "MM/dd/YYYY");
docProps.setProperty(key, value)('dateSent');
};
function hasAnEmailBeenSentToday() {
var docProps = PropertiesService.getDocumentProperties();
var todaysDate = new Date();//Use date constructor, then convert date object to a string
var scriptTimeZone = Session.getScriptTimeZone();
var formatedDate = Utilities.formatDate(todaysDate, scriptTimeZone, "MM/dd/YYYY");
var dateWhenEmailWasSent = docProps.getProperty('dateSent');
if (dateWhenEmailWasSent === formatedDate) {
return true;
} else {
return false;
};
};
function didTheValueInCellChange(currentValue) {
var docProps = PropertiesService.getDocumentProperties();
var theLastValueSaved = docProps.getProperty('originalValue');
if (theLastValueSaved !== currentValue) {
return true;
} else {
return false;
};
};
function setOriginalValue(theValue) {
docProps.setProperty('originalValue', theValue);
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)