Search code examples
google-apps-scriptgoogle-sheetscustom-function

Google App Script - Why can I not use methods for an input?


I am trying to create a custom function for Google Spreadsheets, but I'm encountering a problem. This piece of Google App Script code returns the error:

TypeError: Cannot read property "length" from undefined. (line 2, file "Code")

function FOO(input) {
      var x = input.length
      return x
    }

Can anyone tell me what is wrong?

The following works and throws no errors:

function FOO(input) { 
  return input

Also, none of the methods for input works, such as input.map, input.values, etc.


Solution

  • In a spreadsheet, =FOO(A1) returns 1 and =FOO(A1:A5) returns 5. So that's working correctly.

    enter image description here

    In the script editor, if you simply run FOO(), then you will receive an error because you didn't pass in the input. You need to create a test function and run it instead.

    function test_FOO() {
      var input = [1];
      Logger.log(FOO(input));
    }
    

    enter image description here