簡體   English   中英

在 Google 表格中將日期增加 7 天

[英]Increment 7 days to a date in Google Sheets

我的目標解釋:通過按我的谷歌表格中的按鈕,將 7 天添加到單元格中當前存在的日期。

當前發生的情況:當我按下運行我的腳本而不是添加 7 天時,它只是在結束時寫入 7。

我的腳本

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

output 給了我什么:

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

我的 output 應該是

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

感謝您的幫助。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM