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.
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.
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
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
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
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
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
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
major until I decide I want to upload them into a spreadsheet.
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
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
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
Finally, once we have a
Sheet, we need to create the spreadsheet with it’s respective
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
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