Home > database >  duplicate google sheet based on a template, rename and add the same name to a cell in each new sheet
duplicate google sheet based on a template, rename and add the same name to a cell in each new sheet

Time:06-04

There are many code snippets that are close to what I need but nothing that quite fits. I have a bit of code that duplicates and renames the tabs. I am just missing the bit that will put the tab name in cell C2 of each sheet. For example, if the sheet name is ADM_001.a then C2 also contains ADM_001.a. Is this an easy edit?

    function duplicateTemplateN(){
  const sh = SpreadsheetApp.getActive();
  const template = sh.getSheetByName("TEMPLATE");
  const sNames = ["ADM_001.a","ADM_001.b","ADM_002.a","ADM_002.b","ADM_003.a","ADM_003.b","ADM_004.a","ADM_004.b","ADM_005.a","ADM_005.b","ADM_006.a","ADM_006.b","ADM_007.a","ADM_007.b","ADM_008.a","ADM_008.b","ADM_009.a","ADM_009.b","ADM_010.a","ADM_010.b"];
 
  for (let i in sNames){
    template.copyTo(sh).setName(sNames[i]);
  }
}

CodePudding user response:

Try this

function duplicateTemplateN(){
  const ss = SpreadsheetApp.getActive();
  const template = ss.getSheetByName("TEMPLATE");
  const sNames = ["ADM_001.a","ADM_001.b","ADM_002.a","ADM_002.b","ADM_003.a","ADM_003.b","ADM_004.a","ADM_004.b","ADM_005.a","ADM_005.b","ADM_006.a","ADM_006.b","ADM_007.a","ADM_007.b","ADM_008.a","ADM_008.b","ADM_009.a","ADM_009.b","ADM_010.a","ADM_010.b"];
 sNames.forEach(n => {
   template.copyTo(ss).setName(n);
   ss.getSheetByName(n).getRange("C2").setValue(n);
 });
}
  • Related