首页 文章

将数组传递给包含在ArrayFormula中的自定义函数?

提问于
浏览
1

我有一个电子表格,其中包含使用合并单元格的列,以便进行格式化 . 我正在尝试创建镜像第一组列的列,但在所有受影响的行上使用合并的单元格值 . 由于我在网上找到的自定义功能,我可以做到这一点 . 我不能做的就是在arrayformula中包含这个,我不知道为什么 .

以下是电子表格的小版本:https://docs.google.com/spreadsheets/d/1mp8PpgO4sI60bbx__1L4a17qL1VGIB9QVB910vTyhg0/edit?usp=sharing

自定义功能是:

/**
* Takes into account merged cells and returns the value of the merged cell 
* for all the cells within the merged range, rother than just the top left 
* cell of the merged range.
*
* Copied from https://webapps.stackexchange.com/questions/110277/how-do-i-reference-the-values-of-merged-cells-in-formulas
*
* Used by Patrick Duncan. - 7 May 2018
*/

function cellVal(cellAddress) {
  var cell = SpreadsheetApp.getActiveSheet().getRange(cellAddress);  
  return (cell.isPartOfMerge() ? cell.getMergedRanges()[0].getCell(1, 1) : cell).getValue();
}

没有Arrayformula的公式是:

= cellVal2(index(address(row(),5,4)))

而我正在尝试的是:

= arrayformula(cellVal2(index(address(row(E3:E),5,4))))

知道我在这里做错了吗?

干杯,

帕特里克

1 回答

  • 1

    这个修改怎么样?我认为有几种解决方案适合您的情况 . 所以请把它想象成其中之一 .

    修改点:

    • index(address(row(E3:E30),5,4)) 被赋予 cellAddress 时, cellAddress[["E3"], ["E4"], ["E5"],,,] . 这是一个二维数组 .

    • 展平 getRangeList() 的这个二维数组 .

    • 使用 getRangeList() 将展平的数组转换为范围数组 .

    • 使用转换后的范围检索每个值并返回它们 .

    修改后的脚本:

    function cellVal3(cellAddress) { // Modified the function name to "cellVal3"
      cellAddress = Array.prototype.concat.apply([], cellAddress);
      var cells = SpreadsheetApp.getActiveSheet().getRangeList(cellAddress).getRanges();
      return cells.map(function(cell){return [(cell.isPartOfMerge() ? cell.getMergedRanges()[0].getCell(1, 1) : cell).getValue()]});
    }
    

    用法:

    使用此自定义功能时,请使用如下 .

    =ARRAYFORMULA(cellVal3(index(address(row(E3:E30),5,4))))
    

    要么

    =cellVal3(index(address(row(E3:E30),5,4)))
    

    参考文献:

    如果我误解你的问题,我很抱歉 .

相关问题