我目前有一个Google电子表格,表示包含多列的待办事项列表。 其中2列如下:
所以我们最终得到类似的东西
BY WHEN DAYS TO ACT
20 August 2014 0
20 August 2014 0
20 August 2014 0
21 August 2014 1
21 August 2014 1
21 August 2014 1
21 August 2014 1
22 August 2014 2
26 August 2014 6
26 August 2014 6
26 August 2014 6
29 August 2014 9
11 September 2014 22
18 September 2014 29
18 September 2014 29
我在此电子表格中附加了一个应用程序脚本,该脚本根据剩余天数对行进行着色。该脚本可以在此消息的底部找到。脚本中的所有内容都运行良好,并且已经使用了好几个月。
我遇到的问题是,当我将电子表格一夜之间打开,然后在早上创建一个新条目时,DAYS TO ACT值对于旧条目不会改变,因为公式在表单中而不在代码中。
有没有办法告诉工作表重新计算或失败,是否有办法以编程方式更新DAYS TO ACT值? 如果是这样,怎么样?
//Sets the row color depending on the value in the "DAYS TO ACT" column.
function setRowColors() {
SpreadsheetApp.flush();
var sheet = SpreadsheetApp.getActiveSheet();
var range = SpreadsheetApp.getActiveSheet().getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn());
range.setFontWeight("bold");
range.setVerticalAlignment("top");
var whenColumnOffset = getColumnOffset("BY WHEN");
var actColumnOffset = getColumnOffset("DAYS TO ACT");
var noteColumnOffset = getColumnOffset("NOTES");
var priorityColumnOffset = getColumnOffset("PRIORITY");
range.sort([{column: 5, ascending: true}, {column: 6, ascending: true}]);
for (var i = range.getRow(); i < range.getLastRow(); i++) {
rowRange = range.offset(i - 2, 0, 1);
days_to_act = rowRange.offset(0, actColumnOffset).getValue();
by_when = rowRange.offset(0, whenColumnOffset).getValue();
notes = rowRange.offset(0, noteColumnOffset).getValue();
if (notes == "DONE") {
rowRange.setBackground("#0000FF");
rowRange.setFontColor("#FFFFFF");
rowRange.offset(0, actColumnOffset, 1, 1).setValue(9999);
rowRange.offset(0, actColumnOffset, 1, 1).setFontColor("#0000FF");
range.sort([{column: 5, ascending: true}, {column: 6, ascending: true}]);
continue;
}
if (notes == "IN PROGRESS") {
rowRange.setBackground("#000000");
rowRange.setFontColor("#FFFFFF");
rowRange.offset(0, actColumnOffset, 1, 1).setValue(9998);
rowRange.offset(0, actColumnOffset, 1, 1).setFontColor("#000000");
range.sort([{column: 5, ascending: true}, {column: 6, ascending: true}]);
continue;
}
if (days_to_act > 14) {
rowRange.setBackground("#00FF00");
} else if (days_to_act <= 14 && days_to_act > 7) {
rowRange.setBackground("#FFD700");
} else if (days_to_act <= 7 && days_to_act > 3) {
rowRange.setBackground("#FF8C00");
} else if (days_to_act <= 3 && days_to_act > 1) {
rowRange.setBackground("#FF4500");
} else if (days_to_act <= 1 && days_to_act >= -1 && by_when != "") {
rowRange.setBackground("#FF0000");
} else {
rowRange.setBackground("#808080");
}
}
range.sort([{column: 5, ascending: true}, {column: 6, ascending: true}]);
}
//Returns the offset value of a specific column
//(eg, if the 7th column is labeled "Status", this function returns 6)
function getColumnOffset(colName) {
lastColumn = SpreadsheetApp.getActiveSheet().getLastColumn();
var range = SpreadsheetApp.getActiveSheet().getRange(1,1,1,lastColumn);
for (var i = 0; i < range.getLastColumn(); i++) {
if (range.offset(0, i, 1, 1).getValue() == colName) {
return i;
}
}
}
function onOpen() {
setRowColors();
}
function onEdit() {
setRowColors();
}
我不会在日期更改时自动更新表单,只有在创建新条目或通过从我创建的菜单项调用新函数时才会失败。
此致 克鲁齐莱
答案 0 :(得分:1)
选择“剩余天数”并将此公式粘贴到字段中:
=DAYS360(today(),C2)
然后转到格式→数字→更多格式→自定义数字格式并选择没有小数的数字。
答案 1 :(得分:0)
只需在SpreadsheetApp.flush()
函数中添加onEdit()
,即可强制重新评估所有公式。
答案 2 :(得分:0)
这对你有用吗
创建一个日期对象,然后从代码本身计算days_to_act。
var Today = new Date()
var days_to_act
days_to_act = by_when - Today.
if (days_to_act > 14) {
rowRange.setBackground("#00FF00");
} else if (days_to_act <= 14 && days_to_act > 7) {
rowRange.setBackground("#FFD700");
} else if (days_to_act <= 7 && days_to_act > 3) {
rowRange.setBackground("#FF8C00");
} else if (days_to_act <= 3 && days_to_act > 1) {
rowRange.setBackground("#FF4500");
} else if (days_to_act <= 1 && days_to_act >= -1 && by_when != "") {
rowRange.setBackground("#FF0000");
} else {
rowRange.setBackground("#808080");
}