简体   繁体   English

如何在 ARRAYFORMULA 中为一系列单元格使用自定义函数?

[英]How to use a custom function in an ARRAYFORMULA for a range of cells?

I have a Google Form that is populating a Google Sheet.我有一个填充 Google 表格的 Google 表单。 Due to having custom formulas in the sheet to manipulate the data populated from the form I am using ARRAYFORMULA to apply to all rows in a column.由于工作表中有自定义公式来操作从表单填充的数据,我使用 ARRAYFORMULA 应用于列中的所有行。

I have a custom function to encode rows containing html我有一个自定义函数来编码包含 html 的行

function base64EncodeWebSafe(input) {

  try {
    // Try and fetch the specified url.
    return Utilities.base64EncodeWebSafe(input);

  } catch (e) { 
    Utilities.sleep(1000);
    return Utilities.base64EncodeWebSafe(input);

  }
}

When I call this function inside of ARRAYFORMULA(base64EncodeWebSafe(T2:T))当我在 ARRAYFORMULA(base64EncodeWebSafe(T2:T)) 中调用这个函数时

I receive an error "Cannot convert Array to (class)[]."我收到错误“无法将数组转换为(类)[]。”

What I expect to happen is to apply the encoding function to the range T2:T我期望发生的是将编码函数应用于范围 T2:T

This is described in the Custom Functions guide .这在自定义函数指南中进行了描述。 I've adapted the implementation used in the guide, but you could do something else.我已经调整了指南中使用的实现,但您可以做其他事情。 Basically, if using ARRAYFORMULA, then you need to treat the input as a 2-dimensional array.基本上,如果使用 ARRAYFORMULA,那么您需要将输入视为二维数组。

function base64EncodeWebSafe(input) {
  if (input.map) { // Test whether input is an array.
    return input.map(base64EncodeWebSafe)
  } else {
    try {
      return Utilities.base64EncodeWebSafe(input);
    } catch (e) {
      Utilities.sleep(1000);
      return Utilities.base64EncodeWebSafe(input);
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM