简体   繁体   中英

How to refer to a specific row in Google Sheets Scripts using a variable?

In my script, I need to refer to a specific row by using the number in a variable. For example, if the variable "rownumber" is 21, I want to have this code line:

var final_row = target_sheet.getRange('21:21');

You can use the getRange(row, column, numRows, numColumns) version of getRange :

var rownumber = 21;
var final_row = target_sheet.getRange(rownumber,1,1,target_sheet.getMaxColumns());

getMaxColumns() will give you the full row until the last column of your sheet, starting from the first column.

Alternatively, you can use getLastColumn() to get from the first column until the last column with content:

var rownumber = 21;
var final_row = target_sheet.getRange(rownumber,1,1,target_sheet.getLastColumn());

There are multiple ways to do this:

  1. Template literals (template string)
var final_row = target_sheet.getRange(`${rownumber}:${rownumber}`);
  1. Addition operator +
var final_row = target_sheet.getRange(rownumber + ':' + rownumber);
  1. Array.prototype.join()
var final_row = target_sheet.getRange([rownumber, rownumber].join(':');

Resources

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM