【发布时间】:2022-01-01 21:57:05
【问题描述】:
我能够清除程序中的backlog 状态,并将更改推送到segment = data [x] 数组,但我无法在var range = sheet.getRange("E2:J10"); 引用的单元格中获取这些更改。我的问题特别是在 for 循环的第二个 if 语句中。
// Description:
//The task will be available again after 14 days
//First the program will scan `Task`s to detect backlog tasks
//Then for each task the program will take the current date `cDate` - `dateOfTask` =`diff` (inDays)
//If days past is greater than 14 days, then the task will be set to an empty cell `Task`.setblank
//Once the cell is empty the task will be available to the algo task manager for prioritization
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("E2:J10"); //debugged // test task on line 9
// // Fetch values for each row in the Range.
//const d = dRange.getValues();
var cDate = range.getCell(1,6);
console.log(cDate.getValue());
var data = range.getValues();
console.log(data);
//forloop
for (x=0; x<data.length; x++) {
var segment = data[x];
if (segment[1] == "Backlog"){
var now = new Date();
var dateOfTask = new Date(segment[5]);
var diff = now - dateOfTask;
var timeValue = Math.floor(diff / 1000 / 60 / 60 / 24);
//console.log("Difference in milliseconds: ", diff); // 11140829739
console.log("Difference in days: ", timeValue)
//var timeValue = DAYS(cDate, segment[5]);
if(timeValue > 14){
var refreashBacklog = segment.splice(1,1,"clear contents");
range.setValue(refreashBacklog.segment);
Logger.log(segment);
//ask for help on stackOverflow
}
else{
continue
}
}
else{
continue // remember to set a stop at a certain row so it doesn't go to 999
}
}
}
【问题讨论】:
-
下面是另一个版本的代码,它在嵌套数组的预期位置返回“清除内容”。 ` if(timeValue > 14){ segment.splice(1,1,"here");控制台.log(段); } `
-
你不能像这样减去 Date() 对象
var diff = now - dateOfTask;你可以减去 valueOf() 和 getTime() 但请注意这些数字是自过去日期以来的毫秒数。 -
我认为,一旦您开始使用返回毫秒的方法,您将获得更大的成功。
-
这个等式似乎在我的控制台中有效。 ` 天数差异:272` 我使用的日期是 2021 年 4 月 4 日。
-
这是我偶尔使用的一个小函数:
function DiffInDays(Day1,Day2) { if(Day1 && Day2 && (Object.prototype.toString.call(Day1) === '[object Date]') && (Object.prototype.toString.call(Day2) === '[object Date]')) { var day=86400000; var t1=new Date(Day1).valueOf(); var t2=new Date(Day2).valueOf(); var d=Math.abs(t2-t1); var days=Math.floor(d/day); //Logger.log(days); return days; } else { throw 'Invalid Inputs'; } }
标签: javascript arrays for-loop google-apps-script google-sheets