How to get the list of all sheet in a dropdown list in a sidebar

前端 未结 1 1203
無奈伤痛
無奈伤痛 2021-01-28 03:44

I\'m working in an sidebar menu in an google sheet, to set some variables that i need in my script.

My script is already working. It is easy, on form submit, it will cre

相关标签:
1条回答
  • 2021-01-28 03:55

    You can use scriptlets

    This allows you to use Apps Script functions within the html code:

      <body>
        <select name="Available sheets" onchange="myJsFunction()")>
        <? var sheets=spreadsheet.getSheets(); ?>
        <? for(var i=0;i<sheets.length;i++) { ?>
          <option value=<?=sheets[i].getName()?>> <?= sheets[i].getName()?></option>
        <? } ?>
        </select>
      <script>
       function myJsFunction(){
         var name=document.getElementsByName("Available sheets")[0].value;
         google.script.run.goToSheet(name);
        };
      </script>
      </body>
    

    google.script.run allows you to pass the selected sheet to an Apps Script function.

    In the .gs file you then need to define what you want to do within the Apps Script function based on the selected sheet, example:

    function goToSheet(selectedSheetName){
      Logger.log('The selected sheet is: '+ selectedSheetName);
      }
    

    Also, you need to change your line var html = HtmlService.createHtmlOutputFromFile('SideBar')

    to var html = HtmlService.createTemplateFromFile('SideBar').evaluate()

    to allow the script to evaluate the content of the scriptlets.

    0 讨论(0)
提交回复
热议问题