带有 INDIRECT 的 ARRAYFORMULA 的解决方法脚本

问题描述 投票:0回答:2

在浪费了太多时间阅读支持表单之后,目前似乎没有办法在 Google 表格中同时使用 ARRAYFORMULA 和 INDIRECT。

我目前正在尝试根据选项卡名称数组从选项卡中提取信息。

如果 Google Sheets 中没有数组的解决方法公式,我将转向使用 Apps 脚本创建自定义函数。我已经仔细阅读了 3 个分支,但没有成功,所以我现在会采取任何想法。我试过:

  1. 创建一个新的间接公式,可以处理在 ARRAYFORMULA 中使用
  2. 创建一个可以获取文本并将其转换为公式的评估函数
  3. 创建可以传递工作表名称数组的间接公式

如果有人知道任何可以远程执行此操作的脚本或插件,请分享。

如果需要,我可以分享我对自定义函数的尝试,但考虑到我在 JavaScript 中高效编写的能力有多差,它们很可能对主题有害。

javascript google-sheets automation formula add-on
2个回答
0
投票

他们基本上是在尝试这样做: 给出这个示例表

A B
1 第 1 组 A1:A
2 第2组 A1:A
3 第 4 组 B3:B
4 另一组 A2:A

1)

    =ARRAYFORMULA("=INDIRECT('"&A1:A&"'!"&B1:B&")")
    =ARRAYFORMULA(EVALUATE(C1:C)) // Would turn previous text to formula, created through Apps Script
    =ARRAYFORMULA(BETTERINDIRECT("'"&A1:A&"'!"&B1:B&"")) // text makes make 'name'!range
// BETTERINDIRECT would be a Apps Script function that works with ArrayFormula
    =INDIRECTARRAY("'"&A1:A&"'!"&B1:B&"")) // a custom function that accepts an array of entries
// Would need to be made through apps script

他们所问的只是是否有人设计了任何插件或编写了可以做到这一点的代码。


0
投票

这是一个简单的应用程序脚本,可以满足您的要求。

/**
 * Returns an array consisting of the concatenation of the given range from all sheets, skipping a specified number of initial sheets.
 *
 * @param {string} cell_range - The range to be taken from all sheets.
 * @param {number} [first=1] - The number of initial sheets to skip.
 * @param {boolean} [horizontal=false] - Concatenate horizontally or vertically.
 * @param {boolean} [name=false] - Add sheet name as the first column or row depending on horizontal.
 * @returns {Array<Array<any>>} The concatenated data.
 */
function rangeFromAllSheets(cell_range, first=1, horizontal=false, name=false) {
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  var result = [];

  // Loop over all sheets and get the data from the specified range
  for (var i = first; i < sheets.length; i++) {
    var sheet = sheets[i];
    var data = sheet.getRange(cell_range).getValues();

    if (name) {
      if (!horizontal) {
        // If concatenating horizontally and name is true, add sheet name as the first row
        data.unshift([sheet.getName()]);
      } else {
        // If concatenating vertically and name is true, add sheet name as the first column
        for (var j = 0; j < data.length; j++) {
          data[j].unshift(sheet.getName());
        }
      }
    }

    if (!horizontal) {
      // Concatenate the data horizontally
      result = result.concat(data);
    } else {
      // Concatenate the data vertically
      if (result.length === 0) {
        result = data;
      } else {
        for (var j = 0; j < data.length; j++) {
          result[j] = result[j].concat(data[j]);
        }
      }
    }
  }
  
  return result;
}

您可以将其用于:

=rangeFromAllSheets("E1:F1")
© www.soinside.com 2019 - 2024. All rights reserved.