Home > Blockchain >  create new sheets from a template (sheetnames coming from Sheet1!A1:A5) and exclude name "EFG&q
create new sheets from a template (sheetnames coming from Sheet1!A1:A5) and exclude name "EFG&q

Time:10-10

  function CreateNEWSheets() 
  {var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Sheet1');
  var tsh=ss.getSheetByName('Template');
  var sr=1;
  var vA=sh.getRange(sr,1,sh.getLastRow()-sr 1,1).getValues();
  for (var i=0;i<vA.length;i  ) 
  {ss.insertSheet(vA[i][0], {template:tsh});}
   }



     A
1    XXX
2    YYY
3    ZZZ
4    ABC
5    CBC
6    EFG
7    ABC
8    CBC
9    EFG
10   EFG 

Function is to create new sheets from a template (sheetnames coming from Sheet1!A4:A10) and exclude name "EFG"

Tried my best and modified script to:

 function CreateNEWSheets1111() 
  {var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Stats');
  var tsh=ss.getSheetByName('Template');
  var sr=4;
  var lr=10;
  var vA=sh.getRange(sr,1,lr-sr 1,1).getValues();
  for (var i=0;i<lr;i  ) 
  {ss.insertSheet(vA[i][0], {template:tsh});}
   }

just want to add an Exclusion to "EFG"

CodePudding user response:

Use Array.filter() and Array.forEach(), like this:

function createSheetsUsingTemplate() {
  const ss = SpreadsheetApp.getActive();
  const templateSheet = ss.getSheetByName('Template');
  const sheetNames = ss.getRange('Sheet1!A1:A')
    .getValues()
    .flat()
    .filter(name => name && name !== 'EFG');
  sheetNames.forEach(name => ss.insertSheet(name, { template: templateSheet }));
}
  • Related