1
votes

I can't set a variable to return the number to the last row under the K column in google sheets. The code fails on the last line .setValues(exampleText) where I have already defined exampleText as a variable.

I have used different methods of setting variables including setting a variable for a column.

I get the following error:

"Cannot find method getRange(number,number)."

The top three rows are an example I tried before.

Column = 'K' 
range = ss.getRange(LastRow, Column)
range.setValue(exampleText)

var ss = SpreadsheetApp.getActiveSpreadsheet()

var column = 11

var lastRow = ss.getLastRow();

ss.getRange(lastRow, column)
.setValues(exampleText)
1
getRange(row,column) type syntax only accepts numbers NOT strings.TheMaster
You need to review the method signatures. These appear in the autocomplete popup (while writing code in the editor) and have more detail explained on the reference documentation website (use the Help menu).tehhowch
I have edited my code to include column = 11 and replaced k with column but still doesn't work. I don't understand why? @TheMaster I have already checked documentation before posting but don't know how to proceed.Jonathan Harker
May be you would like to find last row with data for a given column ("K" in your case). Correct?Александр Ермолин
Only getRange(string) is available on Spreadsheet(ss). So, you can use ss.getRange('Sheet1!A1:B4'). getRange(number,number) is only available on sheet. ss.getActiveSheet().getRange(1,4)TheMaster

1 Answers

2
votes

Issue:

  • getRange is a method that's available both on Spreadsheet class and Sheet class.

  • However, only getRange(string) is available on Spreadsheet(ss). So, you can use ss.getRange('Sheet1!A1:B4').

  • And getRange(number,number) is only available on sheet. So, You can use ss.getActiveSheet().getRange(1,4).

  • Sheet class also accepts other variations of this method, but Spreadsheet class doesn't, which only accepts string as the only parameter.

  • Sometimes, You also receive

    Exception: The parameters (String,number,number,number) don't match the method signature for SpreadsheetApp.Spreadsheet.getRange

    The reason the first parameter is a string is because there is a attempt by JavaScript engine to match the actual function call signature: getRange(string). It converts the first number to string, but it doesn't know what to do with the rest of the parameters(numbers). As there is no method signature that match the call (string, number,number,number) on Spreadsheet class, it throws a error.

Solution:

Use proper methods on the intended class as described in the official documentation. For most purposes, You must use the Sheet class.

SpreadsheetApp.getActive().getSheets()[0].getRange(1,1)