Google表格:根据单元格的COUNTA(ARRAYFORMULA)重复一行

时间:2018-10-28 20:05:25

标签: google-sheets array-formulas

我有一张下表。该表会不时更新,因此不知道确切的行数:

+--+-------+-------------+
|a |red    |1, 1, 1,     |
+--+-------+-------------+
|b |green  |2, 2,        |
+--+-------+-------------+
|c |blue   |3,           |
+--+-------+-------------+

我需要根据第3列中的COUNTA重复每一行,如下所示:

+--+-------+-------------+
|a |red    |1            |
+--+-------+-------------+
|a |red    |1            |
+--+-------+-------------+
|a |red    |1            |
+--+-------+-------------+
|b |green  |2            |
+--+-------+-------------+
|b |green  |2            |
+--+-------+-------------+
|c |blue   |3            |
+--+-------+-------------+

我写了一个公式,但是要确保它能处理足够的行,我必须手动向该公式添加另一行(考虑列是E,F和G):

={
if(len(E2)>0,{
transpose(split(rept(E2&"****",COUNTA(split(G2,", "))),"****")),transpose(split(rept(F2&"****",COUNTA(split(G2,", "))),"****")),TRANSPOSE(split(G2,", "))}
,{"","",""});


if(len(E3)>0,{
transpose(split(rept(E3&"****",COUNTA(split(G3,", "))),"****")),transpose(split(rept(F3&"****",COUNTA(split(G3,", "))),"****")),TRANSPOSE(split(G3,", "))}
,{"","",""});

if(len(E4)>0,{
transpose(split(rept(E4&"****",COUNTA(split(G4,", "))),"****")),transpose(split(rept(F4&"****",COUNTA(split(G4,", "))),"****")),TRANSPOSE(split(G4,", "))}
,{"","",""});

if(len(E5)>0,{
transpose(split(rept(E5&"****",COUNTA(split(G5,", "))),"****")),transpose(split(rept(F5&"****",COUNTA(split(G5,", "))),"****")),TRANSPOSE(split(G5,", "))}
,{"","",""})

}

Example sheet

尚不清楚确切的行数,我想将其转换为2-1000行的ARRAYFORMULA。

那有可能吗?如果可以,公式是什么?谢谢!

1 个答案:

答案 0 :(得分:0)

将此脚本粘贴到脚本编辑器中。

    /** 
 * Splits the array by commas in the column with given index, by given delimiter
 * @param {A2:B20}  range Range reference
 * @param {2}  colToSplit Column index
 * @param {","}  delimiter Character by which to split
 * @customfunction
 */


function advancedSplit(range, colToSplit, delimiter) {
    var resArr = [], row;
    range.forEach(function (r) {
        r[colToSplit-1].replace(/(?:\r\n|\r|\n)(\d|\w)/g,", ").split(delimiter)
            .forEach(function (s) {
                row = [];
                r.forEach(function (c, k) {               
                   row.push( (k === colToSplit-1) ? s.trim() : c);
                })
                resArr.push(row);
            })
    })
     return resArr.filter(function (r) {
        return r.toString()
            .replace(/,/g, "")
    })
    }

然后在电子表格中将此脚本用作自定义公式

=advancedSplit(E2:G, 3, ",")

我希望这对您有帮助吗?