简体   繁体   中英

Creating an autocomplete function in Google script that works with a list of values from the Google Sheet

I'm trying to create an autocomplete text field, that autocompletes the country that's filled in, if the country already exists in the google sheet. At the moment my code only works, when I write all the possible countries in the 'availabletags' variable. But I want it to get the values directly from the google sheet. This is the html & script:

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" href="/resources/demos/style.css">

    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
    <div class="ui-widget">
        <label for="text">country</label>
        <input id="text">
    </div>
    <div>
        <button id="btn"> Run it! </button>
    </div>
    <script>
        $(function() {
            var availableTags = [ //should be changed to availableTags = list;
                "belgium",
                "france",
                "greece",
                "spain",
                "italy",
                "the netherlands"
            ];
            $("#text").autocomplete({
                source: availableTags
            });
        });
        document.getElementById("btn").addEventListener("click", doStuff);

        function doStuff() {
            var ucountry = document.getElementById("text").value;
            google.script.run.userClicked(ucountry);
            document.getElementById("text").value = "";
        };
    </script>
</body>
</html>

I wrote following code in google script to retrieve the countries from the google script, and when I look at the log, the list of countries from the google sheet is indeed in the list variable.

function doGet() {
    var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
    var list = ws.getRange(1,1,ws.getRange("A1").getDataRegion().getLastRow(),1).getValues(); // contains countries
    Logger.log(list);
    var template = HtmlService.createTemplateFromFile("page");
    template.list = list.map(function(r){return r[0]; });
    var html = template.evaluate();
    return html;
}
function userClicked(country){
    var url = "https://docs.google.com/spreadsheets/d/1IMxZwN3swMTf9EoF_k3iRV7Zc6iwzoWzov5-qC_MSKU/edit#gid=0";
    var ss = SpreadsheetApp.openByUrl(url);
    var ws = ss.getSheetByName("Data");
    ws.appendRow([country]);
}

I would like to have the var availableTags = list; But when I do that, the autocomplete stops working. Any help would be appreciated!

Use google.script.run with SuccessHandler

This implies the creation of an additional .gs function that will be called from clientside onload.

Sample:

Code.gs

function doGet() {
  var template = HtmlService.createTemplateFromFile("page");
  var html = template.evaluate();
  return html;
}

function getCountry(){
  var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
  var list = ws.getRange(1,1,ws.getRange("A1").getDataRegion().getLastRow(),1).getValues(); // contains countries
  list = list.map(function(r){return r[0]; });
  Logger.log(list);
  return list;
}
function userClicked(country){
  var url = "https://docs.google.com/spreadsheets/d/1IMxZwN3swMTf9EoF_k3iRV7Zc6iwzoWzov5-qC_MSKU/edit#gid=0";
  var ss = SpreadsheetApp.openByUrl(url);
  var ws = ss.getSheetByName("Data");
  ws.appendRow([country]);
}

page.html

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" href="/resources/demos/style.css">

    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
    <div class="ui-widget">
        <label for="text">country</label>
        <input id="text">
    </div>
    <div>
        <button id="btn"> Run it! </button>
    </div>
    <script>
        google.script.run.withSuccessHandler(tags).getCountry();
        function tags(list) {
          console.log(list);
          var availableTags = list;
          $("#text").autocomplete({
              source: availableTags
            });
        };
        document.getElementById("btn").addEventListener("click", doStuff);

        function doStuff() {
          var ucountry = document.getElementById("text").value;
          google.script.run.userClicked(ucountry);
          document.getElementById("text").value = "";
        };
    </script>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM