Reputation: 53
I'm trying to create a program that sends emails based on a schedule.
If the email is set to daily it will run daily. If weekly weekly etc.
I keep getting syntax error on the "var mDaily =" line. Do I have too many && and ||? Will this program properly time the emails to be Monday - Friday and at the times scheduled?
Thanks!!! Only been coding a few months!!!
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()
};
};
};
};
};
Upvotes: 0
Views: 55
Reputation: 1248
You cannot use if on a var declaration.
You have two options:
1) Either you move the variable declaration inside the if statement:
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) Or you use the simplified handler (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: As an aside note, I think your && and || conditions needs more parenthesis for clarity, btw.
Upvotes: 1
Reputation:
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
statements don't return values, so it doesn't make sense to assign it to mDaily
. Do you mean to declare var mDaily = askForLoads()
inside the if
statement?
From context it's not really clear what the responsibilities of mDaily
and daily
are.
Upvotes: 1