【问题标题】:How to compare current edited time vs. specified criteria time in an IF Statement in Google Apps Script/Sheets?如何在 Google Apps 脚本/表格中的 IF 语句中比较当前编辑时间与指定条件时间?
【发布时间】:2021-04-13 04:32:37
【问题描述】:

目标: 将当前日期或明天的日期插入到用户编辑的 K 列/行下的单元格中,具体取决于用户何时将 D 列下的单元格更改为“聊天 => 电子邮件”。如果可能的话,我希望日期的比较和输出基于 JST(日本标准时间)时区。

我的代码上下文: 在下面的代码中,我创建了一个名为“insertDeadlineDate()”的函数,它从另一个 .gs 文件接收 onEdit() 信息。在 insertDeadlineDate() 代码中,我尝试创建一个 if 语句来检查以下几点并执行结果。

  1. 如果用户将单元格值更改为“聊天 => 电子邮件”并且当前时间在上午 9:30 之间。和下午 2:00 (JST),然后将当天的日期插入到用户编辑的 K 列/行。
  2. 如果用户将单元格值更改为“聊天 => 电子邮件”并且当前时间在下午 2:00 之间。和下午 6:30 (JST),然后将当天的日期插入到用户编辑的 K 列/行。

仅供参考:我的 IF 语句为空白,因为我不知道该写什么。对不起……

function insertDeadlineDate(range, sheetName, row, column) {
  const statusSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Status');
  const editedRangeValue = range.getValue();
  
  // Column numbers for Status sheet
  const caseTypeColumnStatus = 4;

  if (sheetName == 'Status' && column == ticketTypeColumnStatus && row > 15 && editedRangeValue == 'Chat => Email') {
    var statusColumn = column + 3;
    if () { // If the current time is between 9:30 A.M. and 2:00 P.M. (JST), then TRUE. 
      // Insert today's date
      statusSheet.getRange(row, statusColumn).setValue(new Date());
    } else if () { // If the current time is between 2:00 P.M. and 6:30 P.M. (JST), then TRUE.
      // Insert tomorrow's date
      statusSheet.getRange(row, statusColumn).setValue();
    }
  }
}

样张截图:

【问题讨论】:

  • 只是为了澄清这个场景,如果当前时间在下午 6:30 (JST) 和上午 9:30 之间会发生什么 - 或者您的脚本永远不会出现这种场景?
  • 感谢您的提问!!因此,如果当前时间在下午 6:30 到上午 9:30 之间,那么我宁愿脚本什么也不做!

标签: google-apps-script google-sheets


【解决方案1】:

由于我没有传递任何参数,所以我做了一些调整,直接是onEdit

function onEdit(e) { 
  const sheetName = 'Status';
  const statusSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  const cell = statusSheet.getActiveCell();
  const row = cell.getRow();
  const column = cell.getColumn();
  const editedRangeValue = cell.getValue();

  // var currentDate = new Date(new Date().toLocaleString('en-US', { timeZone: 'Japan' }));

  // The above line of code translates your time into japan timezone but since 
  // sheets is converting your timezone automatically to the regional one, 
  // it adjusts an additional day when used. Be careful in using the above line.

  var currentDate = new Date();

  // Use the line below to test different time slots, we do not want to wait the 
  // exact time just to test this script.
  // currentDate.setHours(11, 30, 0 ,0);

  startDate = new Date(currentDate.getTime());
  startDate.setHours(9, 30, 0, 0);

  endDate1 = new Date(currentDate.getTime());
  endDate1.setHours(14, 0, 0, 0);

  endDate2 = new Date(currentDate.getTime());
  endDate2.setHours(18, 30, 0, 0);

  // Column numbers for Status sheet
  const caseTypeColumnStatus = 4;

  if (sheetName == 'Status' && column == caseTypeColumnStatus && row > 15 && editedRangeValue == 'Chat => Email') {
    var statusColumn = column + 3;
    if (startDate <= currentDate && endDate1 > currentDate) { // If the current time is between 9:30 A.M. and 2:00 P.M. (JST), then TRUE. 
      // Insert today's date
      statusSheet.getRange(row, statusColumn).setValue(currentDate);
    } else if (endDate1 <= currentDate && endDate2 > currentDate) { // If the current time is between 2:00 P.M. and 6:30 P.M. (JST), then TRUE.
      // Insert tomorrow's date
      var tomorrow = new Date(currentDate.setDate(currentDate.getDate() + 1));
      if(tomorrow.getDay() == 6){ 
        // if saturday, add 2 days 
        tomorrow.setDate(tomorrow.getDate() + 2);
      } 
      else if(tomorrow.getDay() == 0){
        // if sunday, add 1 day
        tomorrow.setDate(tomorrow.getDate() + 1);
      } 
      statusSheet.getRange(row, statusColumn).setValue(tomorrow);
    }
  }
}

【讨论】:

  • 非常感谢@nazia!这行得通!但是我有一个我没有意识到的问题。你能帮助如何增加工作日吗?因此,例如,如果用户在星期五下午 2:00 到下午 6:30 之间将值设置为“聊天 => 电子邮件”,我希望将日期设置为工作日并避免添加工作日在周末或银行假日。这可能吗?
  • 嗨@JohnT。 ,我们以哪些假期为基础?我可以向你保证,我们可以跳过周末,但我会在假期尝试
  • 更新了答案@JohnT.,你可以测试修改的部分吗?当您插入明天的日期时,它应该是最后一个条件。至于假期,请在另一个问题中发布,因为我仍然不确定如何接近假期并且可能需要一些时间。其他用户可能能够尽快帮助您发布问题。不要忘记添加google-apps-script 作为标签,这样我会更容易找到它
  • 嗨@NaziA!那是超级快。你是如何变得如此博学的......无论如何,它奏效了,非常感谢你!
  • 我希望有一种方法可以将问题分配给您。但我会确保用google-apps-script 标签提出问题。再次衷心感谢您快速而详细的支持!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多