I am very new to Google Apps Scripts, but am working to create a spreadsheet function that will create new tabs based a list of names (in cells C6:C). Some of the cells may not have values in them, so would like to exclude these cells from creating new tabs.
The function works as planned, but after running the script, the new tabs are named with a (1), (2), (3),... etc. after the name that is in the list. For example if the first name in the list is "First Tab", the new tab gets named "First Tab (1)" after my current script is run.
I'm not sure as to what would be causing the numbers in the tab names, so any help would be greatly appreciated!
For reference, Cell B4 contains a count of the number of names in the List that the new Tabs should be created from
'''
function createProjectTabs() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = ss.getRange("C6:C");
var values = range.getValues().filter(String);
var total = ss.getRange('B4').getValue();
for(var k=0; k < total; k++){
try{
var tabName = range[k][0];
ss.insertSheet(tabName);}
catch(err){
tabName = values [k][0]+ " " + k;
ss.insertSheet(tabName);}
}
}
function createNonDuplicateProjectTabs() {
var ss = SpreadsheetApp.getActive();
const shts = ss.getSheets().map(s => s.getName());
var sh = ss.getSheets()[0];
var vs = ss.getRange(6,3,sh.getLastRow()-5,1).getValues().flat();
vs.forEach(n => {
if(!~shts.indexOf(n)) {
ss.insertSheet(n)
}
});
}