Adding Script to Make Each New Tab a Template

378 views
Skip to first unread message

Timothy Patten

unread,
May 11, 2021, 11:36:23 AM5/11/21
to Google Apps Script Community
Hi All,

I'm using this to run a function that creates a new tab for each student on the "Roster" page when it is run. 

Code.png

I'm trying to add that each of those newly created tabs would, except for the name, would also be a duplicate of the tab called 'Template,' or at least to automatically copy and paste the formulas from the 'Template' tab to the newly created pages. I haven't been able to get it to work with both functionalities and any help would be greatly appreciated. 

Doc linked below.

Laurie Nason

unread,
May 12, 2021, 2:36:44 AM5/12/21
to google-apps-sc...@googlegroups.com
Hi Tim,
I do something similar - it takes the name of the new sheet (sheetName) as it checks to see if it exists and will re-create or not as necessary, the name of the template sheet (templateName), and if you want to re-create it (recreate) - this will delete the old sheet so be careful!! I learned a trick that would make this simpler this week, but I may or may not use it to make this simpler in the future. This inserts the new sheet after the currently selected sheet - but you can move them if you need to at a later stage.
...and here's the function that I use - and reuse... and reuse... :-) :

function sheetExists(sheetName,templateName,recreate){
  var mySheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  if (mySheet==null){
    //sheet doesn't exist so create it use duplicate
    // Create a new sheet based on the template sheet
    var myTemplate = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(templateName);
    mySheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet(sheetName, {templatemyTemplate});    
  }
  else{
    //sheet exists, do we need to recreate it?
    if (recreate==1){
      //Delete old sheet
      SpreadsheetApp.getActiveSpreadsheet().deleteSheet(mySheet);
      //Create a new sheet
      var myTemplate = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(templateName);
      mySheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet(sheetName, {templatemyTemplate});
    }
  }
  return mySheet;
}


Hope this helps,
Laurie

Virus-free. www.avg.com

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/ff2eff23-3e47-4846-966c-448652b8bfdan%40googlegroups.com.


--

Laurie Nason 

The KAUST School – Operational Analyst

Information Technology Support
Deep in the Bowels of the School
Landline: +966-12-808-6853

Kim Nilsson

unread,
May 12, 2021, 2:58:05 AM5/12/21
to Google Apps Script Community
@Timothy,

you can probably learn a few things from Alice Keeler's Template Tab script.
There is still a link to one of her Sheet versions on the page, even though she has now turned it into an addon.
Also the RubricTab could be enlightening.

/Kim
Reply all
Reply to author
Forward
0 new messages