2

Just want to create simple filler to use it in other functions:

function fillLine(row, column, length, bgcolor)
{
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1+row, 1+column, 1, length).setBackground(bgcolor)
}

But getting: "Cannot find method getRange(number,number,number,(class))".

Is it possible to create one?

added 23.10.2014 23:32 GMT+2

I was trying to run function itself. Option "fillLine" in run menu was checked. When I change it to my main function "onOpen" everything works. For else functions I can run them even without arguments, but not for this one. Maybe someone can explain?

ximik
  • 21
  • 1
  • 3
  • Your function should work, it looks like you are passing an invalid value for length. Length should be a number. – Cameron Roberts Oct 23 '14 at 18:52
  • The problem is I need to send `lenght` as agument. But even when I'm changing `lenght` to constant, for example `5` getting new error: "Can't convert NaN to (class)" @cameron-roberts – ximik Oct 23 '14 at 19:19
  • This is definitely a problem with the values you are passing to fillLine(), either row or column is also not a number. If you call fillLine(1,1,10,'red'); it works. – Cameron Roberts Oct 23 '14 at 19:27
  • `sheet.getRange(1, 1, 1, 1).setBackground(bgcolor)` works great. Everything else with `row column length` no. – ximik Oct 23 '14 at 19:27

3 Answers3

4

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.

Community
  • 1
  • 1
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • Thanks a lot for so detailed answer. I'm beginer in G-app-script and java. So such complete explanations is very helpfull for me. Thanks! @mogsdad – ximik Oct 23 '14 at 21:26
1

I succeeded by using offset.

var sheet = SpreadsheetApp.getActiveSheet()
//var range = sheet.getRange(row, column, height, width)
var range = sheet.getRange('A1').offset(row - 1, column - 1, height, width)

So in your case, the following code may run.

function fillLine(row, column, length, bgcolor) {
  var sheet = SpreadsheetApp.getActiveSheet()
  //var range = sheet.getRange(row, column, 1, length)
  var range = sheet.getRange('A1').offset(row - 1, column - 1, 1, length)
  range.setBackground(bgcolor)
}

In my case, I need to add permission to use setBackground. A popup menu to add the permission appeared in a script editor when I run a test script as @Mogsdad said.

function test_fillLine() {
  fillLine(1, 1, 2, 'red');
}

enter image description here

https://developers.google.com/apps-script/guides/services/authorization

Community
  • 1
  • 1
asukiaaa
  • 1,594
  • 17
  • 19
  • Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. – SuperBiasedMan Dec 02 '15 at 14:26
  • Thank for your advice. I improved my answer. – asukiaaa Dec 03 '15 at 02:47
0

You have to go through the activeSpreadsheet, where the activeSheet is "embedded" in, i. e.

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();

intead of

var sheet = SpreadsheetApp.getActiveSheet();
franz
  • 1
  • 1