[英]How can I extract specific values from an array ?
I have this variable : 我有这个变量:
var test = sheet1.getRange(sheet1.getLastRow(),1,1,sheet1.getLastColumn()).getValues();
Which return me the following result : 哪个返回我以下结果:
[[data0, data1, data2, data3, data4, etc...]]
Can I convert this array into an array or array [[data0],[data1],[data2],[data3],etc...]
so I can do something like this test[1][3]
so select data1
and data3
? 我可以将此数组转换为数组或数组[[data0],[data1],[data2],[data3],etc...]
以便执行类似此test[1][3]
因此选择data1
和data3
? Is this the best solution ? 这是最好的解决方案吗? If yes, how can I do this ? 如果是,我该怎么做? If no what's the best solution. 如果没有,什么是最好的解决方案。
Use getDataRange().getValues() to get all the values of the sheet first and assign that to a variable. 使用getDataRange()。getValues()首先获取工作表的所有值,并将其分配给变量。 You can now access that variable like a 2D array. 现在,您可以像二维数组一样访问该变量。 Here's a short snippet to show you. 这是一个简短的片段,向您展示。
function main(){
Logger.log("the value returned was "+ fetchValue(1,1) ); //will fetch the value at index 1,1
}
function fetchValue(row,col) {
var sheet = SpreadsheetApp.openById("SPREADSHEET_ID_HERE");
var data = sheet.getDataRange().getValues();
return data[row][col];
}
The fetchValue
method we've created extracts the value from the specified row,col
index. 我们创建的fetchValue
方法从指定的row,col
索引中提取值。 Check my answer here if you want additional reference. 如果您需要其他参考,请在此处查看我的答案 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.