简体   繁体   English

如何使用变量引用 Google 表格脚本中的特定行?

[英]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:例如,如果变量"rownumber"是 21,我想要这个代码行:

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

You can use the getRange(row, column, numRows, numColumns) version of getRange :您可以使用 getRange 的getRange(row, column, numRows, numColumns) 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. getMaxColumns()将为您提供整行,直到工作表的最后一列,从第一列开始。

Alternatively, you can use getLastColumn() to get from the first column until the last column with content:或者,您可以使用getLastColumn()从第一列到最后一列的内容:

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资源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google表格脚本-如何引用给定行的单元格? - Google sheets scripts--how to refer to a cell given a row? Google脚本/表格:如何基于值汇总行/对象? - Google Scripts/Sheets: How to aggregate row/objects based on values? Google Sheets Apps Scripts如何在另一个指定列的最后一个非空单元格的同一行中填充特定文本 - Google Sheets Apps Scripts how to populate specific text in the same row of the last non-empty cell in another specified column 如果行包含特定文本,则构建平均google应用脚本(google表格)(Javascript) - if row contains specific text then build average google apps scripts (google sheets) (Javascript) 如何使用脚本在 Google 表格中更新单元格时添加时间戳? - How to time stamp when a cell was updated in Google Sheets using scripts? 如何在 Google Apps 脚本中引用数组中的元素 - How to refer to element in array in Google apps scripts 如何遍历范围,获取特定单词并将其粘贴到相邻列,直到使用脚本在 Google 表格中找到下一个关键字? - How to iterate over a range, get a specific word and paste it to an adjacent column until next key word is found in Google Sheets using scripts? 使用脚本在Google表格中重新定位光标 - Relocating the cursor in Google Sheets using scripts 根据使用 Google Sheets Apps Scripts 在未知数量的行中过滤关键字来移动特定行 - Move Specific Rows depending on Filtering Keywords within unknown amount of rows using Google Sheets Apps Scripts 如何在Google表格脚本中比较时间? - How to compare time in google sheets scripts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM