当前位置: 首页 > 知识库问答 >
问题:

使用应用程序脚本比较Google工作表中的日期

岑元徽
2023-03-14

我有一个GoogleApps电子表格,其中一列包含日期和时间。这张纸是由几张纸合并而成的。在某些行上,该值是一个日期(即typeof sheet.getRange(i,2).getValue()==“object”,带有日期的方法)。其他时间,此单元格是一个数字(即typeof sheet.getRange(i,2).getValue()=“number”),但单元格格式将单元格显示为一个日期。

例如,日期为2015年5月16日星期六上午9:30

如果typeof firstDate==“number”,则该值为42140.395833333336

如果typeof firstDate==“object”,则“valueOf”为1431783000000

此函数的目的是在日期发生变化时添加边框,以便每天显示在单独的单元格组中。当前,此函数在日期和数字之间的数据类型发生变化时添加额外的边框。

/**
 * A special function that runs when the spreadsheet is open, used to add a
 * custom menu to the spreadsheet.
 */
function onOpen() {
  var spreadsheet = SpreadsheetApp.getActive();
  var menuItems = [
    {name: 'Add Borders', functionName: 'Add_Borders'}
  ];
  spreadsheet.addMenu('Scheduling', menuItems);
}

/**
 * Retrieves all the rows in the active spreadsheet that contain data and logs the
 * values for each row.
 * For more information on using the Spreadsheet API, see
 * https://developers.google.com/apps-script/service_spreadsheet
 */
function Add_Borders() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var numRows = sheet.getLastRow();
  var cols = sheet.getLastColumn();
  var firstDate=new Date();
  var secondDate = new Date();

//Logger.log(sheet.getParent().getName() + " ! " + sheet.getName());

  for (var i = 2; i <= numRows - 1; i++) {
    firstDate = sheet.getRange(i,2).getValue();

    Logger.log("row " + i + " Date type is " + typeof firstDate)
    if (typeof firstDate == "number"){
      Logger.Log ("row " + i + " number value = " + firstDate)
    }
    else {
      Logger.Log ("row " + i + " date value = " + firstDate.valueOf())
      Logger.Log ("row " + i + " numeric value = " + firstDate)
    }


    firstDate = (typeof firstDate == "number") ? new Date(firstDate * 86400) : firstDate
    secondDate = sheet.getRange(i+1,2).getValue();
    secondDate = (typeof secondDate == "number") ? new Date(secondDate * 86400) : secondDate
 // Logger.log("row " + i + " first date = " + firstDate + " firstDate.getDate() = " + firstDate.getDate() + "; firstDate.getMonth() = " + firstDate.getMonth())
 // Logger.log("row " + i + " second Date = " + secondDate + " secondDate.getDate() = " + secondDate.getDate() + "; secondDate.getMonth() = " + secondDate.getMonth())
    if (firstDate.getDate() != secondDate.getDate() || firstDate.getMonth() != secondDate.getMonth()){
      sheet.getRange(i, 1, 1, cols).setBorder(null, null, true, null, null, null);
    }
    else {
      sheet.getRange(i, 1, 1, cols).setBorder(null, null, false, null, null, null);
    }
  }
};

共有1个答案

凤经武
2023-03-14

经过一些实验后,我最终给出了一些定义:对于数字,值是自1899年12月31日起在当地时区的天数;日期。getTime()返回自UTC 1970年1月1日以来的毫秒数。

因此,以下函数强制值:

function FixDate(dte){

  // If dte is a number then it contains a date in the form of the number of days since
  // 00:00 on 12/31/1899 in the current time zone

  if (typeof dte == "number"){
     return dte
  }

  // If dte is a date object then the getTime property contains the date as the number of
  // milliseconds since 00:00 on January 1, 1970 in GMT.  This value is converted to be
  // compatible with the other format

  return (dte.getTime() / 86400000) + 25569 - dte.getTimezoneOffset() / (60 * 24)
}
 类似资料:
  • 我知道复选框是Google Sheets中一个相对较新的功能,所以我正试图找到一种在单元格中自动创建复选框的方法。 到目前为止,我还没有在谷歌应用程序脚本文档中找到与此相关的参考资料。 目前我正在手动操作,但任何使用脚本的建议都将不胜感激。

  • 在当前版本的Google Sheet中,要在单元格中插入超链接,可以执行以下操作 在新版Google Sheets中,超链接插入是不同的,您可以这样做。 但是,我有一个问题,在电子表格中插入的值不是我所期望的。在这张显示插入链接的图像中,在=HYPERLINK之前插入了一个“插入”——我不知道这是从哪里来的。有什么想法吗?

  • 有人能帮我把脚本布局转换成实际的功能代码吗?我有一般的编码知识,但我不知道正确的语法。 基本上,我需要的是一个脚本,当提交表单条目时,它会在所有工作表/选项卡之间循环。表单包含提交表单的人的姓名(字符串)、开始日期、结束日期,最后是注释字段(字符串)。 我需要脚本来遍历每个工作表的第5行,并查找在第一个表单字段中输入的字符串(提交表单的人的姓名)。如果它找到了该名称,它应该将与该名称对应的列保存到

  • 在谷歌工作表上,尝试根据特定单元格中的值隐藏/取消隐藏行,并尝试在AppsScript中为此编写脚本。发现一个隔离工作(如果B55=NO,则隐藏64行): 但我需要对多个单元格和多行使用相同的方法,只要我展开它,就只有代码的最后一部分有效,而不是第一部分: 从这里开始,B121号牢房开始工作,但我的B55停止工作。有什么提示吗?谢谢!

  • 我正在为谷歌表发送数据到服务器的脚本。目标Google Sheets文档有几个工作表。每个工作表必须有自己的菜单。所以我已经搜索了当活动表更改时触发的任何触发器,但我不能。 如何获取工作表切换事件? 谢谢你:) 使现代化我有这个功能: 但我想用函数替换onOpen,该函数将由激活工作表时触发的某个事件调用。 更新2. 我根据@Cooper的回答应用更改。现在,onOpen仍然可以正常工作,但是on

  • 通过SpreadsheetApp global,使用绑定到电子表格的应用程序脚本来影响工作表非常简单。但是,有一些功能,例如在工作表上获取/设置过滤器,只能从GoogleSheetsRESTAPI访问。 我见过一个例子,它使用应用程序脚本中的UrlFetchApp来调用谷歌工作表应用编程接口,但它的编写方式好像应用程序脚本实际上没有绑定到特定的电子表格。 当我试图从绑定到电子表格的应用程序脚本中调