简体   繁体   English

如何从数组中提取特定值?

[英]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]因此选择data1data3 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.

相关问题 如何从对象数组中提取值 - How can I extract values from an array of objects 如何从 JavaScript 中的嵌套数组对象中提取特定属性作为数组 - How can I extract specific property as an array from nested array object in JavaScript 如何从 object 中提取值并将其返回到具有特定要求的字符串数组中 - How to extract values from an object and return it in an array of strings with specific requirements 如何从 axios 响应对象中的所有项目中提取特定值到 aa vuex 状态 - How can I extract specific values from all items in axios response object to a a vuex state 从多维数组JavaScript中提取特定值 - Extract specific values from multidimensional array javascript 我可以使用过滤器从对象数组中提取值吗? - Can I use filter to extract values from an array of objects? 如何从数组中的多个对象中准确提取 2 个键和值 - How can I extract exactly 2 keys and values from several objects in an array 如何提取和配对基于数组的 object 的值 - How can I extract and pair the values of an array based object 如何通过从单独的数组中匹配的字符串对象值从 3 个嵌套的 json 数组中提取对象值,然后在 html/javascript 中显示它 - How can I extract object values from 3 nested json arrays by matched string object values from a separate array and then display it in html/javascript 如何从 html 中提取特定元素 - how can i extract a specific element from the html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM