【问题标题】:Increment 7 days to a date in Google Sheets在 Google 表格中将日期增加 7 天
【发布时间】:2020-07-06 16:03:39
【问题描述】:

我的目标说明:按一下我的 google 表格中的按钮,将单元格中当前存在的日期添加 7 天。

当前发生的情况:当我按下以运行我的脚本而不是添加 7 天时,它只是在末尾写入 7。

我的脚本

function IncrementW() {
  var spreadsheet = SpreadsheetApp.getActive();
  SpreadsheetApp.getActiveSheet().getActiveCell().setValue(SpreadsheetApp.getActiveSheet().getActiveCell().getValue() + 7);
};

输出给了我什么:

From: 7/12/2020 to >> Sun Jul 12 2020 00:00:00 GMT-0400 (Eastern Daylight Time)7

我的输出应该是什么

From: 7/12/2020 to >> 7/19/2020

感谢您的帮助。

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    getValue() 返回一个Date 对象(当与+ 运算符一起使用时,它被转换为string)。使用setDate() 设置日期:

    function IncrementW() {
      const spreadsheet = SpreadsheetApp.getActive();
      const cell = spreadsheet.getActiveSheet().getActiveCell();
      const dt = cell.getValue();
      dt.setDate(dt.getDate() + 7);
      cell.setValue(dt);
    };
    

    【讨论】:

    • 当我尝试你的新函数时,它返回 1595131200000。起初我认为这是一个格式问题,但似乎不是。有什么想法吗?
    • @Cruxer 我在发布后不久对其进行了编辑。尝试新答案
    猜你喜欢
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多