这是获得所需结果的一种可能方式,我将其编写为自定义函数,即您必须将其放在您希望它出现在表单中的单元格中=dayToToday()
这是带有一些日志和注释的脚本,用于显示中间值。
function dayToToday(x){
var sh = SpreadsheetApp.getActiveSheet();
Logger.log(sh.getActiveCell().getRowIndex())
var refcell = sh.getRange(sh.getActiveCell().getRowIndex(),1).getValue();;// get value in column A to get the reference date
var refTime = new Date(refcell);
var ref = refTime.setHours(0,0,0,0)/(24*3600000);// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
var today = new Date();
var TD = today.setHours(0,0,0,0)/(24*3600000);// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
var day = parseInt(TD-ref);// get the difference in days (integer value )
return day ; // return result that will be in cell
}
编辑:
如果我了解您的用例,我建议您放弃自定义函数方法(我从不使用它,因为我不太喜欢它)并选择使用计时器触发器和/或菜单来更新值的解决方案一批电子表格。
(ps:很抱歉没有足够快的回答......这些天太忙了:-)
function onOpen() {
var menuEntries = [ {name: "test function", functionName: "toTrigger"},
];
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.addMenu("test menu",menuEntries);//
}
// create a timer trigger that will call "toTrigger" every 15 minutes
function toTrigger(){
var sh = SpreadsheetApp.getActiveSheet();
var data = sh.getRange(1,1,sh.getLastRow(),2).getValues();
for(var n=0;n<data.length;++n){
if(typeof(data[n][0])=='object'){
data[n][1]=dayToToday(data[n][0])
}
}
sh.getRange(1,1,sh.getLastRow(),2).setValues(data)
}
function dayToToday(x){
var refcell = x;;// get value in column A to get the reference date
var refTime = new Date(refcell);
var ref = refTime.setHours(0,0,0,0)/(24*3600000);// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
var today = new Date();
var TD = today.setHours(0,0,0,0)/(24*3600000);// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
var day = parseInt(TD-ref);// get the difference in days (integer value )
return day ; // return result that will be in cell
}
第二次编辑:
使用时var data = sh.getRange(1,1,sh.getLastRow(),2).getValues();
getRange 中的数字是起始行、起始列、行数、列数,因此您必须选择一个包含所需列的范围,然后使用 data [n][columnNumber-1] 获取相应的值. (-1 因为数组从 0 开始,列从 1 开始计数(实际上是 A,但 A 是第一列 ;-))如果您需要变量引用,请告诉我,因为必须修改函数以接受 2 个变量(一个 ref 和另一个)。
最后编辑(我希望)
我更改了一些细节...参见对应于 J 和 F 的 [9] 和 [5] 索引
function toTrigger(){
var sh = SpreadsheetApp.getActiveSheet();
var data = sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
for(var n=0;n<data.length;++n){
if(typeof(data[n][9])=='object'){
data[n][5]=dayToToday(data[n][9])
}
}
sh.getRange(1,1,data.length,data[0].length).setValues(data)
}
更新/编辑
以小时为单位(根据评论中的要求)
function hoursToToday(x){
var refcell = x;;// get value in column A to get the reference date
var refTime = new Date(refcell);
var refhrs = refTime.getHours();
var ref = refTime.setHours(refhrs,0,0,0)/3600000;// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
var today = new Date();
var todayHrs = today.getHours()
var TD = today.setHours(todayHrs,0,0,0)/3600000;// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
var hrs = TD-ref;// get the difference in days (integer value )
return hrs ; // return result that will be in cell
}