Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Exception: Range not found. Trying to place multiple columns using an active cell. (however works when manually entered)

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..

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading