Explanation of my my objective : By the press of a button in my google sheets, add 7 days to a currently exsting date in a cell.
What currently happens : When I press to run my script instead of adding 7 days it simply writes 7 at then end.
My script :
function IncrementW() {
var spreadsheet = SpreadsheetApp.getActive();
SpreadsheetApp.getActiveSheet().getActiveCell().setValue(SpreadsheetApp.getActiveSheet().getActiveCell().getValue() + 7);
};
What the output gives me:
From: 7/12/2020 to >> Sun Jul 12 2020 00:00:00 GMT-0400 (Eastern Daylight Time)7
What my output should be :
From: 7/12/2020 to >> 7/19/2020
Thank you for the help.
getValue()
returns a Date
object(which gets converted to string
, when used with the +
operator). Use setDate()
to setDate:
function IncrementW() {
const spreadsheet = SpreadsheetApp.getActive();
const cell = spreadsheet.getActiveSheet().getActiveCell();
const dt = cell.getValue();
dt.setDate(dt.getDate() + 7);
cell.setValue(dt);
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.