Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.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 Apps Script_Google Sheets - Fatal编程技术网

Google apps script 检查谷歌电子表格中的单元格更新

Google apps script 检查谷歌电子表格中的单元格更新,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我正在制作一个由多人编辑的谷歌电子表格。这是为了跟踪不同的成员统计数据,因此它们每个只更新一行。我试图做的是检查行中的一个单元格何时更新,并根据更新日期更改该行中另一个单元格的背景 function onEdit() { var s = SpreadsheetApp.getActiveSheet(); if( s.getName() == "HT ~ Stats" ) { //checks that we're on the correct sheet var r = s.getAct

我正在制作一个由多人编辑的谷歌电子表格。这是为了跟踪不同的成员统计数据,因此它们每个只更新一行。我试图做的是检查行中的一个单元格何时更新,并根据更新日期更改该行中另一个单元格的背景

function onEdit() {
 var s = SpreadsheetApp.getActiveSheet();
 if( s.getName() == "HT ~ Stats" ) { //checks that we're on the correct sheet
   var r = s.getActiveCell();
   if( r.getColumn() == 7 ) { //checks for the correct column
     var dateCell = r.offset(0,21);
     //var date = new Date()
     var date = Utilities.formatDate(new Date(), "GMT", "MM-dd-yyyy")

     dateCell.setValue(date); //sets column AB of selected row to the date of the update

 };
}
就在这一点上,我被卡住了。在这一点之后,我希望将var日期的值与当前日期进行比较。一旦进行比较,如果结果在当前日期的3天内,我希望所选行中的第一个单元格的背景更改为绿色


这是我第一次用谷歌脚本做任何事情,所以我相信有任何更简单或更好的方法可以做到这一点。感谢您的帮助:)

我还没有测试下面的内容,但这可能会让您走上正轨。它基本上与上面的操作相同,但在第二次编辑时,它会获取原始日期,将其转换为整数,获取新的日期整数,计算出差值,如果差值大于3天(以毫秒为单位),则设置第一个单元格的背景色

它可能需要一些调整,但我希望它有帮助

 function onEdit() {
  //G ets current spreadsheet
  var s = SpreadsheetApp.getActiveSheet();
  // If on the right spreadsheet
  if( s.getName() == "HT ~ Stats" ) { 
    // Gets the active cell
    var r = s.getActiveCell();
    // If on the right column
    if( r.getColumn() == 7 ) {
      // Sets the AB column
      var dateCell = r.offset(0,21);
      // Retrieves the currentDate
      var currentDate = dateCell.getValue().getTime();
      // Creates a new date
      var date = new Date();
      // Adds the date to the AB cell
      dateCell.setValue(date);
      // Creates a date integer
      var dateInt = date.getTime();
      // Works out the difference of the current date and new date
      var difference = dateInt - currentDate;
      // Creates a 3 day integer
      var threeDays = 259200000;
      // If the difference is greater than 3 days. 
      if(difference >= threeDays)
      {
        //Set the first cell background colour.
        r.offset(0,-6).setBackgroundColor("#D3E375");
      }
    }
  }
}

它起作用了。非常感谢。起初我对它有个问题,但后来我意识到它必须先在AB单元中有一个日期。因为不是所有的单元格都已经有日期了,所以没有正确更新。现在我想让它自动检查,并相应地更新,而不是依赖于正在更新的单元格。我计划制作一个“聚光灯”系统。更新后3到几天,它是绿色的,更新后4到6天,它变成黄色,之后的任何东西都变成红色。按原样,它将始终是绿色的,因为它只在他们做出更改时更新。很高兴它有所帮助。如果您想要不断检查更新的值,我建议创建一个单独的函数,针对一个新的date对象遍历date列中的所有单元格。一旦您成功地在手动运行中使用了此功能,您可以设置['triggers'](),以特定的时间间隔运行特定的功能。。。。。另外,如果答案有帮助,别忘了用勾号接受:)是否可以用onOpen功能设置它?如果是这样的话,最好的方法是什么?为new Date()设置一个变量,然后将每个AB列与之进行比较?我是否必须为每个AB列设置一个新的var,或者是否有方法从指定的单元格中提取数据?如果这些事情很简单,我很抱歉,但我以前从未真正做过这样的事情。