If you have data stored in a spreadsheet and you want to move back or forwards through that data then here is some sample code you can use.
In this example, we have a table with news headlines and news articles. I probably should have called the columns Title and Content, but I’ve gone for
News. You can add in additional content like Author, Date, Image Source etc but once you understand how to use two columns you can expand it to as many as you like.
Show the first Article
Our sample app has only two labels, one for the title of the article and the other is for the body of the article. To allow the user to navigate around the app we’ll need two variables.
articleIndex keeps track of which row we should be reading from in our spreadsheet.
numberOfArticles is calculated when the app first loads and is used to store the number of rows (articles) in our database.
Once we know how many stories there are we can call the
Showing an Article
In this function we can specify how many articles we want to move forwards (or backwards) by passing a value to the “increment” input. For a more robust experience we should probably start by handling what happens if there is no
increment argument - but we’ll leave that for another article.
The first thing that happens here is we add the
increment to the
articleIndex. After a quick check to make sure our index hasn’t strayed out of bounds the
GetCell functions are used to pull the relevant information from the database.
Navigating between articles
Because of the way we’ve designed the
showArticle function, passing it an increment of 1 will move forwards one article while an increment of -1 moves back one.
Remix this example for yourself by clicking on the link below: