Stuck with an error getMonth()

762 views
Skip to first unread message

Gustavo Jair Ortega Quezada

unread,
Jul 24, 2021, 8:19:55 PM7/24/21
to Google Apps Script Community
Hi everyone, I've been trying to write a script that can get a number that is written in a cell on a Spreadsheet and then check on a specific column where I have dates. If the month on this column matches the number on the cell, then I want to count them so that at the end I can have how many times a month appears (there's also another condition in my code but I believe it is irrelevant right now). Problem is, in the line where I create var mes_cont I get an error that mes_cont.getMonth() is not a function and I don't understand why is this happening. Any hint would be appreciated. Anyway, here's my code:

  var mes = hoja.getRange("N10").getValue()
  mes = mes-1;
  switch (mes){
    case 6:
      var cont_resueltos = 0;
          
      for(var i=0; i<datos.length; i++){        
        var mes_cont = hoja.getRange("I"+(i+11)).getValue();
        mes_cont = mes_cont.getMonth();
        if(datos[i][1]=="Resuelto" && mes_cont == mes){
         cont_resueltos++
           Logger.log(cont_resueltos)
          
        
        }
        //var lastName = regExp.exec(string)[1];
        
      }
      hoja.getRange("O10").setValue(cont_resueltos)
    break;
  }

Clark Lind

unread,
Jul 25, 2021, 11:36:55 AM7/25/21
to Google Apps Script Community
Try forcing it into a date format. Something like:

var mes_cont = new Date(hoja.getRange("I"+(i+11)).getValue(), "YYYY-mm-dd"); //replace whatever date format you are using for YYYY-mm-dd

Joshua Snyder

unread,
Jul 26, 2021, 5:57:45 AM7/26/21
to Google Apps Script Community
I would recommend verifying that the cell value is indeed in the format you expect (using a regex) before passing it to the Date constructor.

Passing user input into Date constructors often leads to unexpected errors that are hard to debug :(

CBMServices Web

unread,
Jul 26, 2021, 1:07:12 PM7/26/21
to google-apps-sc...@googlegroups.com
Agreed. Another thing to watch out for is the year entered. Many enter the year with only 2 digits "21" and most forms translate that to year 0021 which the date object does not like because of how it was defined (starting from year 19xx - I forget the specific year).

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/95e54d75-eac1-486d-897f-3defbe468e56n%40googlegroups.com.

Ella Hansen

unread,
Jul 26, 2021, 2:20:07 PM7/26/21
to Google Apps Script Community
Everything that comes out of a sheet is going to be a string, even if it's formatted as a date in the sheet. So as cwl said, you'll need to create a Date object out of the string first, otherwise .getMonth() won't work because the String prototype doesn't have a .getMonth() method.

Here's some documentation of the JS Date() constructor: 

Here are the formats it'll take as input:

new Date()
new Date(value)
new Date(dateString)

new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)

So you could do something like this:

var mes_texto = hoja.getRange("I"+(i+11)).getValue();
var mes_fecha = new Date(mes_texto);
mes_cont = mes_fecha.getMonth();

Happy coding!


Ella Hansen

unread,
Jul 26, 2021, 2:20:41 PM7/26/21
to Google Apps Script Community

Ella Hansen

unread,
Jul 26, 2021, 2:23:56 PM7/26/21
to google-apps-sc...@googlegroups.com
Also, .getMonth() returns a number in the range of 0-11. So if you're expecting January to be 1 and December to be 12, you'll need to add 1 to the .getMonth() variable.

Ella Hansen // they, them, theirs

Schedule a meeting:

remote desktop session

video call
onsite visit

Tech support:

415.854.0881
sup...@ignitionit.com
Luvvv us? Yelp us! ❤️



You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/bbwDgWwEgHw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/6f86c0c4-db6f-4497-8dd0-ffcef36e1bccn%40googlegroups.com.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages