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

后端 未结 2 1900
孤独总比滥情好
孤独总比滥情好 2021-01-23 21:19

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

2条回答
  •  不要未来只要你来
    2021-01-23 21:35

    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 = 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:

    var userdates = ["12/13/2019", "12/14/2019", "12/16/2019", "12/17/2019", "12/24/2019"];
    

    To:

    var userdates = [];
    
    
      userdates.push();
    
    
    • 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 = , you can also modify var userdates = to var userdates = .split(",");.

    Pattern 2:

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

    From:

    var userdates = ["12/13/2019", "12/14/2019", "12/16/2019", "12/17/2019", "12/24/2019"];
    

    To:

    google.script.run.withSuccessHandler(userdates => {
      $(function() {
        $('#datepicker').datepicker({
          minDate: "+3W", 
          maxDate: "+12W",
          beforeShowDay: function (date) {
            $thisDate = (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear();
            if ($.inArray($thisDate, userdates) == -1) {
              return [true, ""];
            } else {
              return [false, "", "Unavailable"];
            }
          }
        });
      });
    }).getuserdates();
    
    • 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:

    • HTML Service: Templated HTML
    • Class google.script.run

    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 . So please modify as follows.

    From:

    To:

  • In this case, please don't add to JavaScript of .
  • 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:

userdates.push('"' + datasheetData[i][5]+ '"');

To:

userdates.push(datasheetData[i][5]);

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



  
    
    
    
  
  
  

jQuery datepicker

click here :

HTML & Javascript: JavaScript.html




提交回复
热议问题