Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 使用google脚本在电子表格中插入两天之间的结果计数天数_Google Apps Script - Fatal编程技术网

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

Google apps script 使用google脚本在电子表格中插入两天之间的结果计数天数,google-apps-script,Google Apps Script,如何在单元格中输入从特定日期起经过的天数。例如: Column1:12/05/2013 18:00:00使用函数New Date()的实际天数Column2:脚本将插入天数(今天-Column1)=4 脚本应在第2列中插入从今天起经过的天数。今天是2013年5月16日23:00,脚本将插入4。如果可能的话,原因是什么 如果你能帮助我,我将不胜感激 该函数是由friend传递的,但我认为不起作用,因为日期不是以毫秒为单位的,但单元格中的结果应该是天数 } function form

如何在单元格中输入从特定日期起经过的天数。例如:

Column1:12/05/2013 18:00:00使用函数New Date()的实际天数Column2:脚本将插入天数(今天-Column1)=4

脚本应在第2列中插入从今天起经过的天数。今天是2013年5月16日23:00,脚本将插入4。如果可能的话,原因是什么

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

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

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

这是获得所需结果的一种可能方法,我将其作为自定义函数编写,即您必须将其放入单元格中,以便它以
=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(我从来没有使用过,因为我不太喜欢它),转而使用这个解决方案,使用计时器触发器和/或菜单在一批中更新电子表格中的值

(附言:很抱歉没有及时得到答复……这些天太忙了:-)


更新/编辑

以小时为单位得出结果(根据评论中的请求)


你好,谢尔盖,关于你的剧本。我需要在第2列中插入的天数值。这张纸大约有50行或更多行。脚本应在每行的第2列中插入结果。我的意思是,如果我有50行,脚本应该为每一行执行。目前我无法在列中插入结果。我不知道为什么不工作。看看这个(制作一个副本以便能够编辑它)如果可能的话,脚本直接将解决方案插入单元格而不在列中写入任何函数?根据我上一篇评论,脚本运行良好,但第1列每天都会更改,有时我会更改10行其他所有行,结果不会更改。我的意思是,在我将公式=dayToToday()放在第1列的那一列中,当我更改第1列时,不会更新数字。我将感谢您的帮助。问题是当我修改第1列中的日期时,公式计数日(=dayToToday)不会用新日期更新:-(
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
}
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
}