Combining 2 scripts

Viewed 36

Is there a way to combine these two scripts, so it not only generates all the tabs based on the name from the cell list, but creates a template in each tab with the protections already in place? Thanks in advance for the time saving solution:
Code 1

function makeTabs2() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("FireDeptList");
  var last = sheet.getLastRow();//identifies the last active row on the sheet
 
  for (var i = 1; i < last; i++) {
 
    var tabName = sheet.getRange(i + 1, 1).getValue();//get the range in column A and get the value. 
    try {
      if (ss.getSheetByName(tabName) == null) {
        var create = ss.insertSheet(tabName);//create a new sheet with the value
        // this is the addition to add the formula to the new tab   
    //ss.getSheetByName(tabName).getRange("A1").setFormula('QUERY(Airchecks!A1:Q,"select * where upper(A) = \'' + tabName.toUpperCase() + '\'",1)');
 
      }
    }
    catch (err) {
      Logger.log(err);
 
    }
  }
}

Code 2

function duplicateProtectedSheet() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  sheet = ss.getSheetByName("DeptTemplate");
  copyName = ss.getSheetByName("Main").getRange('A3').getValue();
  sheet2 = sheet.copyTo(ss).setName(copyName); 
  var p = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
  var p2 = sheet2.protect();
  p2.setDescription(p.getDescription());
  p2.setWarningOnly(p.isWarningOnly());  
  if (!p.isWarningOnly()) {
    p2.removeEditors(p2.getEditors());
    p2.addEditors(p.getEditors());
    // p2.setDomainEdit(p.canDomainEdit()); //  only if using an Apps domain 
  }
  var ranges = p.getUnprotectedRanges();
  var newRanges = [];
  for (var i = 0; i < ranges.length; i++) {
    newRanges.push(sheet2.getRange(ranges[i].getA1Notation()));
  } 
  p2.setUnprotectedRanges(newRanges);
}
0 Answers
Related