IF I PUSH THE BUTTON . I WANT TO KNOW HOW CREATE NEW SHEET(COPY SHEET) NAMED NEXT 1 DAY AND THE CELL "C24" CONTENTS CHANGE TOO ?
- BASIC SHEET NAME IS : 04/18/2022
- I PUSH THE =BUTTON
- NEW CREATE SHEET (ALL CONTENTS ARE COPY, BUT THE CELL "C24" DATE 1 DAY
- NEW CREATED SHEET`S NAME : 04/19/2022
CodePudding user response:
This code help you to get the latest date form all sheet names and create a new name which is 1 day after the found sheet name.
// get all the sheet names of a spreadsheet and turn them into numbers, if the sheet name cannot be parsed into an int, make it a 0.
const sheetNames = SpreadsheetApp.getActiveSpreadsheet().getSheets()
.map(sheet => {
const name = parseInt(sheet.getName());
return isNaN(name) ? 0 : name;
});
// find the biggest numbers of all sheet names.
const maxDate = Math.max(...sheetNames) '';
// create a date object with the found number.
const maxDateObj = new Date(`${maxDate.slice(0,4)}-${maxDate.slice(4,6)}-${maxDate.slice(6,8)}`);
// add 1 day on it.
const nextDateArr = JSON.stringify(new Date(maxDateObj.setDate(maxDateObj.getDate() 1))).split(/"|-|T/g);
// format the date object into your sheet name format.
const nextDate = [nextDateArr[1],nextDateArr[2],nextDateArr[3]].join('');
console.log(nextDate);