insert array in Range

1,342 views
Skip to first unread message

Maik Uhlig

unread,
Jul 27, 2021, 9:26:52 AM7/27/21
to Google Apps Script Community
Hi,

I am searching for the syntax to insert an array into a Range.

For example:

array: data_ar=['Hungary','Germany','England','USA');

The result should be:

         A                 B                 C
1  Hungary
2  Germany
3  England
4  USA
5  

I can solve it with a for-loop and getRange().setValue(), but there a more than thousand values and the loop will take to many time. 

Is there a direct syntax, like getRange('A1').setValue(data_ar)?

Thanks for help

Darren D'Mello

unread,
Jul 27, 2021, 9:33:04 AM7/27/21
to google-apps-sc...@googlegroups.com
sheet.getRange("A1:A4).setValues(
[
['Hungary']',
[Germany'],
['England'], 
['USA']
]
);

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/743dc69e-f674-4da4-9f0d-f757bd93a33bn%40googlegroups.com.

Marco Pires

unread,
Jul 27, 2021, 9:38:11 AM7/27/21
to Google Apps Script Community
Yes, you can use the getRange().setValues(). In your case:

data_ar=['Hungary','Germany','England','USA');
getRange('A1:A4').setValues(data_ar);

The size of the two-dimensional array must match the size of the range.

Have a nice day.

Maik Uhlig

unread,
Jul 27, 2021, 11:33:22 AM7/27/21
to Google Apps Script Community
Hello, thanks for help. But the result is:
Exception: The parameters (number[]) don't match the method signature for SpreadsheetApp.Range.setValues.
I tried this example!

I made a screenshot.

fault.PNG


Any ideas?

Marco Pires

unread,
Jul 27, 2021, 12:13:12 PM7/27/21
to Google Apps Script Community
Ops...  sorry, the array has to be two-dimensional. Like this:
data_ar=[ ['Hungary','Germany','England','USA] ];
getRange('A1:D1').setValues(data_ar);

In your example: 
data_ar=[ ['Hungary'], ['Germany'], ['England'], ['USA'] ];
getRange('A1:A4').setValues(data_ar);

Maik Uhlig

unread,
Jul 27, 2021, 3:28:29 PM7/27/21
to Google Apps Script Community

THANK YOU VERY MUCH! Now, it works!
Reply all
Reply to author
Forward
0 new messages