ARRAYFORMULA() does not work with SPLIT()

时光总嘲笑我的痴心妄想 提交于 2019-12-17 15:57:10

问题


Why doesn't the split formula get expanded over the entire column when I use =arrayformula(split(input!G2:G, ",")) ?

I get result only for the input!G2 cell, but not the rest in the G column. Other formulas like =arrayformula(find(",", input!G2:G)) seem to function and get expanded without problems.


回答1:


SPLIT applied to a vertical array does, now, work. (Jan 4 2017)

=ArrayFormula(SPLIT(input!G2:G,","))

or for error masking

=ArrayFormula(IFERROR(SPLIT(input!G2:G,",")))

Note: the resultant array is as wide as the maximum number of SPLITted elements.


(The remainder of the answer no longer applies, but retained for... historical purposes?)

This will be regarded as a really bad answer, but: it just doesn't.

I suspect it might be because it potentially will produce a jagged array (different number of elements in each row), which may be considered problematic. However, an "array SPLIT" used to "work" in the previous version of Sheets (albeit with a bug where it didn't work properly on the first element of the array).

SPLIT is not alone as a function that can't be iterated over an array (eg INDIRECT, INDEX, SUMIFS).


A workaround (edited Jan 4 2017):

=ArrayFormula(REGEXREPLACE(input!G2:G&REPT(",",6),REPT("([^,]*),",6)&",*","$"&COLUMN(OFFSET(A1,,,1,6))))

The 6 in the OFFSET(A1,,,1,6) and the REPT functions determines the maximum number of elements in the SPLIT. You could inject an expression that would calculate the maximum number of elements from the column of data, but then performance would suffer even more.

Also, a caveat here is that it will only support splitting by a single character (in this case a comma).

Alternatively, you could look at a Google Apps Script custom function.


This previous workaround no longer works, as REGEXEXTRACT no longer appears to support an array for the second argument - as it stands in January 2017, anyway.

=ArrayFormula(IFERROR(REGEXEXTRACT(","&input!G2:G,"^"&REPT(",+[^,]+",COLUMN(OFFSET(A1,,,1,6))-1)&",+([^,]+)")))




回答2:


You can use this script:

function better_split(col, separator) {
  var new_col = [];
  for (var r = 0; r < col.length; r++) {
    if (col[r]) {
      new_col.push(col[r][0].split(separator));
    }
  }
  return new_col;
}

use it like so (for performance reasons):

=better_split(filter(A2:A, len(A2:A)>0), "/")



回答3:


Another workaround would be to first JOIN your G2:G column with commas using ARRAYFORMULA. Then you can SPLIT it. Finally you can optionally TRANSPOSE it back into a column (otherwise it will output as a row).

=TRANSPOSE(SPLIT(ARRAYFORMULA(JOIN(",",FILTER(G2:G,NOT(ISBLANK(G2:G))))),","))



回答4:


An array formula is not required since Data > Split text into columns... works without any function or formula.



来源:https://stackoverflow.com/questions/26137115/arrayformula-does-not-work-with-split

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