使用Google脚本在电子表格中插入两天之间的结果计数天数

时间:2013-05-17 03:34:28

标签: google-apps-script

如何在特定日期过入天数。例如:

Column1:12/05/2013 18:00:00使用函数的实际天数新日期()第2列:脚本将插入天数(今天 - 第1列)= 4

该脚本应在第2列中插入从今天开始的天数。今天是16/05/2013 23:00,脚本将插入4.如果可能的话,这个?

如果你能帮助我,我将不胜感激。

该功能是由朋友传递的,但我认为不起作用,因为日期不是以毫秒为单位,但单元格中的结果应该是天数

    }
    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)
    }  

1 个答案:

答案 0 :(得分:1)

这是获得所需结果的一种可能方式,我将其作为自定义函数编写,即您必须将其放在您希望以=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
}

编辑: 如果我理解你的用例,我建议你放弃自定义函数aproach (我从不使用,因为我不太喜欢它)并选择使用定时器触发器和/或菜单可以一次更新电子表格中的值。

(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和另一个变量)。


最后编辑(我希望)

我更改了一些细节...参见[9]和[5]对应于J和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) 
}

<强>更新/修改

结果为小时(在评论中提出请求)

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
}