Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot find function includes in object

I have very little experience using Google Script but I was attempting to use it to search through one column of a spreadsheet and find all instances of the string "Film Dub" (knowing that there can be only one per cell).

Below is my code:

    function filmDub() {
      var sheet = SpreadsheetApp.getActiveSheet();
      var data = sheet.getDataRange().getValues();
      for (var i = 1; i < 100; i++) {
        var s = data[i][2].toString();
        if (s.includes('Film Dub')) {
          data[5][13]++;
        }
      }
    }

However I keep receiving the error

TypeError: Cannot find function includes in object Let's Make A Date, Film Dub, Three Headed Broadway Star, Film TV Theater Styles, Greatest Hits, World's Worst. (line 6, file "Code")

"Let's Make A Date, Film Dub, Three Headed Broadway Star, Film TV Theater Styles, Greatest Hits, World's Worst" is the correct content of data[i][2] so it is getting correct information from the spreadsheet. I have used the debugger in Google Script Editor to verify that s is a string (this was one of the solutions to similar questions here on Stack Overflow) but that didn't fix my problem. What else could be wrong with it?

like image 914
bookworm144 Avatar asked Sep 23 '16 20:09

bookworm144


People also ask

How do I fix the “includes is not a function” error?

The "includes is not a function" error occurs when the includes () method is called on a value that is not of type string or array. To solve the error, convert the value to a string or array before calling the method or make sure to only call the includes method on strings or arrays. Here is an example of how the error occurs.

Why Apigee says cannot find function includes in object?

Apigee says " Cannot find function includes in object ", this is due to any error in the code or this is specify to apigee. Also whenever we try to use negation (!) or not equal to (!=) operators in the logic, we are not getting expected results. Also I am new to javascript.

How do I fix the error when calling the includes method?

We called the includes method on a number which caused the error. The includes method is only supported on strings or arrays. To solve the error, convert the value to a string or an array before calling the method, or only call the method if the value is of the correct type.


1 Answers

You should use indexOf with a string to check for the existence of a text block.

function filmDub() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  for (var i = 1; i < 100; i++) {
    var s = data[i][2].toString();
    if (s.indexOf('Film Dub') !== -1) {
      data[5][13]++;
    }
  }
}
like image 148
Amit Agarwal Avatar answered Oct 11 '22 20:10

Amit Agarwal