Reading and creating a Spreadsheet in Android

Pedro Carrillo
Feb 27, 2018 · 6 min read

In this article I will shed some light on how we can read and create a spreadsheet using the Sheet v4 API in Android. I will be following the official quickstart guide with some added changes while trying to explain every component used. We will also see an example on how to create a spreadsheet.

Reading an spreadsheet and displaying it on our view
Create Spreadsheet Screen with newly created Spreadsheet id
Result after creating the spreadsheet

Repository Information: Github Pedro Carrillo — SpreadSheetAndroid (Don’t forget to check out the branches :) )

Before starting you need to create a project in your google play services console and authorize the Drive and Sheets API.

Authentication

In order to create files using an specific account we need to authenticate first. We will need to use the Google Account Login API and the Google API Client Library for Java

app gradle for authentication API’s

The Google Account Login will help us start the process of authentication with the sign in intent. It will launch the account picker and ask for the required scopes needed for your app. It will also be able to provide the information of the last account signed in. We will also need the Google API Client Library for Java. This library will help us authenticate ourselves in order to access the Sheet API, which is a Java library.

We will use an Authentication manager that contains the GoogleSignInClient that will provide us the sign in intent and the GoogleAccountCredential which will be used by the sheets api.

Then we can just launch the authentication by telling the view to start GoogleSignInClient.signInIntent

We can just handle the response from the intent in the `onActivityForResult` method

After we successfully logged in, we can access the last signed account. We need to assign the GoogleSignInClient.getLastSignedAccount()?.account property to the GoogleAccountCredential.selectedAccount. This step is needed because at the moment of using the Sheet API, we will be providing a GoogleAccountCredential which requires authorization from the account.

Lastly, we will initialize the clients in our Activity with the following Scopes:

Reading a Spreadsheet

Once we are authenticated, we can start reading an spreadsheet with an specific ID. For this example, I will be using the same spreadsheetId and range that the official guide used

We will need an instance of Sheets which takes cares of writing and reading Google Sheets. From it, we can use the get method specifying an id and a range to get an spreadsheet. I decided to use RxJava in order to avoid using AsyncTask. Probably you could use coroutines from Kotlin but that’s an improvement I will make later.

In this class, we build the Sheets object and we create a method readSpreadsheet that will return the call to get as an observable. Then you just need to handle the information obtained and show it accordinly. For this scenario I created and object
Person which will take the information provided from the spreadsheet and show it on a RecyclerView

And that’s it. Now you can read an spreadsheet and show it as you would like in your application!

If you would like to see the code only for Reading : Github Repo — Branch Read

Creating an spreadsheet

Once we are authenticated, we can start creating an spreadsheet with the specific needs that we have. For this example, I will be uploading an object Person. I will be creating Person using a name and major until I decide I want to upload them into a spreadsheet.
Using the Sheets it’s basically the same as the read just with an slight change of parameters and api use.

We are sending an Spreadsheet object and calling the create api. This will create the spreadsheet with all the features that you want.

Now, here I will describe how I decided to create the Spreadsheet object. Here is a rough explanation of what is needed in order to create a Spreadsheet object.

1. Spreadsheet: Represents the whole Spreadsheet
2. Sheet: Represents a sheet in a spreadsheet
3. GridData: Represents data in an specific grid with an starting position (startColumn and startRow)
4. RowData: Represents data of each cell that belongs to a Row
5. CellData: Data about specific cell

We will be creating two cells per row in this example, as I want to add the name and major. Using the following Facade for CellData, we create a CellData for any data that we have.

Because we need two CellData per RowData, we do the following:

Now we need to create a GridData, which will create all the rows for each person that I have:

After we created our GridData, we need a Sheet. We also send the sheet title as we can add it as a SheetProperty:

Finally, once we have a Sheet, we need to create the spreadsheet with it’s respective title:

Now when we are ready to upload a list of Person we do the following:

And it’s ready! We can customize the title and send our own data.

If you would like to see the code only for Reading : Github Repo

Conclusion

We observed very briefly how to read and create spreadsheets in android. There is more functionality that I haven’t touch, like creating a graph and adding developer metadata, but it’s a great start with the Sheets Api! If you have any doubt, comment or correction, please don’t hesitate to leave a comment as we are always learning new things :)

Relevant Tags: Android, Spreadsheet, Google Sheet, Sheet Api, Creating spreadsheet, Adding Spreadsheet, Kotlin, RxJava

Pedro Carrillo

Written by

Student of MS in Computer Science at FIU. Previously Android Intern at The New York Times. Organizer at AndroidDevPeru and DroidLatino

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade