Storing an HTML selection into a variable / using that data in Google Sheets

三世轮回 提交于 2021-02-11 02:44:13

问题


I am almost there with an HTML form selector, which creates an array based on data from a google sheets column (done) and populates that data into a dropdown box (done).

What I have been trying all weekend to figure out is how to capture the selection of the dropdown option and then lump that into a variable.

Here is my google script code that launches the HTML popup:

function ClientDropDownHTML() {
  var template = HtmlService.createTemplateFromFile('index');  
  var htmlDlg = template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME)
  //  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
//   var lastRow = sheet.getLastRow();
 //  var myArray = sheet.getRange('A2:A' + lastRow).getValues();
  .setWidth(500)
  .setHeight(150);
  SpreadsheetApp.getUi()
  .showModalDialog(htmlDlg, 'Select An Existing Client');
}

The HTML dropdown runs. This is the code of my HTML form, which runs the array on Google sheets data and populates it as a selector:

<!DOCTYPE html>
<html>
<head>

</head>
<body>
<form id="myForm">
  <select id="selectClient">
    <option>Choose an Existing Client</option>
         
    <?  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); ?>
   <?   var clientindex = sheet.getRange('T2').getValue(); ?>
   <?   var myArray = sheet.getRange('V2:V' + clientindex).getValues(); ?>
  
    <? for (var i = 0; i < myArray.length; ++i) { ?>
     <option> <?=myArray[i]?> </option>
     <? } ?>

  </select>

</form>
  </body>
</html> 

This is where I am stuck. To behin with I need to ensure I can capture the selection so I tried to log it - I have tried something like seen here: https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement#Example

I end up with something like the following to my HTML:

<? var select = document.getElementById('selectClient'); ?>

<? console.log(select.selectedIndex); ?>
<? console.log(select.options[select.selectedIndex].value)  ?>

However, if I do this I just get a "document is not defined" error.

I have read different issues on this website and none of the solutions seem to work for me.

In short, I need a way to:

Reference the user's selection from the HTML Store it in a variable Be able to move that variable back to Google Sheets.


回答1:


Make a selection from Select tag

GS:

function getClientList() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Sheet15');
  var vA=sh.getRange(2,1,sh.getLastRow()-1,1).getValues();
  return vA;
}

function selectClient(client) { 
  SpreadsheetApp.getUi().alert('Client is ' + client);
}

function runTwo() {
  var ui=HtmlService.createHtmlOutputFromFile('ah2');
  SpreadsheetApp.getUi().showModelessDialog(ui, 'Select Client')
}

HTML('ah2'):

<!DOCTYPE html>
<html>
<head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<form id="myForm">
  <select id="sel1" onchange="selClient()"></select>
  <script>
  $(function(){
    google.script.run
    .withSuccessHandler(function(vA){
      updateSelect(vA);
    })
    .getClientList();
  });
  function updateSelect(vA,id){
      var id=id || 'sel1';
      var select = document.getElementById(id);
      select.options.length = 0; 
      for(var i=0;i<vA.length;i++)
      {
        select.options[i] = new Option(vA[i],vA[i]);
      }
    }

  function selClient() {
    google.script.run.selectClient($('#sel1').val());
  }
  </script>

</form>
  </body>
</html> 

Sheet15:



来源:https://stackoverflow.com/questions/60362326/storing-an-html-selection-into-a-variable-using-that-data-in-google-sheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!