【问题标题】:Fill Down values from a 1x6 array into an _x6 array Google App Script for Google Sheet将 1x6 数组中的值填充到 _x6 数组中 Google 表格的 Google App 脚本
【发布时间】:2021-12-18 06:57:07
【问题描述】:

我有这个:

array_to_fill_down = [a, b, c, cat, 15, blue]
rows = 3

我想要的是: 数组输出

[
{a, b, c, cat, 15, blue},
{a, b, c, cat, 15, blue},
{a, b, c, cat, 15, blue}
]

抱歉,我知道我可能没有使用正确的符号。这是我的问题的一部分,我敢肯定。

谢谢!

【问题讨论】:

    标签: arrays google-apps-script google-sheets range rows


    【解决方案1】:

    console.log([...Array.from({length:3}, x => ['a', 'b', 'c', 'cat', 15, 'blue'])])
    console.log([...Array.from(Array(3), x => ['a', 'b', 'c', 'cat', 15, 'blue'])])

    【讨论】:

      【解决方案2】:

      您专门要求提供脚本。但是,如果您是未来的网站访问者,希望通过以下公式做到这一点:

      =ArrayFormula(TRANSPOSE(SPLIT(REPT({"a";"b";"c";"cat";15;"blue"}&"~",3),"~",1,0)))

      如果要重复的数组被输入到水平范围内(例如,A1:F1):

      =ArrayFormula(TRANSPOSE(SPLIT(REPT(TRANSPOSE(A1:F1)&"~",3),"~",1,0)))

      如果要填充的数组被输入到一个垂直范围内(比如,A1:A6):

      =ArrayFormula(TRANSPOSE(SPLIT(REPT(A1:A6&"~",3),"~",1,0)))

      【讨论】:

        【解决方案3】:

        大概是这样的:

        array_to_fill_down = ['a', 'b', 'c', 'cat', 15, 'blue'];
        rows = 3;
        
        var new_array = [];
        while (rows--) new_array.push(array_to_fill_down)
        

        或者

        var new_array = new Array(rows).fill(array_to_fill_down);
        

        不过,我不确定我是否理解你的目标。

        【讨论】:

        • 这很完美!非常感谢!
        • 它不会让我将其标记为“有用”,因为我没有足够的“声誉”。但这正是我所需要的。我认为 Array(rows).fill(array_to_fill_down) 会比 while 循环快得多,所以我将使用它。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多