Abhay
Abhay

Reputation: 845

Getting Error: "sheet.indexOf is not a function" when creating dropdown in Google Apps Script

I want to create a dropdown in the google sheets sidebar that will include the name all sheets that do NOT contain 'Lookup' in their name.

I'm trying to use scriptlets to filter the sheet names and this is what I got in the Page.html file:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
  
    <!-- Select an exisiting data table -->
    Select sheet to classify: <br>
    <select id="dataSheetDropdown">
    <? var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets() ?>
    <? for(var i=0;i<sheets.length;i++) { ?>          
          <? sheet = sheets[i] ?>
          <? if(sheet.indexOf("Lookup")>-1){?>
          <option> <?= sheet.getName()?> </option>
          <? } ?>
    <? } ?>
    </select>
    <button onclick="selectLookup()">Select</button>
   
    
    
    <script> 
      function selectLookup(){
       var name=document.getElementsById("sheetDropdown")[0].value;
       google.script.run.goToSheet(name);
      };
    </script>
    
  </body>
</html>

When I try and run this script, I get this error:

TypeError: sheet.indexOf is not a function

Any idea how I can get my script to run correctly? Your help would be greatly appreciated

Upvotes: 1

Views: 466

Answers (1)

Tanaike
Tanaike

Reputation: 201603

I believe your goal as follows.

  • You want to retrieve the sheet with the sheet name which don't include Lookup and show them to the dropdown list.

Modification points:

  • I think that the reason of your issue is that sheet is the sheet object. By this, such error occurs at indexOf.
  • When you want to retrieve the sheet with the sheet name which don't include Lookup, please use if(sheet.getSheetName().indexOf("Lookup") == -1){.

When your script is modified, it becomes as follows.

Modified script:

From:
<? sheet = sheets[i] ?>
<? if(sheet.indexOf("Lookup")>-1){?>
<option> <?= sheet.getName()?> </option>
<? } ?>
To:
<? sheet = sheets[i] ?>
<? if(sheet.getSheetName().indexOf("Lookup") == -1){?>
<option> <?= sheet.getSheetName() ?> </option>
<? } ?>

References:

Upvotes: 2

Related Questions