google-apps-script - 如何将单元格引用传递给 Apps 脚本自定义函数?

标签 google-apps-script google-sheets

假如说:

A1 = 3
B1 = customFunc(A1)  // will be 3

在我的自定义函数中:
function customFunc(v) {
  return v;
}
v将是 3。但我想访问单元格对象 A1 .

以下内容是从下面的评论中转录的。

输入:
+---+---+
|   | A |
+---+---+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
+---+---+

我要复制A1:A4B1:C2使用自定义函数。

想要的结果:
+---+---+---+---+
|   | A | B | C |
+---+---+---+---+
| 1 | 1 | 1 | 2 |
| 2 | 2 | 3 | 4 |
| 3 | 3 |   |   |
| 4 | 4 |   |   |
+---+---+---+---+

最佳答案

要实现将输入列表拆分为多行的预期结果,您可以尝试以下方法。

function customFunc(value) {
  if (!Array.isArray(value)) {
    return value;
  }
  // Filter input that is more than a single column or single row.
  if (value.length > 1 && value[0].length > 1) {
    throw "Must provide a single value, column or row as input";
  }
  var result;
  if (value.length == 1) {
    // Extract single row from 2D array.
    result = value[0];
  } else {
    // Extract single column from 2D array.
    result = value.map(function (x) {
      return x[0];
    });
  }
  // Return the extracted list split in half between two rows.
  return [
    result.slice(0, Math.round(result.length/2)),
    result.slice(Math.round(result.length/2))
  ];
}

请注意,它不需要使用单元格引用。它纯粹是处理输入的二维数组并返回转换后的二维数组。

使用该函数会产生以下结果:
  • A1:A4是硬编码的,B1包含 =customFunc(A1:A4)
    +---+---+---+---+
    |   | A | B | C |
    +---+---+---+---+
    | 1 | a | a | b |
    | 2 | b | c | d |
    | 3 | c |   |   |
    | 4 | d |   |   |
    +---+---+---+---+
    
  • A1:D4是硬编码的,A2包含 =customFunc(A1:D4)
    +---+---+---+---+---+
    |   | A | B | C | D |
    +---+---+---+---+---+
    | 1 | a | b | c | d |
    | 2 | a | b |   |   |
    | 3 | c | d |   |   |
    +---+---+---+---+---+
    
  • A1:B2是硬编码的,A3包含 =customFunc(A1:B2) ,错误消息是“必须提供单个值、列或行作为输入”
    +---+---+---+---------+
    |   | A | B |    C    |
    +---+---+---+---------+
    | 1 | a | c | #ERROR! |
    | 2 | b | d |         |
    +---+---+---+---------+
    

  • 通过处理更多参数(即要拆分的行数、每行的项目数、拆分为行而不是列等)或分析值本身,可以构建此方法以执行更复杂的转换。

    通过创建一个将函数作为参数的函数来执行任意转换的快速示例。

    但是,这种方法有以下限制:
  • 您无法在单元格公式中指定函数,因此您需要创建包装函数以从单元格公式调用
  • 这对所有单元格值执行统一转换

  • 功能:
    /**
     * @param {Object|Object[][]} value The cell value(s).
     * @param {function=} opt_transform An optional function to used to transform the values.
     * @returns {Object|Object[][]} The transformed values.
     */
    function customFunc(value, opt_transform) {
      transform = opt_transform || function(x) { return x; };
      if (!Array.isArray(value)) {
        return transform(value);
      }
      // Filter input that is more than a single column or single row.
      if (value.length > 1 && value[0].length > 1) {
        throw "Must provide a single value, column or row as input";
      }
      var result;
      if (value.length == 1) {
        // Extract single row from 2D array.
        result = value[0].map(transform);
      } else {
        // Extract single column from 2D array.
        result = value.map(function (x) {
          return transform(x[0]);
        });
      }
      // Return the extracted list split in half between two rows.
      return [
        result.slice(0, Math.round(result.length/2)),
        result.slice(Math.round(result.length/2))
      ];
    }
    

    并进行快速测试:
    function test_customFunc() {
      // Single cell.
      Logger.log(customFunc(2, function(x) { return x * 2; }));
    
      // Row of values.
      Logger.log(customFunc([[1, 2, 3 ,4]], function(x) { return x * 2; }));
    
      // Column of values.
      Logger.log(customFunc([[1], [2], [3], [4]], function(x) { return x * 2; }));
    }
    

    其中记录以下输出:
    [18-06-25 10:46:50:160 PDT] 4.0
    [18-06-25 10:46:50:161 PDT] [[2.0, 4.0], [6.0, 8.0]]
    [18-06-25 10:46:50:161 PDT] [[2.0, 4.0], [6.0, 8.0]]
    

    关于google-apps-script - 如何将单元格引用传递给 Apps 脚本自定义函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50997634/

    相关文章:

    google-apps-script - 有没有办法查看我执行的操作的脚本代码?

    google-apps-script - 返回二维数组的特定行并写入 Range

    google-apps-script - 使用 Apps 脚本直接(以编程方式)更改单个表单项的响应,而不是使用编辑响应 urlj

    javascript - 多个表单触发到单个工作表

    javascript - 超过 Google 电子表格的 ImportXML 限制

    google-apps-script - Google Apps 脚本 - 获取包含特定值的单元格的行号

    xpath - 在 Google 工作表中创建函数以获取我的外部 IP 地址

    email - 将电子邮件签名添加到电子邮件通知脚本

    javascript - 尝试将 CSV 文件导入 Google 云端硬盘电子表格时出现 400 错误请求

    javascript - 将 Google 表格中的日期和时间列合并为 createEvent() 函数