Home > Software design >  Get Sheet By Name with an OR statement
Get Sheet By Name with an OR statement

Time:05-13

I currently have a script that looks into multiple spreadsheets and pulls in data from a sheet contained within depending on user input.

Unfortunately there are times when users add new sheets in and do not follow the correct format for example each sheet should have four numbers, however sometimes a user will hit the space-bar before inputting those numbers.

Then when the script runs it will come back with an error because it can not find the sheet its looking for, so I need to build in a OR function, my code is quite long so I will type out an example of where I need the function below.

But for example if we can not find sheet "1234" I need to look for sheet " 1234"

var ss= getActiveSpreadsheet()
var sheet = '1234';
var altSheet = ' 1234'
var s1 = ss.getSheetByName(sheet)
//if sheet can not be found, I need to find altSheet instead

CodePudding user response:

You can get the full list of sheets using getSheets(), then use the find() and trim() methods to find the correct one.

const s1 = ss.getSheets().find(sheet => sheet.getName().trim() === '1234');
  • Related