简体   繁体   中英

Add days to a date with google-scripts in spreadsheet

I want to add 7 days to a specific cell (which contains a date) when using a script in spreadsheets

So far I have this:

 var Date = sheet.getRange("A3");
 var Day = [Date.getCell(1, 1).getValue()];
    Day = Number(Day)+7;
    Date.setValue(Day);

but it doesn't add 7 days and I keep getting really long GMT dates. My format needs to be like: 21-06-2016 (european style)

Took me a while, but here it is:

In this case, cell A3 is the date which needs to change a week (7days) after using google-scripts.

Take a different cell and give it the same date for you to start with (for example take A1 ). Now take cell A2 and give it the function =A1 Change this cells layout from a date layout to a normal tekst. Now the date will change in a number. For instance: 26-12-2016 ( cell A1 ) will become 42730 ( cell A2 ).

Now go to cell A3 and give it the function =A2 and give it the layout of a date (in this case: 26-12-2016)

In Script: use this

var Date = sheet.getRange("A2");  
var Day = [Date.getCell(1,1). getValue()];]

Day = Number(Day)+7; //change "7" for the amount of days you want
Date.setValue(Day);

This script will add the amount of days you want to cell A2 and cell A3 will change accondingly.

After you used the scripts once, there is no need for the use of cell A1 anymore because cell A2 has changed after using the script. You can clear cell A1 if you like.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM