0

This returns a date Logger.log(sht.getRange(1,1).getValue()) How do I get the day of the month for that cell?

I tried Logger.log(sht.getRange(1,1).getValue()).date() and Logger.log(date(sht.getRange(1,1).getValue())) neither worked

cweedon
  • 3
  • 2
  • 1
    Unfortunately, I cannot know whether the cell value of `sht.getRange(1,1)` is the date object from your question. So, if `sht.getRange(1,1).getValue()` is the date object, I think that when you want to retrieve the date, your script can be modified to `Logger.log(sht.getRange(1,1).getValue().getDate())`. How about this? – Tanaike Aug 28 '23 at 00:29
  • Does this answer your question? [How to get year/month/day from a date object?](https://stackoverflow.com/questions/2013255/how-to-get-year-month-day-from-a-date-object) – Wicket Aug 28 '23 at 01:27
  • Thanks Tanaike The cell is formated as a date and sht.getRange(1,1).getValue() does return the date – cweedon Aug 28 '23 at 11:49
  • Thank you for replying. I have to apologize for my poor English skill. Unfortunately, from `Thanks Tanaike The cell is formated as a date and sht.getRange(1,1).getValue() does return the date`, I cannot understand whether your issue was resolved by my comment. Can I ask you about the details of it? – Tanaike Aug 28 '23 at 12:02
  • function myFunction() { var ss = SpreadsheetApp.getActive(); var sht = ss.getActiveSheet(); var row = 2; Logger.log(sht.getRange(row, 1).getValue().getDate()) while (sht.getRange(row, 1).getValue().getDate() !== 1) { row++ }; } I want to skip lines where the date is not the first day of the week. In the code above the Logger line works well, but the second line kicks the following TypeError: sht.getRange(...).getValue(...).getDate is not a function. ?? – cweedon Aug 28 '23 at 21:26
  • Hi! Welcome to StackOverflow. Would you be able to provide a copy of your sample sheet (with your initial output), so that we can further help you with your issue? – Mic Test Aug 28 '23 at 21:48
  • Sorry in my last post I meant to say "first day of the month" – cweedon Aug 28 '23 at 22:38
  • So basically what you would want to happen is that you have a large list of dates, and you would only want to filter all dates on the FIRST day, of every MONTH, is that correct? – Century Tuna Aug 28 '23 at 22:44
  • Here is a link to the google sheet https://docs.google.com/spreadsheets/d/16BHj7ygqqgw2dqRehC3p7b4yMzs2UnJ0Uhq-_3z4bwk/edit?usp=sharing – cweedon Aug 28 '23 at 23:14
  • Century Tuna I am trying to hide all the rows before the first day of a month and all the days after the end of a month. I have done this another way but I am trying to learn about how to correctly use this getDate function as part of making my whole sheet cleaner. – cweedon Aug 28 '23 at 23:21
  • So just to clarify, basing off on your spreadsheet, you want to hide all the rows on the sheet before September 1st, and then once that date arrives, hide all the rows after it? So all the rows that contain August will be hidden, then the rows for September will be shown. – Mic Test Aug 28 '23 at 23:40

1 Answers1

0

ALTERNATE SOLUTION

Using your sample spreadsheet as a guideline, and based on one of your comments:

I am trying to hide all the rows before the first day of a month and all the days after the end of a month. I have done this another way but I am trying to learn about how to correctly use this getDate function as part of making my whole sheet cleaner.

You can try to follow this:

  1. First, I've created separate functions for hiding and showing rows depending on the day of the month. One thing that I noticed on your sample sheet is that, for weekdays there are 4 rows populated for different times, while on weekends there are 6 rows populated.
  2. As such, I've taken those into consideration in creating the two functions which executes by using the hideRows() and showRows() function, respectively.
  3. Now, we can go to the main function, where the other functions will be called to perform the hiding (or unhiding) of rows. For this, I've also used a separate date variable, which calls the current date to compare with the other dates on the spreadsheet, and check if they are to be shown or hidden.

You can refer to the following scripts for your reference:

  • The two functions for hiding and showing rows:

    function hideRows(date, index) {
      var ss = SpreadsheetApp.getActive();
      var sht = ss.getActiveSheet();
      // checks if the date is a weekend
      if (date[index].getDay() == 0 || date[index].getDay() == 6) {
        sht.hideRows(index + 2, 6);   // you can change the 2nd integer depending on the number of rows populated on weekends
        return index += 5;
      }
      // if the date is a weekday
      else {
        sht.hideRows(index + 2, 4);   // you can change the 2nd integer depending on the number of rows populated on weekdays
        return index += 3;
      }
    }
    
    function unhideRows(date, index) {
      var ss = SpreadsheetApp.getActive();
      var sht = ss.getActiveSheet();
      // checks if the date is a weekend
      if (date[index].getDay() == 0 || date[index].getDay() == 6) {
        sht.showRows(index + 2, 6);   // you can change the 2nd integer depending on the number of rows populated on weekends
        return index += 5;
      }
      // if the date is a weekday
      else {
        sht.showRows(index + 2, 4);   // you can change the 2nd integer depending on the number of rows populated on weekdays
        return index += 3;
      }
    }
    
  • The main function:

    function myFunction() {
      var ss = SpreadsheetApp.getActive();
      var sht = ss.getActiveSheet();
      var row = 2;
      var date = sht.getRange(row, 1, sht.getLastRow() - (row - 1), 1).getValues().flat();
      var today = new Date();
    
      for (i = 0; i < date.length; i++) {
        // a ternary operator that checks if 
        // a) the date on the spreadsheet is in the same month as today's date (to show the rows), or;
        // b) hide all rows before the 1st date of the next month, or dates after the end of the previous month
        date[i] != "" ? date[i].getMonth() == today.getMonth() || (date[i].getMonth() == today.getMonth() + 1 && date[i].getDate() == 1) ? i = unhideRows(date, i) : i = hideRows(date, i) : false;
      }
    }
    

For the ternary operator, its logic is similar to this if-else statement:

if (date[i] != "") {
    if(date[i].getMonth() == today.getMonth() || (date[i].getMonth() == today.getMonth()+1 && date[i].getDate() == 1)){
      unhideRows(date,i);
    }
    else{
      hideRows(date,i);
    }
  }

REFERENCES

Mic Test
  • 106
  • 5
  • Let me know if this answers the issue addressed on one of your comments, or if there are some clarifications needed so that I can adjust my answer accordingly. – Mic Test Aug 29 '23 at 02:02