如何在Google Apps脚本中对数组进行索引? [英] How to index array's in Google Apps Script?

查看:73
本文介绍了如何在Google Apps脚本中对数组进行索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



现在使用javascript,我可以调用任何元素的一个元素。

我正在处理这个项目,并且我想创建一个显示特定列元素的函数。数组

  Array [i] 

但由于某些原因,这似乎不适用于Google Spreadsheets。

  var MyFunction (A){
return A [1];
}

这个函数不会产生任何结果。



EDIt:我解决了它。
我用这种循环的双重索引:

pre $ for(var i = 0; i <37 ; i ++){
if(A [0] [i] var max = A [0] [i] ;
};
};

如果我在电子表格中选择一个行向量,则[0] [0]是第一个元素。
例如,如果我选择A2:A5作为A,则A [0] [0]会给我单元格A2的值!
感谢您的帮助!

解决方案

它不会那么奏效。您必须首先允许访问电子表格,然后通过



然后我们创建一个名为 fetchValue 的函数,它接受2个参数 - 行和列。

我们执行调用 fetchValue 的函数 main

< pre $ function main(){
Logger.log(返回的值是+ fetchValue(1,1)); //返回拉斯维加斯
}

函数fetchValue(row,col){
var sheet = SpreadsheetApp.openById(SPREADSHEET_ID_HERE);
var data = sheet.getDataRange()。getValues();

返回数据[row] [col];
}

然后我们查看Logs是否返回了索引1,1的值是拉斯维加斯





确保您传递给 fetchValue 的行和列在范围内,否则它会返回错误。


I was working on this project and I wanted to make a function that displays an element of a specific column.

Now with javascript I can call any element of an array with

Array[i]

But for some reason this doesn't seem to work in Google Spreadsheets.

 var MyFunction(A) {
 return A[1];
 }

This function yields me nothing.

EDIt: I solved it. I used this loop with a sort of double indexing:

for (var i=0; i < 37; i++) {
if (A[0][i] < max && A[0][i] != 0) {
var max = A[0][i];
};
};

A[0][0] is the first element if I select a row vector in spreadsheet. For example, If I select A2:A5 as A, A[0][0] will give me the value of cell A2! Thanks for the help!

解决方案

It doesn't work that way. You have to first allow access to the spreadsheet then tell Apps Script which Spreadsheet you're working on through openById. After that you can now access the data. Here's a simple code snippet for you. I'm using the standalone mode. There maybe other ways of doing this.

We're going to access this sheet. I've included the index numbers so you can easily understand:

Then we create a function named fetchValue which accepts 2 parameters- the row and the column.

We execute main function which makes calls to fetchValue.

function main(){
   Logger.log("the value returned was "+ fetchValue(1,1) ); //returns las vegas
}

function fetchValue(row,col) {
  var sheet = SpreadsheetApp.openById("SPREADSHEET_ID_HERE");
  var data = sheet.getDataRange().getValues();

   return data[row][col];
}

We then view the Logs if it returned the value of index 1,1 which is las vegas.

Make sure the row and column you pass to fetchValue is within range else it will return errors.

这篇关于如何在Google Apps脚本中对数组进行索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆