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

时间:2018-06-23 03:14:31

标签: 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:A4复制到B1:C2

所需结果:

+---+---+---+---+
|   | A | B | C |
+---+---+---+---+
| 1 | 1 | 1 | 2 |
| 2 | 2 | 3 | 4 |
| 3 | 3 |   |   |
| 4 | 4 |   |   |
+---+---+---+---+

1 个答案:

答案 0 :(得分:2)

要获得将输入列表分成多行的预期结果,可以尝试以下方法。

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))
  ];
}

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

使用该函数将产生以下结果:

  • 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]]