我创建了一个脚本,通过Birthday Reminder向特定人发送电子邮件。这个用到前天工作。我不知道为什么我会收到无法找到函数getMonth的错误,任何人都可以告诉错误在哪里
function emailAlert() {
// Short circuit if email notice is set to "No". This causes an error and the script stops.
if (turnOnEmailNotice.toLowerCase() == "no")
{
Logger.log("The Email Notification is NOT turned ON. System will Exit.");
exit
}
//Get the total number of filled row in the sheet.
var currentRowAT = 2;
var currentCellValueAT = "start";
while (currentCellValueAT != ""){
if (currentCellValueAT = birthdaysSheet.getRange("G" + currentRowAT).getValue() != ""){
currentRowAT = currentRowAT +1;
}
}
var birthdaysSheetLastRow = currentRowAT - 1;
// Get today's Date (with Month, Date and Year)
var today = new Date();
var todayMth = today.getMonth()+1;
var todayDate = today.getDate();
var todayYear = today.getFullYear();
// Check sheet cell for match to alertDate, k is the current row number being checked. Starting at 2 as the row #1 is for the headers
for (k=2; k < birthdaysSheetLastRow + 1; k++)
{
var targetBday = new Date();
targetBday = birthdaysSheet.getRange("P" + k).getValue();
// If Birthday is not speicified, continue with the next row
if (targetBday == ""){continue};
var unadjTargetBday = new Date();
var unadjTargetBdayMth = targetBday.getMonth()+1;
var unadjTargetBdayDate = targetBday.getDate();
var unadjTargetBdayYear = targetBday.getFullYear();
var unadjTargetBday = targetBday;
targetBday.setDate(targetBday.getDate()-daysInAdvance); // Calculating how many days in advance you want to trigger the notification. This is set in Settings Tab.
var targetBdayMth = targetBday.getMonth()+1;
var targetBdayDate = targetBday.getDate();
if (targetBdayMth + " " + targetBdayDate == todayMth + " " + todayDate)
{
var targetBirthDateYearsOld = (today.getYear() - unadjTargetBday.getYear())-1900;
prepareAndSendEmail(k, targetBirthDateYearsOld);
}
}
}
答案 0 :(得分:3)
getValue将返回类型字符串/日期/数字,具体取决于电子表格中的单元格类型(请参阅菜单格式 - &gt;数字)。可以肯定的是,只需始终转换为日期类型。这是转换它的正确方法:
var targetBday = new Date(birthdaysSheet.getRange("P" + k).getValue());
答案 1 :(得分:2)
检查单元格值是Google表格的有效日期。
来自https://developers.google.com/apps-script/reference/spreadsheet/range#getvalue(强调我的)
的getValue()
返回范围中左上角单元格的值。价值可能是 类型为
Number
,Boolean
,Date
或String
,具体取决于 细胞即可。空单元格将返回一个空字符串。
为避免此类问题,您可以包含某种数据验证。您可以使用内置功能,例如条件格式,数据验证或类似编辑脚本以及try / catch或emailAlert脚本中的冗余验证。