Skip to content
Advertisement

Google Webapp: How to dynamically pass array values to jquery script

I’ve been working on an answer to StackOverflow question Datepicker: Disabling dates in the data. I’ve successfully developed a small webapp that excludes specific dates from a jQuery Datepicker using the beforeShowDay option and an array of hardcoded dates.

Problem

At present, the array of excluded dates is hard coded, but these dates should be generated dynamically. Although I have a function in code.gs getuserdates() which will return the “userdates” array ["12/13/2019", "12/14/2019", "12/16/2019", "12/17/2019", "12/24/2019"], I haven’t found a single reference on the web to explain how to pass the array values dynamically to the webapp.

The answer by @Tanaike on Disable dates in the datepicker based on values from the Google Sheet using Google Apps Script seems relevant to this problem, but I’ve failed to adapt any successful code that includes the array. I think part of the problem here is that Tanaike’s answer was 100% Javascript whereas this scenario requires both Javascript and jQuery.

I tried scriptlets (not expecting them to work, but you never know. They all generated an error Uncaught SyntaxError: Unexpected token '<'

  • var userdates = <? getuserdates(); ?>

  • var userdates = <?= getuserdates(); ?>

  • var userdates = <?!= getuserdates(); ?>

Goal To update dynamically the values of the variable array.

Link to Spreadsheet

Latest webapp url (updated)

Code

The following code works flawlessly; the only problem is that the array values are hard coded.

code.gs

JavaScript

Page.html

JavaScript

JavaScript.html

JavaScript

Advertisement

Answer

When getuserdates() in Google Apps Script side returns the value of ["12/13/2019", "12/14/2019", "12/16/2019", "12/17/2019", "12/24/2019"], userdates of var userdates = <?= getuserdates(); ?> is 12/13/2019,12/14/2019,12/16/2019,12/17/2019,12/24/2019 of the string type. I thought that by this, your script doesn’t work.

So, as one of several answers, how about this answer? Please modify JavaScript.html.

Pattern 1:

In this pattern, the scriptlets are used. I thought that this thread might be useful.

From:

JavaScript

To:

JavaScript
  • When the script is run, userdates is ["12/13/2019", "12/14/2019", "12/16/2019", "12/17/2019", "12/24/2019"].
  • As one more pattern using the scriptlets, for example, if you want to use var userdates = <?= getuserdates(); ?>, you can also modify var userdates = <?= getuserdates(); ?> to var userdates = <?= getuserdates() ?>.split(",");.

Pattern 2:

In this pattern, google.script.run is used.

From:

JavaScript

To:

JavaScript
  • When the script is run, userdates retrieved from getuserdates() is used as ["12/13/2019", "12/14/2019", "12/16/2019", "12/17/2019", "12/24/2019"].

Note:

  • In this case, it supposes that getuserdates() returns ["12/13/2019", "12/14/2019", "12/16/2019", "12/17/2019", "12/24/2019"].

References:

If I misunderstood your question and this was not the direction you want, I apologize.

Edit 1:

About issue 1:

About the reason that the error of Uncaught SyntaxError: Unexpected token '<' occurs, the reason of this issue is <?!= include('JavaScript'); ?>. So please modify as follows.

From:

JavaScript

To:

JavaScript
  • In this case, please don’t add <script>###</script> to JavaScript of <?!= include('JavaScript'); ?>.
  • Unfortunately, it seems that the scriptlets cannot be used into the scriptlets.

About issue 2:

About the reason that [""12/11/2019"", ""12/15/2019"", ""12/16/2019"", ""12/17/2019"", ""12/24/2019""] is returned from getuserdates(), the reason of this issue is userdates.push('"' + datasheetData[i][5]+ '"');. So please modify as follows.

From:

JavaScript

To:

JavaScript

Edit 2:

When the pattern 1 is used, the script is as follows. About getuserdates() of GAS side, please modify from userdates.push('"' + datasheetData[i][5]+ '"'); to userdates.push(datasheetData[i][5]);. And please modify HTML & Javascript side as follows.

HTML & Javascript: Page.html

JavaScript

HTML & Javascript: JavaScript.html

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement