我在编写代码时遇到了麻烦,我不确定丢失了什么。我之前已经编写过几次代码,但我绝不是专家。
我正在尝试在单元格值等于30时发送自动电子邮件提醒。我的代码成功运行,但根本不生成电子邮件。我在做什么错了?
我不确定还有什么尝试的方法,我已经搜索了如何实现此目标,但是对于实现此目标的确切方式我却没有任何意义。
function sendEmail(){
var sSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
var Dept = sSheet.getRange(1, 1, sSheet.getLastRow(), 1);
var PDName = sSheet.getRange(1, 3, sSheet.getLastRow(), 3);
var HRPname = sSheet.getRange(1, 26, sSheet.getLastRow(), 26);
var HRPemail = sSheet.getRange(1, 27, sSheet.getLastRow(), 27);
var AnniversaryDate = sSheet.getRange(1, 12, sSheet.getLastRow(), 12);
var DaysUntilAnni = sSheet.getRange(1, 13, sSheet.getLastRow(), 13);
if (DaysUntilAnni == 30){
var subject = "Upcoming Postdoc Anniversary: " + PDName + " - " + AnniversaryDate;
var message = "Hi " + HRPname + "," + "\n\n" + "The Postdoc appointment for "+PDName+" in "+Dept+" has an appointment end date and/or anniversary date coming up on "+AnniversaryDate+". Please review this postdoc appointment and" +
"\n\n" + "take necessary action to extend their appointment and/or update level change based on their anniversary date." +
"\n\n" + "Please update the BEST Postdoc Tracker no later than " +AnniversaryDate+ " once the actions have been completed."
"\n\n" + "You will continue to receive weekly reminders regarding this action until the appointment end date or anniversary date has been updated in the BEST Postdoc Tracker.";
MailApp.sendEmail(HRPemail, subject, message);
}
}
我的代码应每天运行,并且应将电子邮件发送给Google工作表中列出的适当人员。
答案 0 :(得分:1)
在此行var HRPemail = sSheet.getRange(1, 27, sSheet.getLastRow(), 27);
中,您定义了一个从第1行第27列开始的范围,它是sSheet.getLastRow()行,并且有27列,您实际上从未运行过getValue()或getValues()方法就可以了。因此,HRPemail中没有数据。它只是一个范围对象,它只是单元格的集合。
我怀疑您有一个包含多行数据的电子表格。对吧?
因此,在与您聊天之后,我相信您将在此脚本上获得更多的成功。
function sendEmail(){
var sSheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
var srg=sSheet.getRange(1,1,sSheet.getLastRow(),28);
var sData=srg.getValues();
for(var i=0;i<sData.length;i++) {
var Dept = sData[i][0];//columns begin at 1 arrays begin at zero
var PDName = sData[i][2];
var HRPname = sData[i][25];
var HRPemail = sData[i][2];
var AnniversaryDate = sData[i][11];
var DaysUntilAnni = sData[i][12];
var Sent=sData[i][27];
if (DaysUntilAnni == 30 && !Sent){
var subject = "Upcoming Postdoc Anniversary: " + PDName + " - " + AnniversaryDate;
var message = "Hi " + HRPname + "," + "\n\n" + "The Postdoc appointment for "+PDName+" in "+Dept+" has an appointment end date and/or anniversary date coming up on "+AnniversaryDate+". Please review this postdoc appointment and" +
"\n\n" + "take necessary action to extend their appointment and/or update level change based on their anniversary date." +
"\n\n" + "Please update the BEST Postdoc Tracker no later than " +AnniversaryDate+ " once the actions have been completed."
"\n\n" + "You will continue to receive weekly reminders regarding this action until the appointment end date or anniversary date has been updated in the BEST Postdoc Tracker.";
MailApp.sendEmail(HRPemail, subject, message);
//i+1 is the row number assuming no headers
sSheet.getRange(i+1,27).setValue('Sent');//if column 28 has data in it then an email will not be sent again on the next time through the loop.
}
}
}
我还没有调试它,但是它应该更接近您想要的。看一遍,问问题。如果您的电子表格中包含标题,那么我们需要考虑到该标题,让我知道。