2

From the html I made with date picker, if a date was selected and submitted, it's output will be saved in the Google Sheet.

Here is the sample output:

result of the datepicker

here is the html code:

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

and here is the datePicker sample:

As you have noticed there are some disabled dates in the calendar. It is due to the option in the following java script:

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

I wanted to disable the repeating dates in the google sheet if it reaches 5 times in the column. In the example output above, you will notice:

August 20, 2019
July 26, 2019
July 19, 2019

Exist 5 times in the column. Now, to get only the values which exist 5 times, I used the code which I got from @Christopher Bradley

Google Apps 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);
 Logger.log(dateDisable);
 return dateDisable;
 }

the log of this code is:

I want to disable the dates of the following log/ result. And to disable it, I think I need to place it in the disabledDays array in the javascript. I used

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

But still I can't disable the dates. I thought it should be in the format of

new Date("2019, 12, 25").valueOf()

and @Rubén gave this code:

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

since it resulted in an error I tried to make this:

var testDate = Utilities.formatDate(new Date(dateDisable[i]), "GMT+8","yyyy, MM, dd");

and logging it the result is:

Still, I can't disable the date in the datepicker.

Rubén
  • 34,714
  • 9
  • 70
  • 166
GPni
  • 143
  • 1
  • 14
  • I apologize for my poor English skill. Can I ask you about your goal? You want to disable the dates of weekends and 19, 20 and 26. Is my understanding correct? – Tanaike Jul 22 '19 at 05:41
  • @Tanaike, I want to disable the dates of the weekends and all the dates in my google sheet which reached 5 times. Sorry I'm a newbie in programming. Disabling the weekend is not an issue, the problem is disabling the dates in the date picker if the dates in my google spreadsheet reached 5 times. – GPni Jul 22 '19 at 05:50
  • 1
    Thank you for replying. I could understand that you want to disable the dates retrieved from your Spreadsheet. If my understanding is correct, in order to understand about `all the dates in my google sheet which reached 5 times`, can you provide a sample Spreadsheet? Of course, please remove your personal information. – Tanaike Jul 22 '19 at 05:51
  • please check if it's accessible: https://docs.google.com/spreadsheets/d/1lEfzjG1zzJVPMN8r-OpeZm6q9_IqSwk9DNCEY-q7ozY/edit#gid=0 – GPni Jul 22 '19 at 05:54
  • Thank you. But unfortunately, I cannot see your sample Spreadsheet. – Tanaike Jul 22 '19 at 05:57
  • is it okay if I send a screen shot? or you can click the link above for the screen shot of the column of dates. the one with sample output: "result of the datepicker" – GPni Jul 22 '19 at 05:58
  • At your shared Spreadsheet, you are required to permit for accessing it. So I sent it and am waiting for your response. – Tanaike Jul 22 '19 at 06:00
  • I set a viewing access. Thank you. – GPni Jul 22 '19 at 06:04
  • Thank you for your response. I could open it. When your script is run for the shared Spreadsheet, July 19, 2019, August 20, 2019 and July 26, 2019 are retrieved. You want to disable those 3 days for the datepicker. Is my understanding correct? – Tanaike Jul 22 '19 at 06:08
  • @Tanaike, yes. those dates and other dates which will reach 5 times. – GPni Jul 22 '19 at 06:11
  • Can I ask you about `other dates which will reach 5 times`? Because in your Spreadsheet, there are only 3 values of July 19, 2019, August 20, 2019 and July 26, 2019. – Tanaike Jul 22 '19 at 06:11
  • In my spreadsheet there are 4 dates. August 20, 2019, July 26, 2019, July 19, 2019, July 17, 2019. Meaning there will be added dates as the application is being used. that's why the google script select the dates in the spreadsheet which appeared 5 times. – GPni Jul 22 '19 at 06:15
  • Thank you for replying. I think that I could understand about it. – Tanaike Jul 22 '19 at 06:17
  • Thank you. I hope you can help me on my problem. – GPni Jul 22 '19 at 06:26
  • 1
    I proposed a modified script as an answer. Could you please confirm it? If this modification didn't resolve your issue, I apologize. At that time, can you provide the whole script for completely replicating the issue. By this, I would like to confirm it. – Tanaike Jul 22 '19 at 06:53
  • It solved my problem. Thank you very very much. – GPni Jul 22 '19 at 07:30
  • Thank you for replying. I'm glad your issue was resolved. Thank you, too. – Tanaike Jul 22 '19 at 07:58

1 Answers1

2
  • You want to disable the dates of weekends and the dates retrieved from the values of Spreadsheet for the datepicker.
    • In your sample Spreadsheet, you want to disable August 20, 2019, July 26, 2019, July 19, 2019 and the weekends.

If my understanding is correct, how about this modification? Please think of this as just one of several answers.

Modified script:

Please modify the function of populateDates() of HTML & Javascript side as follows.

function populateDates(disabledDays){
  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){ // Modified
      return disabledDays.some(e => {
        var obj = new Date(e);
        return obj.getFullYear() == day.getFullYear() && obj.getMonth() == day.getMonth() && obj.getDate() == day.getDate();
      });
    }
  });
}

Note:

  • In this case, the values of disabledDays from revealDates() of Google Apps Script are the string values. So the string values are converted to the date object at the script of disabledDays = disabledDays.map(e => new Date(e)).
  • In this modification, I didn't modify Google Apps Script.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165