我有以下html代码:
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
.dp-highlight .ui-state-default {
background: #484;
color: #FFF;
}
</style>
<script type='text/javascript'>
var gigDates;
</script>
<script>
window.onload = getGigDates();
function getGigDates(){
google.script.run.withSuccessHandler(retDates).getDates(date);
}
function retDates(gigDatesTwo){
console.log("Returning the dates needed: " +gigDatesTwo)
gigDates = gigDatesTwo;
}
$(function() {
$( "#datepicker" ).datepicker({
onSelect: function(date) {updDate(date);},
selectWeek: true,
inline: true,
startDate: '01/01/2000',
firstDay: 1,
beforeShowDay : available,
});
});
function available(date) {
console.log("From the Spread sheet: " +gigDates);
var dmy = (date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear();
console.log(dmy+' : '+($.inArray(dmy, gigDates)));
if ($.inArray(dmy, gigDates) != -1) {
return [true, "","Available"];
} else {
return [false,"","unAvailable"];
}
}
function updDate(date){
google.script.run.withSuccessHandler(myReturnFunction).updDate(date);
}
function myReturnFunction(whatGotReturned){
document.getElementById("demo").innerHTML = whatGotReturned;
}
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker" onchange="updDate()"></p>
<p id="demo"></p>
<input type="button" value="Close" onclick="google.script.host.close()" />
</body>
函数getGigDates返回以下内容:
[“ 5/12/2014”,“ 6/14/2014”,“ 6/6/2014”,“ 6/13/2014”,“ 6/17/2014”,“ 5/30/2014”,“ 6/3/2014”,“ 6/4/2014”,“ 6/18/2014”,“ 6/18/2014”,“ 6/20/2014”,“ 6/25/2014”,“ 6 / 19/2014”,“ 6/13/2014”,“ 6/27/2014”,“ 7/4/2014”,“ 7/1/2014”,“ 7/6/2014”,]
但是,当函数“ available”在2014年4月4日将dmy与gigDates进行比较时,它将返回-1。
编辑:这是一个条目的控制台输出的一部分:
在电子表格中:[“ 5/12/2014”,“ 6/14/2014”,“ 6/6/2014”,“ 6/13/2014”,“ 6/17/2014”,“ 5/30 / 2014”,“ 6/3/2014”,“ 6/4/2014”,“ 6/18/2014”,“ 6/18/2014”,“ 6/20/2014”,“ 6/25/2014” “,” 6/19/2014“,” 6/13/2014“,” 6/27/2014“,” 7/4/2014“,” 7/1/2014“,” 7/6/2014“, ]
2014年6月14日:-1
我的变量dmy或gigDates是否格式错误?
劳伦
编辑Google脚本:
function getDates(){
var ss = SpreadsheetApp.openById("0AnDrS3YbyWoLdC15aktnMTluUkQ3dWhNTFlyQXBPOFE");
var list = ss.getSheetByName('Form Responses 1');
var values = list.getRange(1,5,ss.getLastRow(),5).getValues();
var gigDates;
gigDates= "[";
for (var i in values){
if (values[i][0]!=""){
if (values[i][0] != "Gig Date"){
var ssDate = Utilities.formatDate(values[i][0], "GMT", "M/d/yyyy");
gigDates = gigDates + '"'+ssDate+'"'+",";
}
}
}
gigDates = gigDates + "]";
Logger.log(gigDates);
return gigDates;
}
最佳答案
您的代码应返回Array对象而不是字符串。您的代码应如下所示:
function getDates(){
var ss = SpreadsheetApp.openById("0AnDrS3YbyWoLdC15aktnMTluUkQ3dWhNTFlyQXBPOFE");
var list = ss.getSheetByName('Form Responses 1');
var values = list.getRange(1,5,ss.getLastRow(),5).getValues();
var gigDates = [];
for (var i in values){
if (values[i][0]!=""){
if (values[i][0] != "Gig Date"){
var ssDate = Utilities.formatDate(values[i][0], "GMT", "M/d/yyyy");
gigDates.push(ssDate);
}
}
}
Logger.log(gigDates);
return gigDates;
}
另一个选择是使用正则表达式(
[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}
)解析返回的字符串,因此您的availible
函数可能类似于: function available(date) {
console.log("From the Spread sheet: " +gigDates);
var dmy = (date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear();
console.log(dmy+' : '+($.inArray(dmy, gigDates.match(/[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}/gmi))));
if ($.inArray(dmy, gigDates) != -1) {
return [true, "","Available"];
} else {
return [false,"","unAvailable"];
}
}
但是,我建议以更正确的方式修复
getDates
。关于javascript - 不确定为什么日期和数组之间的比较找不到匹配项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24498490/