首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google Apps脚本-2维变量

Google Apps脚本-2维变量
EN

Stack Overflow用户
提问于 2019-09-28 03:26:08
回答 1查看 66关注 0票数 0

我真的不明白,如何在Google Apps脚本中创建二维变量/数组。下面的代码提供了相同的结果(12),即使变量不同。我做错了什么?

代码语言:javascript
复制
function test(){
var Latitude = [20,20];

Latitude[1,2] = 15;
Latitude[5,2] = 12;

SpreadsheetApp.getActiveSpreadsheet().getRange('A1').setValue(Latitude[5,2]);
SpreadsheetApp.getActiveSpreadsheet().getRange('B1').setValue(Latitude[1,2]);
}
EN

回答 1

Stack Overflow用户

发布于 2019-09-28 04:10:45

索引二维数组是嵌套在数组中的索引数组:内部数组是行:内部数组的索引是列值:可以使用1

  • Array:row1 = arr[0]; row1Col1 = arr[0][0]

  • Comma operator in JavaScript计算每个表达式并返回最后一个表达式的值。

这段代码演示了发生的事情:

代码语言:javascript
复制
function test() {
  var Latitude = [20, 20]; //1D array

  Latitude[(1, 2)] = 15; //([1,2]) is evaluated as [2],the third element
  console.log('Latitude is now:'); //[20,20,15]
  console.log(Latitude);
  Latitude[(5, 2)] = 12; //Again setting [2] as 12
  console.log('Latitude is now:'); //[20,20,12]
  console.log(Latitude);
  console.info('Latitude[18,1] is:' + Latitude[(18, 1)]); //=Latitude[1]
  console.info('Latitude[18000,1] is:' + Latitude[(18000, 1)]); //=Latitude[1]

  /** 2D Array example */
  console.info('2D ARRAY:');
  var arr2d = [[1, 2], [3, 4]];
  console.info('arr2d === ' + JSON.stringify(arr2d));
  arr2d.map(function(row, i) {
    console.info('Row' + i + ' is array: ' + JSON.stringify(row)); //=arr[i]
    row.map(function(column, j) {
      console.info('Column is: ' + column); //=arr[i][j]
      console.info('arr2d[' + i + '][' + j + '] is: ' + arr2d[i][j]);
    });
  });
}
test();

要练习,请执行以下操作:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58140421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档