为什么 Google 脚本对于行中的范围和列中的范围的每次运行都不同 [英] Why does Google script for each run differently for range in a row vs range in a column

查看:15
本文介绍了为什么 Google 脚本对于行中的范围和列中的范围的每次运行都不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据行中的值创建选项卡列表,就像我之前对列中的值所做的一样(这里的脚本是一个示例测试,因为我试图确定问题,而不是实际的脚本用过的).但是, forEach() 函数的工作方式不同,我不明白为什么.下面我将附上两组脚本及其结果.

I am trying to create a list of tabs based on values in a row, as I have previously done with values in a column (The script here is an example test as I was trying to identify the issue, not the actual script used). However, the forEach() function is working differently and I do not understand why. Below I will append two sets of scripts and their results.

对于列:

  var app = SpreadsheetApp;
  var ss = app.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var range = sheet.getRange(1,1,5,1);
  var STlist = [];

  range.getValues().forEach(function(x){
    Logger.log(x + " why");
    STlist.push(x);
  });

  Logger.log(STlist);  
}

列输出

[20-02-28 12:10:58:012 HKT] z why
[20-02-28 12:10:58:018 HKT] a why
[20-02-28 12:10:58:021 HKT] b why
[20-02-28 12:10:58:023 HKT] c why
[20-02-28 12:10:58:026 HKT] d why
[20-02-28 12:10:58:029 HKT] [[z], [a], [b], [c], [d]]

对于行

  var app = SpreadsheetApp;
  var ss = app.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var range = sheet.getRange(1,1,1,5);
  var STlist = [];

  range.getValues().forEach(function(x){
    Logger.log(x + " why");
    STlist.push(x);
  });

  Logger.log(STlist);  
}

行输出

[20-02-28 12:11:35:013 HKT] z,x,d,v,g why
[20-02-28 12:11:35:018 HKT] [[z, x, d, v, g]]

行格式可以和列格式一样工作吗?正如我期望看到相同的结果,但我得到了不同的结果.

Would it be possible for the row format to work the same as the column format? As I was expecting to see the same results but I got different results.

谢谢

推荐答案

用电子表格术语来说,values 数组总是包含行,而每行包含列元素.在这两种情况下,

To put in spreadsheet terms, the values array always contain rows and each of the rows contain column elements. In both cases,

range.getValues().forEach(function(x){

x 将是一行.而 row 将是一个包含列元素的数组:

The x will be a row. And row will be a array with column elements:

A1:A5: [[z], [a], [b], [c], [d]]
A1:E1: [[z, x, d, v, g]]

A1:A5 有 5 行,每行都包含一个列元素.因此, forEach(row=>) 将迭代 5 行.A1:E1 包含 1 行,因此 forEach(row=>) 将迭代 1 行.要迭代每一行中的元素,您需要另一个 for 循环:

A1:A5 has 5 rows and each of those rows contain a column element. So, forEach(row=>) will iterate 5 rows. A1:E1 contain 1 row and therefore forEach(row=>) will iterate 1 row. To iterate the elements in each row, you'd need another for-loop:

const arr1 = [["a1","b1"]];
const arr2 = [["a1"],["a2"]];
const arr3 = [["a1","b1"],
                   ["a2","b2"]];
const st = JSON.stringify
const loop_ = (arr) => 
  arr.forEach((row, i) => 
    row.forEach((colEl, j) => 
      console.info(`This array is ${st(arr)} 
            row is ${st(row)} 
            Current column is ${st(colEl)}
            col Element at index [${i},${j}] in this array is ${colEl}`)));

[arr1,arr2,arr3].forEach(loop_)

这篇关于为什么 Google 脚本对于行中的范围和列中的范围的每次运行都不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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