Last active
January 11, 2020 09:48
-
-
Save PolarBearGG/85e869057e47c8348fed7e962c92ef93 to your computer and use it in GitHub Desktop.
Get month if we know only Week number and year + include workdays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var DAY_MILLIS = 24 * 60 * 60 * 1000; | |
function main(data_start, data_end) { | |
//var ss = SpreadsheetApp.openById("Table_ID"); | |
//var name = ss.getName(); | |
//var sheet = ss.getSheetByName('Sheet1'); // or whatever is the name of the sheet | |
//var start = sheet.getRange(2,4); | |
//var data_start = start.getValue(); | |
//var end = sheet.getRange(2,5); | |
//var data_end = end.getValue(); | |
var a = data_start; | |
var b = data_end; | |
var dates = createDateSpan(a, b); | |
var d = new Date(); | |
var startDate = data_start; | |
var endDate = data_end; | |
var n = d.getDay(); | |
var tempDate; | |
var arr1 = []; | |
for (tempDate = startDate; tempDate < endDate; tempDate.setDate(tempDate.getDate() + 1)) { | |
if (tempDate.getDay() == 1 || tempDate.getDay() == 2 || tempDate.getDay() == 3 || tempDate.getDay() == 4 || tempDate.getDay() == 5) { | |
arr1.push(tempDate.getMonth() + 1); | |
var massss = arr1 | |
//Logger.log(arr1); | |
} | |
} | |
var final = searchMostCommonNumber(massss); | |
Logger.log(final); | |
//SpreadsheetApp.getActiveSheet().getRange('A2').setValue(final); | |
return final; | |
} | |
function createDateSpan(startDate, endDate) { | |
if (startDate.getTime() > endDate.getTime()) { | |
throw Error('Start is later than end'); | |
} | |
var dates = []; | |
var curDate = new Date(startDate.getTime()); | |
while (!dateCompare(curDate, endDate)) { | |
dates.push(curDate); | |
curDate = new Date(curDate.getTime() + DAY_MILLIS); | |
} | |
dates.push(endDate); | |
return dates; | |
} | |
function dateCompare(a, b) { | |
return a.getFullYear() === b.getFullYear() && | |
a.getMonth() === b.getMonth() && | |
a.getDate() === b.getDate(); | |
} | |
function searchMostCommonNumber(arr) { | |
if (arr === void 0) { | |
arr = []; | |
} | |
var current = 0; | |
var max = 0; | |
var mostCommonNumber = 0; | |
var i; | |
for (i = 0; i < arr.length - 1; i++) { | |
var current_1 = 1; | |
var j = void 0; | |
for (j = i + 1; j < arr.length; j++) { | |
if (arr[i] === arr[j]) { | |
current_1++; | |
} | |
} | |
if (current_1 > max) { | |
max = current_1; | |
mostCommonNumber = arr[i]; | |
} | |
} | |
return mostCommonNumber; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment