Home > Back-end >  Fill Down values from a 1x6 array into an _x6 array Google App Script for Google Sheet
Fill Down values from a 1x6 array into an _x6 array Google App Script for Google Sheet

Time:11-04

I've got this:

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

What I want is this: array_output

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

Sorry, I know I'm probably not using the correct symbols. That's part of my problem, I'm sure.

Thanks!

CodePudding user response:

Probably something like this:

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

var new_array = [];
while (rows--) new_array.push(array_to_fill_down)

Or

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

Not sure if I understand your goal, though.

CodePudding user response:

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'])])
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You've asked for a script specifically. But in the event that you are a future site visitor wanted to do this with a formula:

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

If the array to repeat were to be entered into a horizontal range (say, A1:F1):

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

And if that to-be-filled array were entered into a vertical range (say, A1:A6):

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

  • Related