Search code examples
twitter-bootstrapgoogle-apps-scripttypeahead

Twitter Bootstrap Typeahead Multiple Values in Google Apps Script


I'm trying to implement a solution I found in this post in Google Apps Script (GAS). It works great in JSFiddle, but there's something I'm missing on account of which it's not working in GAS. Here are my two files that comprise the Google Apps Script implementation of the solution. Can you help me please?

Code.gs

function setUpNewSidebar() {
  var ui = HtmlService.createTemplateFromFile('typeahead')
           .evaluate()
           .setTitle('Title')
           .setSandboxMode(HtmlService.SandboxMode.IFRAME);
         SpreadsheetApp.getUi().showSidebar(ui);
}

typeahead.html

<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.1.1/css/bootstrap.min.css"></script>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.1.1/js/bootstrap.min.js"></script>
</head>

<input type="text" class="typeahead" />
<script>
$(document).ready(function() {

!function(source) {
    function extractor(query) {
        var result = /([^,]+)$/.exec(query);
        if(result && result[1])
            return result[1].trim();
        return '';
    }

    $('.typeahead').typeahead({
        source: source,
        updater: function(item) {
            return this.$element.val().replace(/[^,]*$/,'')+item+',';
        },
        matcher: function (item) {
          var tquery = extractor(this.query);
          if(!tquery) return false;
          return ~item.toLowerCase().indexOf(tquery.toLowerCase())
        },
        highlighter: function (item) {

          var query = extractor(this.query).replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&')
          return item.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
            return '<strong>' + match + '</strong>'
          })
        }
    });

}(["Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Dakota","North Carolina","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"]);

});
</script>
</html>

Solution

  • Big thanks to Spencer for this working solution! To get this to work in GAS, the typeahead.html file should be updated to the following (big changes are in the head tag, and the Code.gs file remains unchanged):

    <!DOCTYPE html>
    <html>
    <head>
    <base target="_top">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.1.1/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.1.1/css/bootstrap.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.2/bootstrap3-typeahead.min.js"></script>
    </head>
    
    <input type="text" class="typeahead" />
    <script>
    $(document).ready(function() {
    
    !function(source) {
        function extractor(query) {
            var result = /([^,]+)$/.exec(query);
            if(result && result[1])
                return result[1].trim();
            return '';
        }
    
        $('.typeahead').typeahead({
            source: source,
            updater: function(item) {
                return this.$element.val().replace(/[^,]*$/,'')+item+',';
            },
            matcher: function (item) {
              var tquery = extractor(this.query);
              if(!tquery) return false;
              return ~item.toLowerCase().indexOf(tquery.toLowerCase())
            },
            highlighter: function (item) {
    
              var query = extractor(this.query).replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&')
              return item.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
                return '<strong>' + match + '</strong>'
              })
            }
        });
    
    }(["Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Dakota","North Carolina","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"]);
    
    });
    </script>
    </html>