How to declare a variable range to a spreadsheet i

2019-08-18 03:22发布

问题:

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 following code works solved by @TheMaster

var ss = SpreadsheetApp.getActiveSpreadsheet()

var column = 11

var lastRow = ss.getLastRow();

range = ss.getActiveSheet().getRange(lastRow,column);
range.setValue(exampletext)

回答1:

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.