我正在尝试创建一个根据时间表发送电子邮件的程序。
如果电子邮件设置为每日,则会每天运行。如果每周一次等等。
我一直在“var mDaily =”行上遇到语法错误。我有太多&&和||?这个程序是否会在周一至周五和预定时间正确计算电子邮件的时间?
谢谢!只编了几个月!!!
function startCustomTrigger()
{
ScriptApp.newTrigger("recurringEmails").timeBased().everyMinutes(60).create();
}
function recurringEmails() {
//http://stackoverflow.com/questions/28565372/time-based-google-script-should-run-between-11am-to-2pm
function recurPostLoadsSchedule() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheetByName("Contacts");
//var start = dataSheet.getRange("H2").getValue();
//var end = dataSheet.getRange("H3").getValue();
var dataRange = dataSheet.getRange("F4:N");
var info = dataRange.getValues();
for (var i = 0; i < dataSheet.getLastRow(); i++){
var date = new Date();
var day = date.getDay();
var hrs = date.getHours();
var dom = date.getdate();
if ((day >= 1) && (day <= 5) && (hrs >= 6) && (hrs <= 17 )) {
var mDaily = if ((info[i][9] == "Email Multiple Daily") && (day >= 1) && (day <= 5) && (hrs >= 7) && (hrs <= 8 )
|| (hrs >= 10) && (hrs <= 11 ) || (hrs >= 14) && (hrs <= 15)){
askForLoads() //this is actually the email program for the specific
};
var daily = if ((info[i][9] == "Email Daily") && (day >= 1) && (day <= 5) && (hrs >= 8) && (hrs <= 9)){
askForLoads()
};
var mWeekly = if((info[i][9] == "Email Multiple Weekly") && (day == 1) || (day == 3) || (day == 4) && (hrs >= 9) && (hrs <= 10)){
askForLoads()
};
var weekly = if((info[i][9] == "Email Weekly") && (dom == 1) || (dom == 5) || (dom == 9) || (dom == 13)|| (dom == 17)
|| (dom == 21) || (dom == 25) || (dom == 29) && (hrs >= 8) && (hrs <= 9)){ //1-2 per week
askForLoads()
};
var monthly = if((info[i][9] == "Email Monthly") && (dom == 1) || (dom == 11) || (dom == 21) || (dom == 31) && (hrs >= 10) && (hrs <= 11 )){ //1-3 per month
askForLoads()
};
};
};
};
};
答案 0 :(得分:1)
var mDaily = if ((info[i][9] == "Email Multiple Daily") && (day >= 1) && (day <= 5) && (hrs >= 7) && (hrs <= 8 )
|| (hrs >= 10) && (hrs <= 11 ) || (hrs >= 14) && (hrs <= 15)){
askForLoads() //this is actually the email program for the specific
};
if
语句不返回值,因此将其分配给mDaily
没有意义。您的意思是在var mDaily = askForLoads()
声明中声明if
吗?
从上下文来看,mDaily
和daily
的责任并不是很清楚。
答案 1 :(得分:1)
你不能在var声明中使用if。
您有两种选择:
1)您可以在if语句中移动变量声明:
if ((info[i][9] == "Email Multiple Daily")&& (day >= 1)&& (day <= 5) && (hrs >= 7) && (hrs <= 8) || (hrs >= 10) && (hrs <= 11 ) || (hrs >= 14) && (hrs <= 15)) { var mDaily = askForLoads(); };
2)或者您使用简化处理程序(condition) ? true : false;
var mDaily = ((info[i][9] == "Email Multiple Daily")&& (day >= 1)&& (day <= 5) && (hrs >= 7) && (hrs <= 8) || (hrs >= 10) && (hrs <= 11 ) || (hrs >= 14) && (hrs <= 15)) ? askForLoads() : null;
PS:另外,我认为你的&amp;&amp;和||为了清晰起见,条件需要更多的括号,顺便说一句。