【发布时间】:2019-12-18 15:31:43
【问题描述】:
我对 Google Script 很陌生,我正在工作中学习。 我有一系列数据作为变量。它只有一列,在这种情况下为 F 列,但值之间有空单元格。我有一个工作脚本(之前从这里得到的),它只循环遍历具有值的单元格。因此,假设 value1 在 F5 中,value2 在 F13 中,它们都是随机的并且总是在变化。
我正在尝试获取这些值的行号,因此脚本应该为 value1 返回“5”,为 value2 返回“13”,最好与值本身一起返回。
到目前为止,这就是我所拥有的,我似乎无法进一步进步。
var sourceID = "sourceID";
var main = SpreadsheetApp.openById("mainID");
var mainsheet = main.getSheetByName("Lab Data");
var sourcesheet = source.getSheetByName("sheet name");
var dataRange = sourcesheet.getDataRange(); // range full sheet
var values = dataRange.getValues(); // values full sheet
var SWrowss = findCellForSW(); // getting start row from another function
var CQrowss = findCellForCQ(); // getting last row from another function
var noRows = CQrowss - SWrowss; // gets number of rows in range
var colss = sourcesheet.getRange(SWrowss,6,noRows,1).getValues(); // range we need, column F
// get rid of empty cells from range - copied script from stack overflow
var cResult = colss.reduce(function(ar, e) {
if (e[0]) ar.push(e[0])
return ar;
}, []);
Logger.log("cResult: " + cResult); // cResult contains all sub headers - no empty cells
// gets element's position in array
for(var b = 0; b < cResult.length; b++){
var position = b+1;
Logger.log("pos " + position);
} // end for
【问题讨论】:
-
你读过
reduce函数吗? -
欢迎来到 StackOverflow。您能否详细说明“我似乎无法进一步进步”的意思?这可能意味着很多不同的事情,所以请具体说明。我是否正确理解您的脚本可以处理第 N 个值的行(例如从 7 到 16 的行),但您需要每个值的实际电子表格行索引?
-
我从这里的用户那里读到了 reduce 函数,它对我有用。我无法继续我的任务,因为我无法检索行号。是的,你是对的!它需要 2 个标题之间的一系列行,具体取决于标题在工作表上的位置,我需要此范围内每个值的实际行索引。
标签: arrays for-loop google-apps-script google-sheets row-number