Use a Variable When Setting the getRange(value)

Use a Variable When Setting the getRange(value)

Is it possible to use a captured value inside a getRange(value) code? If so, what do i need to change here? It keeps failing on the last line of code and i have tried a few different options.

function test(row) { 
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();// Define active sheet
  var amountdue = sheet.getRange("A30").getValue();// Capture amount due
  var rownumber = sheet.getRange("A30").getValue();// Capture row number
  sheet.getRange("B30").setValue(amountdue);// Paste amount due
  sheet.getRange("C"&rownumber).setValue(amountdue);// Paste amount due using captured row number
}

1 Answer

try that code:

function test(row) { 
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();// Define active sheet
  var amountdue = sheet.getRange("A30").getValue();// Capture amount due
  var rownumber = sheet.getRange("A30").getRow()// Capture row number
  sheet.getRange("B30").setValue(amountdue);// Paste amount due
  sheet.getRange(rownumber,3).setValue(amountdue);// Paste amount due using captured row number
}

What I corrected:
when you are trying to get the row use the formula getRow().
When you are trying to set the value to a cell with the retrieved row use the formula getRange(Integer row,Integer column) to point the good cell.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.