I have a template sheet inside a workbook that I want to copy to a new workbook that is not yet created. I want this to run on the 29th of each month and do the following:
I have tried a few copy functions with time triggers but they have required a blank workbook to be open already.
This script
createSheetFromTemplate
SpreadsheetApp.openById
createTrigger
Disclaimer: At the moment this script just creates a new Spreadsheet with today's month and a new sheet named with today's date. I understand you may be looking to create a sheet for next month. This problem is probably best served by another question - perhaps look here.
function createSheetFromTemplate() {
// Getting details of today's date
let date = new Date()
let year = date.getFullYear()
let month = date.getMonth() + 1
let monthName = date.toLocaleString('default', { month: 'long' });
let day = date.getDate()
console.log(year, monthName, day)
// Get template based on year
let templateSpread = SpreadsheetApp.openById("1H7GBVxK4f0nmYR5xfZawy6nreDTCZaj76-dKE9eOtUE")
let templateSheet = templateSpread.getSheetByName("Template_" + year)
// Create new Spreadsheet with renamed template
let newSpread = SpreadsheetApp.create(monthName + " " + year)
let newSheet = templateSheet.copyTo(newSpread).setName(year.toString().substring(2,4) + "." + month + "." + day)
let sheetToDelete = newSpread.getSheetByName("Sheet1")
newSpread.deleteSheet(sheetToDelete)
}
// Create trigger to run on 29th of each month.
function createTrigger() {
ScriptApp.newTrigger('createSheetFromTemplate')
.timeBased()
.onMonthDay(29)
.create();
}