简体   繁体   English

将Google脚本值传递给JavaScript Datepicker

[英]Passing the google script values to javascript datepicker

I have an array of dates which was the result of the function from google app script. 我有一个日期数组,它是Google应用程序脚本功能的结果。 I want to place the date results in the disabledDays variable in JavaScript which was for the datepicker which I got from MaterializeCSS. 我想将日期结果放在JavaScript中的disabledDays变量中,该变量用于从MaterializeCSS获得的datepicker

I have set the initialization of the datepicker in the JavaScript and used the code which Christopher Bradley help me to get the dates which will be disabled. 我已经在JavaScript中设置了datepicker的初始化,并使用了Christopher Bradley帮助我获取将被禁用的日期的代码。 (Related Return the values that occurred multiple times using google apps script ) (相关返回使用Google Apps脚本多次出现的值

I have tried to the following codes but still not disabling the dates in the datepicker . 我尝试了以下代码,但仍未禁用datepicker中的datepicker

html: HTML:

 <div class="row">
 <div class="input-field col s4">
         <input id="subDate" type="text" class="datepicker">
         <label for="subDate">Select Date</label>
 </div> 

This is my initialization for the date picker. 这是我对日期选择器的初始化。

javascript: JavaScript的:

<script>

 document.addEventListener('DOMContentLoaded', function() {
   var timeSelect = document.querySelectorAll('select');
   M.FormSelect.init(timeSelect);

   google.script.run.withSuccessHandler(populateDates).revealDates();                            

  });

   function populateDates(disabledDays){
   var disabledDays = [new Date("2019, 12, 25").valueOf(), new Date("2019, 7, 18").valueOf()];
   var dateSelect = document.getElementById('subDate');
   M.Datepicker.init(dateSelect, {
                        minDate: new Date ("2019, 5, 10"), 
                        maxDate: new Date ("2019, 8, 21"), 
                        disableWeekends: true,
                        disableDayFn: function(day){
                           return disabledDays.indexOf(day.valueOf()) > -1;
                        }
                        });

   }
</script>

then here is the google script function which I got from Christopher Bradley. 这是我从克里斯托弗·布拉德利(Christopher Bradley)获得的Google脚本功能。

google script: 谷歌脚本:

function revealDates(){

  var ss = SpreadsheetApp.openByUrl(url);
  var ws = ss.getSheetByName("Test_Data");
  var dateRg = ws.getRange(1, 9, ws.getLastRow(), 1).getValues();

  var CheckLimitReached = function (T)
  {
   var records= {};
   T.forEach(function (x) { records[x] = (records[x] || 0) + 1; });
   var limit_reached = Object.keys(records).filter(function (R) {
   return records[R] >= 5;});
   return limit_reached;
  };

 var dateDisable = CheckLimitReached(dateRg);
 var testDate = Utilities.formatDate(new Date (dateDisable.valueOf()), "GMT+8","yyyy, MM, dd");
 Logger.log(testDate);
 //return dateDisable;

} }

this is the result of the log: 这是日志的结果:

 [19-07-22 10:26:49:100 HKT] 1970, 01, 01

I want to send the dates which is the result (see log) from the Google Script to the disabledDays array in the JavaScript to disable the date selection in the datepicker . 我想将Google脚本的结果(请参阅日志)发送到JavaScript中的disabledDays数组,以禁用datepicker中的datepicker I'am really in need of help. 我真的需要帮助。 I've been trying to search and figure out what to do but no success. 我一直在尝试搜索并弄清楚该怎么做,但没有成功。 I'll really appreciate all the help. 我将非常感谢所有帮助。

I think that you should take some steps back, even maybe you should start from scratch as it looks that you still doesn't understand some Google Apps Script / JavaScript basic concepts like data types and how the Date constructor works. 我认为您应该退后一步,甚至可能您应该从头开始,因为您似乎仍然不了解某些Google Apps Script / JavaScript基本概念,例如数据类型以及Date构造函数的工作原理。

The Date constructor, new Date() , could have an argument but it should not be an Array object. Date构造函数new Date()可以有一个参数,但不应是Array对象。 In the expression new Date (dateDisable.valueOf()) you are passing one. 在表达式new Date (dateDisable.valueOf())您传递了一个。 For details about the valid arguments for the Date constructor see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date 有关Date构造函数的有效参数的详细信息,请参见https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date

Assuming that dateDisable members are Date objects, in order to make your code log dates as strings with the required format, replace: 假设dateDisable成员是Date对象,为了使您的代码日志日期成为具有所需格式的字符串,请替换:

var testDate = Utilities.formatDate(new Date (dateDisable.valueOf()), "GMT+8","yyyy, MM, dd");
 Logger.log(testDate);

by: 通过:

for(var i = 0; i < dateDisable.length; i++){
  var testDate = Utilities.formatDate(dateDisable[i], "GMT+8","yyyy, MM, dd");
  Logger.log(testDate);
}


Since you input is use type="text" your Google Apps Script should pass the date as a text string with the following format YYYY-MM-dd . 由于您使用type="text"输入,因此您的Google Apps脚本应将日期作为文本字符串传递,格式YYYY-MM-dd

For this you could use Utilities.formatDate(date,timezone,format) 为此,您可以使用Utilities.formatDate(date,timezone,format)

Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM