Google Sheets and Apps Script – Add a Sheet from a Template

google sheetsgoogle-apps-script

Is there a way to add a new sheet to a spreadsheet from a template? If I'm using a template for time, I'd like to add a new sheet from the same template every two weeks.

Best Answer

Yes, using the sheet.copyTo(destination) method. In the spreadsheet where you want to add the new sheets:

function copyFromTemplate(){
  var templateSpreadsheet = SpreadsheetApp.openById(TEMPLATE_ID);
  var template = templateSpreadsheet.getSheets()[0]; //Assuming it is the first sheet

  //The default name will be "Copy of [original name]". We can use today's date to rename it
  var today = (new Date()).toDateString();

  var currentSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  template.copyTo(currentSpreadsheet).setName(today);
}

You can set up a time-driven trigger for every week, or set up two monthly triggers to run the function every two weeks.