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
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
.
In this pattern, the scriptlets are used. I thought that this thread might be useful.
var userdates = ["12/13/2019", "12/14/2019", "12/16/2019", "12/17/2019", "12/24/2019"];
var userdates = [];
<? var data = getuserdates(); ?>
<? for (var i = 0; i < data.length; i++) { ?>
userdates.push(<?= data[i] ?>);
<? } ?>
userdates
is ["12/13/2019", "12/14/2019", "12/16/2019", "12/17/2019", "12/24/2019"]
.var userdates = <?= getuserdates(); ?>
, you can also modify var userdates = <?= getuserdates(); ?>
to var userdates = <?= getuserdates() ?>.split(",");
.In this pattern, google.script.run is used.
var userdates = ["12/13/2019", "12/14/2019", "12/16/2019", "12/17/2019", "12/24/2019"];
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();
userdates
retrieved from getuserdates()
is used as ["12/13/2019", "12/14/2019", "12/16/2019", "12/17/2019", "12/24/2019"]
.getuserdates()
returns ["12/13/2019", "12/14/2019", "12/16/2019", "12/17/2019", "12/24/2019"]
.If I misunderstood your question and this was not the direction you want, I apologize.
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.
</div>
<?!= include('JavaScript'); ?>
</body>
</div>
<script>
var userdates = [];
<? var data = getuserdates(); ?>
<? for (var i = 0; i < data.length; i++) { ?>
userdates.push(<?= data[i] ?>);
<? } ?>
</script>
<?!= include('JavaScript'); ?>
</body>
<script>###</script>
to JavaScript
of <?!= include('JavaScript'); ?>
.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.
userdates.push('"' + datasheetData[i][5]+ '"');
userdates.push(datasheetData[i][5]);
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.
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/themes/cupertino/jquery-ui.css">
<?!= include('Stylesheet'); ?>
</head>
<body>
<div class="demo" >
<h1>jQuery datepicker</h1>
<p>click here : <input type="text" name="date" id="datepicker" /></p>
</div>
<script>
var userdates = [];
<? var data = getuserdates(); ?>
<? for (var i = 0; i < data.length; i++) { ?>
userdates.push(<?= data[i] ?>);
<? } ?>
</script>
<?!= include('JavaScript'); ?>
</body>
</html>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<script>
$(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"];
}
}
});
});
</script>
You can get values from the Apps Script runtime with client-side code.
To do this you need to handle the response from the function you calling with the withSuccessHandler function.
Changing your function to create the DatePicker with the date returned from the backend would look like this:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<script>
function onSuccess(value) {
var userdates = value;
$('#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"];
}
}
});
}
google.script.run.withSuccessHandler(onSuccess).getuserdates();
</script>