使用Google脚本在电子表格中插入两天之间的结果计数天数 [英] Insert the result count number of days between two days in spreadsheet using google script
问题描述
如何将特定日期的天数传递给Cell。例如:
Column1:12/05/2013 18:00:00使用函数的实际天数New Date()Column2:脚本将插入天数(Today - Column1)= 4
脚本应该在第2列中插入从今天开始传递的天数。今天是16/05/2013 23:00,脚本会插入4.如果可能,请填写这个吗?
如果你能帮助我,我将不胜感激。
该函数由朋友传递,但我认为不起作用,因为日期不是以毫秒为单位,而单元格中的结果应该是天数
}
函数formatting2(event){
var sheet = SpreadsheetApp.getActiveSpreadsheet()。getActiveSheet(); //获取图纸
var columnF = sheet.getRange(2,1,sheet.getLastRow() - 1,1);
var updateage = sheet.getRange(2,2,sheet.getLastRow() - 1,1);
var fValues = columnF.getValues(); //获取值
var result = new Date();
updateage.setValue(result-fValues)
}
这是获得您想要的结果的一种可能方式,我将它编写为一个自定义函数,即您必须将它放在希望它出现在窗体< code => dayToToday()
这里是一些带有一些日志和注释的脚本,用于显示中间值。
函数dayToToday(x){
var sh = SpreadsheetApp.getActiveSheet();
Logger.log(sh.getActiveCell()。getRowIndex())
var refcell = sh.getRange(sh.getActiveCell()。getRowIndex(),1).getValue();; // get A列中的值以获取参考日期
var refTime = new Date(refcell);
var ref = refTime.setHours(0,0,0,0)/(24 * 3600000); //将小时,分钟,秒和毫秒设置为0(如有必要)并获取天数
var today = new Date();
var TD = today.setHours(0,0,0,0)/(24 * 3600000); //将小时,分钟,秒和毫秒设置为0(如有必要)并获取天数
var day = parseInt(TD-ref); //获取天数的差值(整数值)
返回日期; //返回单元格中的结果
}
编辑:
如果我理解了你的用例,我建议你放弃自定义函数aproach (我从来没有用过,因为我不太喜欢它)然后使用这个解决方案,它使用计时器触发器和/或菜单在一个批次中更新电子表格中的值。
(ps:抱歉没有现在太忙了: - )
{name:test function,functionName:toTrigger},];
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.addMenu(test menu,menuEntries); //
}
//创建一个定时器触发器,每15分钟调用一次toTrigger
函数toTrigger(){
var sh = SpreadsheetApp.getActiveSheet();
var data = sh.getRange(1,1,sh.getLastRow(),2).getValues();
for(var n = 0; n
data [n] [1] = dayToToday(data [n] [0])
}
}
sh.getRange(1,1,sh.getLastRow(),2).setValues(数据)
}
函数dayToToday(x){
var refcell = x ;; // //获取列A中的值获取参考日期
var refTime = new Date(refcell);
var ref = refTime.setHours(0,0,0,0)/(24 * 3600000); //将小时,分钟,秒和毫秒设置为0(如有必要)并获取天数
var today = new Date();
var TD = today.setHours(0,0,0,0)/(24 * 3600000); //将小时,分钟,秒和毫秒设置为0(如有必要)并获取天数
var day = parseInt(TD-ref); //获取天数的差值(整数值)
返回日期; //返回单元格中的结果
}
$ b $当使用
var data = sh.getRange(1,1,sh。)时,b 第二个EDIT :
getLangeRow(),2).getValues();
getRange中的数字是起始行,起始列,行数,列,所以你必须gat一个范围,包括你想要的列,然后使用data [n] [columnNumber-1]来获得相应的值。 (-1,因为数组从0开始,列从1开始计数(实际上,A是第一列;-))
如果你需要一个变量引用,那么让我知道,因为函数必须被修改为接受2个变量(a ref和另一个)。
最后修改
(我希望)我改变了一些细节...参见与J和F相对应的[9]和[5]索引b
$ b
function toTrigger(){
var sh = SpreadsheetApp.getActiveSheet();
var data = sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn())。getValues();
for(var n = 0; 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)
}
更新/编辑
结果为小时(请在评论中注明)
<$ p $函数hoursToToday(x){
var refcell = x ;; // //获取列A中的值以获取引用日期
var refTime = new Date(refcell);
var refhrs = refTime.getHours();
var ref = refTime.setHours(refhrs,0,0,0)/ 3600000; //将小时,分钟,秒和毫秒设置为0(如有必要)并获取天数
var today = new Date ();
var todayHrs = today.getHours()
var TD = today.setHours(todayHrs,0,0,0)/ 3600000; //将小时,分钟,秒和毫秒设置为0天数
var hrs = TD-ref; //获取以天为单位的差异(整数值)
return hrs; //返回单元格中的结果
}
How to put in a Cell the number of days pass from a specific date. For example:
Column1: 12/05/2013 18:00:00 Actual Days using Function New Date() Column2: The script will insert the numbers of days (Today - Column1) = 4
The script should be insert in the column 2 the number of days pass from today. Today is 16/05/2013 23:00, the script will insert 4. If possible due this?
I'll appreciate if you can help me.
That function was passed by friend but I think doesn't work because the date is not in milliseconds but the result in the cell should be number of days
}
function formatting2(event) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); // get the sheet
var columnF = sheet.getRange(2, 1, sheet.getLastRow()-1, 1);
var updateage = sheet.getRange(2, 2, sheet.getLastRow()-1, 1);
var fValues = columnF.getValues(); // get the values
var result = new Date();
updateage.setValue(result-fValues)
}
This is a possible way to get the result you want, I wrote it as a custom function, ie you have to put it in the the cell where you want it to appear in the form =dayToToday()
here is the script with a few logs and comments to show intermediate values.
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
}
EDIT : if I understand your use case I suggest you abandon the custom function aproach (that I never use since I don't like it so much) and go for this solution that uses a timer trigger and/or a menu to update values in your spreadsheet in one batch.
(ps : sorry for not having answer quickly enough... too busy these days :-)
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
}
second EDIT:
when using var data = sh.getRange(1,1,sh.getLastRow(),2).getValues();
the numbers in getRange are start row, start column, number of Rows, number of Columns so you'll have to gat a range that includes the columns you want and then use data [n][columnNumber-1] to get the corresponding value. (-1 because array start from 0 and columns count from 1 (A in fact but A is first column ;-)) if you need a variable reference then let me know because the function must be modified to accept 2 variables (a ref and another one).
LAST EDIT (I hope)
I changed some details... see [9] and [5] index corresponding to J and F
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)
}
update/edit
result in hours (following request in comments)
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
}
这篇关于使用Google脚本在电子表格中插入两天之间的结果计数天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!