Javascript 使用谷歌应用程序脚本获取电子表格中一列的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14991030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get value in one column in spreadsheet using google apps script
提问by Hashim Adel
I want to get a string value -to compare it later on with an if condition- from only one column in spreadsheet using Google apps script. I searched the internet and I found this link - sorry if this sounds stupid, I am new to Google apps scripts - https://developers.google.com/apps-script/class_spreadsheet
我想使用 Google 应用程序脚本仅从电子表格中的一列获取字符串值 - 稍后将其与 if 条件进行比较。我在互联网上搜索并找到了此链接 - 抱歉,如果这听起来很愚蠢,我是 Google 应用程序脚本的新手 - https://developers.google.com/apps-script/class_spreadsheet
var values = SpreadsheetApp.getActiveSheet().getRange(2, 3, 6, 4).getValues();
I guess that must be helpful, the only problem is that the column I want to get the values from is dynamic so how do I set the range of this column
我想这一定有帮助,唯一的问题是我想从中获取值的列是动态的,所以我如何设置此列的范围
回答by Serge insas
if you use simply :
如果你简单地使用:
var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues()
var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues()
You will get a 2 Dimension array of all the data in the sheet indexed by rows and columns.
您将获得按行和列索引的工作表中所有数据的二维数组。
So to get the value in column A, row1 you use values[0][0], values[1][0]for columnA, row 2, values[0][2]for column C row1, etc...
因此,要获取 A 列中的值,您使用的 row1 values[0][0],values[1][0]对于 columnA,第 2 行,values[0][2]对于列 C row1,等等...
If you need to iterate in a for loop (in a single column) :
如果您需要在 for 循环中迭代(在单列中):
for(n=0;n<values.length;++n){
var cell = values[n][x] ; // x is the index of the column starting from 0
}
If you need to iterate in a for loop (in a single row) :
如果您需要在 for 循环中迭代(在单行中):
for(n=0;n<values[0].length;++n){
var cell = values[x][n] ; // x is the index of the row starting from 0
}
回答by shakee93
much easier way to loop through the rows and get a column value.. hope that helps
循环遍历行并获取列值的更简单方法..希望有帮助
var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
values.forEach( function(row) {
row[4] // column index as 4
});
回答by gg-edu
Here is the script I use to get the values in a dynamic column:
这是我用来获取动态列中的值的脚本:
var SS = SpreadsheetApp.getActiveSheet()
var Avals = SS.getRange("A1:A").getValues();
var numberOfValues = Avals.filter(String).length;
var RangeVals = SS.getRange(1,1,numberOfValues).getValues();
I've never had to change which starting row based based on a dynamic changing starting point though. Would be interested in seeing how that would be done.
不过,我从来没有必要根据动态变化的起点来更改起始行。有兴趣看看这将如何完成。

