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

陌路散爱 提交于 2020-02-23 07:01:46

问题


I have a Google Form that is populating a Google Sheet. 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.

I have a custom function to encode rows containing 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))

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


回答1:


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.

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


来源:https://stackoverflow.com/questions/55989097/how-to-use-a-custom-function-in-an-arrayformula-for-a-range-of-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!