如何获取行中所有非空单元格数据-Google表格脚本编辑器 [英] How to grab all non-empty cell data in row - Google Sheets Script Editor
问题描述
我不确定这是否可能,而且老实说,我没有尝试过很多事情,因为我不确定从哪里开始.我正在使用Google表格的脚本编辑器,顺便说一句.我知道有SpreadsheetApp.getRange()和另一个获取值或类似的东西.但是我想要的是具体的.
I'm not sure if this is even possible, and to be quite honest, I haven't tried many things because I wasn't sure where to even start. I'm using the Script Editor from Google Sheets, btw. I know there are SpreadsheetApp.getRange() and another to get the values or something like that. But what I want is a bit specific.
是否可以获取给定行中的所有单元格数据并将其放入数组中?这些行的大小会有所不同,这就是为什么我无法确定确切范围的原因.
Is there a way to grab all the cell data in a given row and put it into an array? The rows will vary in size, that's why I can't do an exact range.
例如,如果我要让行具有以下值:
So for example, if I were to have rows have these values:
abc | 123 | 987 | efg
blah| cat | 654
我希望能够获取这些值并将它们放入[[abc],"123","987," efg]等数组.然后,如果我在下一行运行该函数,d是["blah","cat","654"].
I want to be able to grab those values and place them into an array like ["abc", "123", "987, "efg"]. And then if I run the function on the next row, it'd be ["blah", "cat", "654"].
实际上,只要可以使用定界符,就可以将其放入任何数据类型中.
Actually, it can be placed into any data type as long as there's a delimiter I'd be able to use.
提前谢谢!
推荐答案
这对您有用吗?
当有 abc时|123 |987 |efg
, blah |猫|654
和 abc ||987 |efg
在第1行,第2行和第3行, myFunction(1)
, myFunction(2)
和 myFunction(3)
返回 [abc,123.0,987.0,efg]
, [blah,cat,654.0]
和 [abc,,987.0,efg]
.
When there are abc | 123 | 987 | efg
, blah| cat | 654
and abc | | 987 | efg
at row 1, row 2 and row 3, myFunction(1)
, myFunction(2)
and myFunction(3)
return [abc, 123.0, 987.0, efg]
, [blah, cat, 654.0]
and [abc, , 987.0, efg]
.
function myFunction(row){
var ss = SpreadsheetApp.getActiveSheet();
var values = ss.getRange(row, 1, 1, ss.getLastColumn()).getValues();
var c = 0;
for (var c = values[0].length - 1; c >= 0; c--){
if (values[0][c] != "") break;
}
values[0].splice(c + 1, values[0].length - c - 1);
return values[0];
}
这篇关于如何获取行中所有非空单元格数据-Google表格脚本编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!