Home > front end >  Exception: Range not found. Trying to place multiple columns using an active cell. (however works wh
Exception: Range not found. Trying to place multiple columns using an active cell. (however works wh

Time:01-08

I'm trying to insert values into cells. Here is my code:

var values = [
              ["test1", "test2", "test3"]
             ];

var ss = SpreadsheetApp.getActiveSpreadsheet()
// var sheet = ss.getSheets()[0]
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()

var cellsToWriteTo = []
function testFunction() {
  var activeCell = sheet.getActiveCell();
  var firstRow = activeCell.getRow()
  var firstColumn = activeCell.getColumn()

  cellsToWriteTo = `["R${firstRow}C${firstColumn}:R${firstRow}C${firstColumn   2}"]`
  console.log(cellsToWriteTo)

  var range = sheet.getRange(cellsToWriteTo)
  range.setValues(values);
}

This gives me the error:

Exception: Range not found

However if I copy and paste cellsToWriteTo from the console log and put it into getRange.. it works perfectly every time..

Things i have tried so far:

Thought it was to do with not fetching the spreadsheet correctly (getActiveSpreadsheet().getActiveSheet()).

Fixed how my values were organised (into arrays within arrays)

Googled the error message and looked at how to properly getRange/setRange in the documentation and in tutorials. Apparently you cannot set arbitrary cells if you are calling them from the excel sheet itself. This is my suspicion as to what is going wrong here. However how can this be the case if when I put in a simple string it functions fine. I am simply doing string interpolation here.

I know this is rudimentary stuff, any help would be appreciated.

CodePudding user response:

Removing both brackets and quotation marks should make it work

Modification:

cellsToWriteTo = `R${firstRow}C${firstColumn}:R${firstRow}C${firstColumn   2}`

Execution:

output

Output:

output2

  •  Tags:  
  • Related