What I am trying to do here is this - I want to give index to only the workdays in each week.
So, if in a week, Monday and Wednesday are holidays, then Tuesday should get 1, Thursday should get 2, Friday should get the index 3. Otherwise, in a normal week without any holidays, Monday should get 1, Tuesday 2, Wednesday 3, and so on ...
Here is the code I have written (I haven't coded in years now, so please pardon the crude approach)
Sheet
'Holidays'contains a list of holidays in the column B starting from row 2Variable
dateis the date for which I want to find out the index forVariable
dayOfTheWeekis the number of day of 'date' counted from last Sunday, so ifdateis a Monday,dayOfTheWeekis 1; ifdateis Tuesday,dayOfTheWeekis 2, and so on ...
function indexOfWorkdayOfTheWeek (date, dayOfTheWeek, lastSundayDate)
{
var activeSheet = SpreadsheetApp.getActiveSpreadsheet();
var activeCell = activeSheet.getActiveRange();
var activeRow = activeCell.getRowIndex();
var activeColumn = activeCell.getColumn();
var count = 1;
for (var j = 1; j < dayOfTheWeek; j++)
{
var date2 = lastSundayDate.valueOf() + j*86400;
Logger.log('Date ' + j + ' is:' + date2);
Logger.log('Last Sunday is:' + lastSundayDate);
if (holidayOrNot(date2) == true)
{
}
else
{
count = count + 1;
}
}
return count;
}
function holidayOrNot(date2)
{
var holidaysSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Holidays');
var listOfHolidays = holidaysSheet.getSheetValues(2, 2, 95, 1);
var isDateMatch = false;
for (var k = 0; k < 90; k++)
{
if (date2 == listOfHolidays[k].valueOf())
{
isDateMatch = true;
break;
}
else
{
continue;
}
}
return isDateMatch;
}
I think the problem is two-fold here:
The
date2calculation isn't working for some reason (var date2 = lastSundayDate.valueOf() + j*86400;)The function
holidayOrNotis returning false, no matter what, even if it encounters a holiday ... the conditiondate2 == listOfHolidays[k]isn't working for some reason...
Help would be appreciated!
lastSundayDatedefined? 2. Looks like the rest of your code will break iflastSundayDateis not defined properly. Well, at least all the points that you talk about in your question... My suggestion is to show all of your code that encompasses the definitions of all your variables, and perhaps post the result of the Execution Transcript from the Script Editor, or an output of detailed Logging.