我无法按照自己的方式创建数组。我需要这个谷歌表脚本,但这不重要,它只是javscript。
我正在创建一个表示一周中的天数的数组,数组中的每个条目代表一天,工作日将整数推送给他们,但周末天数将空白值推入数组。 我遇到的困难是阵列的开始可能是一周中的任何一天。因此,如果开始日是星期五,并且我推送5个值,那么数组将看起来像这样
[22,,,22,22] //fri=22, sat="", sun="", mon=22, tue=22
在我的例子oj jsbin中,我可以让它适用于一个案例,但不是另一个案例。 我将完整代码放在jsbin链接
下面我希望有人可以告诉我一个更好的方法去做我想做的事情。或者帮助修复我糟糕的代码。
jsbin https://jsbin.com/poqumecafo/edit?js,console
var first = [, 500.0, 16.0, "Fri Jun 08 00:00:00 GMT-04:00 2018", 2.0, 24.0, "Sun Jun 03 00:00:00 GMT-04:00 2018", 0.0, "Tue Jun 19 00:00:00 GMT-04:00 2018"];
var second = [, 1200.0, 25.0, "Sun Jun 10 00:00:00 GMT-04:00 2018", 6.0, 17.0, "Fri May 25 00:00:00 GMT-04:00 2018", 5.0, "Wed Jul 18 00:00:00 GMT-04:00 2018"];
console.log('NOTE: day of week index - 6 = saturday, 0=Sunday, 1=mon, 2=tue, 3=wed, 4=thu, 5=fri');
console.log('-------------------------------------------');
var result1 = cleanArray(first);
console.log('result1 expected result = ,42,42,42,42,42,,,42,42,42,42,42,,,42');
console.log('day of week for result1 = ' + first[7])
console.log('result1= ' + result1);
var result2 = cleanArray(second);
console.log('result2 expected result = 31,,,31,31,31,31,31,,,31,31,31,31,31,,,31,31,31,31,31,,,31');
console.log('day of week for result2 = ' + second[7])
console.log('result2= ' + result2);
//found this function on the internet, takes 2 dates returns how many working days (minus weekend days)
function getBusinessDays(startDate, endDate) {
//Logger.log('startdate ' + startDate + 'enddate ' + 'endDate' );
var count = 0;
var curDate = new Date(startDate);
while (curDate <= new Date(endDate)) {
var dayOfWeek = curDate.getDay();
if(!((dayOfWeek == 6) || (dayOfWeek == 0)))
count++;
curDate.setDate(curDate.getDate() + 1);
}
return count;
}
function cleanArray(ed){
//each row is [, 500.0, 16.0, Thu Jun 07 00:00:00 GMT-04:00 2018, 2.0, 23.0, Sat Jun 02 00:00:00 GMT-04:00 2018, 6.0, Mon Jun 18 00:00:00 GMT-04:00 2018]
// which is [options, CubicFeet, installDays, dateOfFirstInstall, rowIndex, colstartIndex, newStartDate, newStartDateDayofWeekNumber, finishDate]
//I have to return an array of the data that will actually show on the spreadsheet
//returns something like[[22,22,22,,,22,22,22],[11,11,11,,,11]] notice the empty cells are weekends
//Logger.log('cleanarray incoming');
//Logger.log(ed);
var newCleanArr = []; //ready for new clean array
// calculate how many working days (subtract weekends from total work days)
var numWorkingDays = getBusinessDays(ed[6],ed[8]);
//calculate how many cubic feet per working days
var dailyCF = Math.round(ed[1]/numWorkingDays);
for (var i = 0; i < ed[2]; i++) {
//Logger.log('i='+i+' ed[7]=' + ed[7]);
if(i<1 && ed[7] == 0){
//Logger.log('Sunday first');
newCleanArr.push("");
} else if( i<7 && i % 6 ===0 ){ //if sat or sun then make blank entry
newCleanArr.push("");
//Logger.log('saturday mod6=0');
} else if (i<8 && i % 7 ===0 ){
//Logger.log('sunday mod7=0');
newCleanArr.push("");
} else if( i>7 && i % 6 ===1 ){ //if sat or sun then make blank entry
newCleanArr.push("");
//Logger.log('saturday mod6=0');
} else if( i>8 && i % 6 ===2 ){ //if sat or sun then make blank entry
newCleanArr.push("");
//Logger.log('sunday mod6=0');
} else {
newCleanArr.push(dailyCF);
}
}
//Logger.log('cleanArr');
//Logger.log(newCleanArr);
//Logger.log('numWorkingDays');
//Logger.log(numWorkingDays);
//Logger.log('dailyCF');
//Logger.log(dailyCF);
return newCleanArr;
}