Having an issue with this code, running on google scripts. It runs fine without any errors but will not mark the column with the overdue text. It is comparing two dates, in the end I want it to check the current date then if the inv_date is exactly 7 days before execute the if statement. Any help would be appreciated.
function onOpen()
{
var ui = SpreadsheetApp.getUi();
ui.createMenu('Invoice')
.addItem('Mark Overdue', 'doOverdueCheck')
.addToUi();
}
//does a check and inputs overdue if past date
{
var ui = SpreadsheetApp.getUi();
ui.createMenu('Invoice')
.addItem('Mark Overdue', 'doOverdueCheck')
.addToUi();
}
function doOverdueCheck()
{
var sheet = SpreadsheetApp.getActiveSheet();
var data_range = sheet.getDataRange();
var last_row = data_range.getLastRow();
sheet.getRange('E:E').clearContent();
var today = new Date();
today.setHours(0, 0, 0, 0);
today.getTime()-7*(24*3600*1000);
for (var r = 2; r <= last_row; r++) {
var inv_date = data_range.getCell(r, 4).getValue();
if (today.getTime() == inv_date.getTime()) {
sheet.getRange(r, 5).setValue("overdue");
}
}
}
function getOverDueInfo(row)
{
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var values = sheet.getRange(row,1,row,6).getValues();
var rec = values[0];
var overdue =
{
first_name:rec[0],
last_name:rec[1],
email:rec[2],
due_date:rec[3],
module_test:rec[5],
};
overdue.name = overdue.first_name +' '+ overdue.last_name;
overdue.date_str = sheet.getRange(row,4).getDisplayValue();
overdue.module = overdue.module_test;
var due_date = new Date(overdue.due_date);
due_date.setHours(0,0,0,0);
var today = new Date();
today.setHours(0,0,0,0);
var difference_ms = Math.abs(today.getTime() - due_date.getTime() );
overdue.num_days = Math.round(difference_ms/(24*60*60*1000) );
return overdue;
}
//Send email Function
function sendEmail(row)
{
var overdue = getOverDueInfo(row);
var templ = HtmlService
.createTemplateFromFile('client-email');
templ.overdue = overdue;
var message = templ.evaluate().getContent();
MailApp.sendEmail({
to: overdue.email,
subject: "Your Trial Has Ended",
htmlBody: message
});
}
//End function
//Start send Overdue emails
function sendOverdueEmails()
{
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var data_range = sheet.getDataRange();
var last_row = data_range.getLastRow();
var today= new Date();
today.setHours(0,0,0,0);
for(var r=2;r<=last_row;r++)
{
var inv_date = data_range.getCell(r,4).getValue();
inv_date.setHours(0,0,0,0);
if(today > inv_date)
{
sendEmail(r);
}
}
}
//End send overdue emails
//Function onOpen
{
var ui = SpreadsheetApp.getUi();
ui.createMenu('Invoice')
.addItem('mark Overdue', 'doOverdueCheck')
.addItem('send Emails', 'sendOverdueEmails')
.addToUi();
}
//End onOpen
getTimehas no()? This can be correct, but can also be a mistake.==? All in all, as a general rule, if an if request based on comparison fails I'd try to read the comparison parameters.