I need to update dates in a sheet once a week.
I would like to manually run a script to do this.
Each date in column "C" will increase by a number of days in column "D" in same row. Only dates prior to a date in specific cell (F2 in example) will be updated.
See this sheet as an example: https://docs.google.com/spreadsheets/d/11f6G5_vNK5Z8UR2A_MUUXpL8awC2mJ8ozpnznWQ_anM/edit?usp=sharing
Column C - Service Date | Column D - No. of days to advance |
---|---|
12/3/2021 | 7 |
12/3/2021 | 14 |
12/10/2021 | 7 |
12/10/2021 | 7 |
12/17/2021 | 28 |
I have read many posts but am very new at these types of scripts. Any help or pointing in the right direction would be great!
Thanks, Dave
CodePudding user response:
function updateDates() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
const vs = sh.getRange(2,1,sh.getLastRow() - 1, sh.getLastColumn()).getValues();
const dt = new Date(sh.getRange('F2').getValue());
const dthv = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();
const oA = [];
vs.forEach(r => {
let d = new Date(r[2]);
let dv = new Date(d.getFullYear(),d.getMonth(),d.getDate()).valueOf();
if(dv < dthv) {
oA.push([new Date(d.getFullYear(),d.getMonth(),d.getDate() r[3])]);
} else {
oA.push([r[2]]);
}
});
sh.getRange(2,3,oA.length,oA[0].length).setValues(oA);
}
Sheet 0 Before:
COL1 | COL2 | COL3 | COL4 | COL5 | COL6 |
---|---|---|---|---|---|
11/10/2021 | 5 | 11/15/2021 | |||
11/11/2021 | 6 | ||||
11/12/2021 | 7 | ||||
11/13/2021 | 8 | ||||
11/14/2021 | 9 | ||||
11/15/2021 | 10 | ||||
11/16/2021 | 11 | ||||
11/17/2021 | 12 | ||||
11/18/2021 | 13 | ||||
11/19/2021 | 14 | ||||
11/20/2021 | 15 | ||||
11/21/2021 | 16 | ||||
11/22/2021 | 17 | ||||
11/23/2021 | 18 | ||||
11/24/2021 | 19 |
Sheet0 After:
COL1 | COL2 | COL3 | COL4 | COL5 | COL6 |
---|---|---|---|---|---|
11/15/2021 | 5 | 11/15/2021 | |||
11/17/2021 | 6 | ||||
11/19/2021 | 7 | ||||
11/21/2021 | 8 | ||||
11/23/2021 | 9 | ||||
11/15/2021 | 10 | ||||
11/16/2021 | 11 | ||||
11/17/2021 | 12 | ||||
11/18/2021 | 13 | ||||
11/19/2021 | 14 | ||||
11/20/2021 | 15 | ||||
11/21/2021 | 16 | ||||
11/22/2021 | 17 | ||||
11/23/2021 | 18 | ||||
11/24/2021 | 19 |
CodePudding user response:
the script below from Copper worked like a charm.