Module persist.googlesheets
ballerinax/persist.googlesheets Ballerina library
Overview
This package provides Google sheets support for the bal persist
feature, which provides functionality to store and query data from a Google sheets conveniently through a data model.
The Google Sheets data store is a cloud-based spreadsheet application that stores data in tables. The Google Sheets data store is useful for storing data in a spreadsheet format.
How to use with bal persist
By default, bal persist
utilizes the in-memory data store. Therefore, you must explicitly specify Google Sheets data store as follows:
Integrate to bal build
-
Initialize
bal persist
and integrate tobal build
using the following command,$ bal persist add --datastore googlesheets --module <module_name>
-
After defining the entities, build the application using the following command,
$ bal build
One time generation
-
Initialize
bal persist
using the following command,$ bal persist init
-
Generate the persist client using the following command,
$ bal persist generate --datastore googlesheets --module <module_name>
Supported Ballerina Types
The following table lists the Ballerina types supported by the Google Sheets data store and the corresponding Google Sheets types used to store the data in the spreadsheet.
Ballerina Type | Google Sheets Type |
---|---|
int | NUMBER |
float | NUMBER |
decimal | NUMBER |
string | STRING |
boolean | BOOLEAN |
time:Date | STRING |
time:TimeOfDay | STRING |
time:Utc | STRING |
time:Civil | STRING |
Configuration
You need to set values for the following basic configuration parameters in the Config.toml
file in your project to use the Google Sheets data store.
Parameter | Description |
---|---|
clientId | The client ID of the Google Sheets API. |
clientSecret | The client secret of the Google Sheets API. |
refreshToken | The refresh token of the Google Sheets API. |
spreadsheetId | The ID of the spreadsheet to be used. |
The following is a sample Config.toml
file with the Google Sheets data store configuration. This is generated by the bal persist generate
command.
[<packageName>.<moduleName>] spreadsheetId = "" clientId = "" clientSecret = "" refreshToken = ""
Please refer to the Google API documentation for more information on how to obtain the client ID, client secret, and refresh token.
How to Set up
How to run script.gs
in the worksheet
The script.gs
file generated from the bal persist generate
command can initiate the google sheets client. This file can be executed in the Google Apps Script console using the following steps.
- Go to the respective spreadsheet.
- Open the AppScript console from the menu item
Extensions - > Apps Script
- Copy the content of the script.gs file into the console.
- Click the Deploy button to Deploy the project as a Web Application.
- Click on the Run button to execute the selected function.
How to obtain Google API tokens
API tokens for the Google sheets can be obtained using the following procedure.
- Get the clientID, and client secret using the code following guidelines.
- If you want to use OAuth 2.0 playground to receive the authorization code and obtain the access token and refresh token, Follow below steps.
- Go to the OAuth 2.0 playground.
- Click the settings icon on the top right corner of the page.
- Select the checkbox
Use your own OAuth credentials
. - Enter the OAuth Client ID and OAuth Client secret.
- Click
Close
. - Select the required Google Sheets API scopes.
- Click
Authorize APIs
. - Click
Exchange authorization code for tokens
to obtain the access token and refresh token.