Google Sheet 将 ArrayFormula 与 textjoin 结合使用

Google Sheet use ArrayFormula with textjoin

我有 this sheet 的物品清单。我想将产品 B:K 的列合并为一列,并自动为所有非空行 运行 此函数。 我尝试将 arrayformulatextjoin 一起使用,但它只会多次复制相同的行。 我该怎么做?

您不能在 arrayformula 中使用 textjoin

这里是您可以改用的自定义函数:

function array_text_join(data) {
  const result = data.map(row=>{
    const isEmptRow = !row.some(cell=>cell !== "")
    return [isEmptRow? undefined: row.filter(cell=>cell!=="").join(",")]
  })
  return result
}

放到L2就可以了=array_text_join(A2:K)

在单元格 L1 中试试这个:

=arrayformula({"Name","Items";A2:A,regexreplace(regexreplace(trim(flatten(query(transpose(B2:K)&",","",9^9))),"[\ ,]+$",),",\ ",",")})

或者这样不删除逗号后的空格:

=arrayformula({"Name","Items";A2:A,regexreplace(trim(flatten(query(transpose(B2:K)&",","",9^9))),"[\ ,]+$",)})

如果您的产品名称中有空格,或者产品之间有间隔(横向),请使用:

=arrayformula({"Name","Items";A2:A,regexreplace(regexreplace(trim(flatten(query(transpose(B2:K)&",","",9^9))),"[,\ ]{2,}",", "),"[,\ ]+$",)})

简单,但在 L2 中最后仍然是一个逗号

=flatten(query(transpose(arrayformula(if(B2:K="","",B2:K&", "))),,COLUMNS(B2:K)))