You're running this in the debugger with NO parameters. If you want to do that, you need to ensure you have appropriate default values defined before using the parameters, otherwise the interpreter will start making guesses.
This would fix things for you, by providing defaults if parameters are empty:
function fillLine(row, column, length, bgcolor)
{
row = row || 0;
column = column || 0;
length = length || 1;
bgcolor = bgcolor || "red";
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1+row, 1+column, 1, length).setBackground(bgcolor)
}
Alternatively, you could use a test function like this, and call it from the debugger:
function test_fillLine() {
fillLine(0, 0, 1, 'red');
}
There are other approaches, some described in Is there a better way to do optional function parameters in Javascript?
So what's going on?
Javascript parameters are not explicitly typed. Therefore, the interpreter must examine passed objects at run time to determine their type.
In this example, with no parameters passed, all are set to the undefined
value, with the 'Undefined' type.
When a function is invoked, the interpreter needs to determine which of sometimes several signatures applies. (Sheet.getRange
has 4 variants.) To do that, it must resolve the number and types of the parameters provided.
So, here we go...
1+row
, and 1+column
are identified as numbers, because the literal "1" is a number.
1
is clearly a number
length
is an Undefined Object, and without any other objects to provide hints, the interpreter thinks it is a plain class
.
Therefore, the interpreter decides it needs to find a getRange
method of the sheet
object with a signature number, number, number, class
. It doesn't find one, and throws the exception you see.