How to display multiple elements of an array
In this article, you will learn how to display not just one row from Google Sheets, but several at once.
This tutorial is applicable not only to Google Sheets: following this algorithm, you can display the elements of any array. This is useful if you want to display several products, links, etc., to your users at once.
Create a spreadsheet in Google Sheets and get its URL
Create a note spreadsheet and add the note
column to it.
As with reading a single row from Google Sheets, the first step is to publish the spreadsheet and get a URL to access it.
Follow the steps according to the instructions.
Add an HTTP request to a spreadsheet
In your script, add the HTTP request block.
Paste the link obtained in the previous step in the URL field.
The GET
method is used to read data.
On the RESPONSE tab, create an $items
variable with the $httpResponse
value.
To read data from spreadsheets, you do not need to fill out the BODY
and HEADERS
tabs.
Add other blocks to your script
Error detection
From the Error option, make a connection to the Text block with the error $httpStatus
value.
This screen will help you understand what is causing the problem if the request fails to complete successfully.
Display the first element of an array
From the Success option, make a connection to the Conditions block with the $items.first()
value.
This uses the first
internal function to print the first element of the array.
Assign a zero value
From the $items.first()
condition, make a connection to the next Conditions block adding the $index = 0
value to it.
Connect both true
and else
options to the next screen.
Since the variable is assigned 0 here, we need to connect both options.
Display the current note
On the next screen, add the Text block and write $items.current().note
in it.
This is where you display the current note. Add the Transition block to the same screen.