Skip to content

Latest commit

 

History

History
231 lines (141 loc) · 9.32 KB

File metadata and controls

231 lines (141 loc) · 9.32 KB

Ballerina Google Sheets Connector

Build Build GitHub Last Commit GraalVM Check License

The Google Sheets, developed by Google LLC, allows users to programmatically interact with Google Sheets, facilitating tasks such as data manipulation, analysis, and automation.

The ballerinax/googleapis.gsheets package offers APIs to connect and interact with Sheets API endpoints, specifically based on Google Sheets API v4.

Setup guide

To use the Google Sheets connector, you must have access to the Google Sheets API through a Google Cloud Platform (GCP) account and a project under it. If you do not have a GCP account, you can sign up for one here.

Step 1: Create a Google Cloud Platform project

  1. Open the Google Cloud Platform Console.

  2. Click on Select a project in the drop-down menu and either select an existing project or create a new one.

    Enable Google Sheets API

Step 2: Enabling Google Sheets API

  1. Select the created project.

  2. Navigate to APIs & Services > Library.

  3. Search and select Google Sheets API. Then click ENABLE.

    Enable Sheets Api

Step 3: Creating an OAuth consent app

  1. Click on the OAuth Consent Screen in the sidebar.

  2. Select External and click CREATE.

  3. Fill in the app information and add the necessary scopes for Google Sheets API.

    OAuth Consent Screen

Step 4: Generating client ID & client secret

  1. In the left sidebar, click on Credentials.

  2. Click on + CREATE CREDENTIALS and choose OAuth Client ID.

    Create Credentials

  3. You will be directed to the OAuth consent screen, in which you need to fill in the necessary information below.

    Field Value
    Application type Web Application
    Name Sheets Client
    Authorized Redirect URIs https://developers.google.com/oauthplayground

    Create Client

Step 5: Obtain the access and refresh tokens

Follow these steps to generate the access and refresh tokens.

Note: It is recommended to use the OAuth 2.0 playground to acquire the tokens.

  1. Configure the OAuth playground with the OAuth client ID and client secret.

    OAuth Playground

  2. Authorize the Google Sheets APIs.

    Authorize APIs

  3. Exchange the authorization code for tokens.

    Exchange Tokens

Quickstart

To use the Google Sheets connector in your Ballerina project, modify the .bal file as follows:

Step 1: Import connector

Import the ballerinax/googleapis.gsheets module.

import ballerinax/googleapis.gsheets;

Step 2: Create a new connector instance

Create a gsheets:ConnectionConfig with the obtained OAuth2.0 tokens and initialize the connector with it.

configurable string clientId = ?;
configurable string clientSecret = ?;
configurable string refreshToken = ?;
configurable string refreshUrl = ?;

gsheets:Client spreadsheetClient = check new ({
    auth: {
        clientId,
        clientSecret,
        refreshToken,
        refreshUrl
    }
});

Step 3: Invoke connector operation

Now, utilize the available connector operations.

Create a spreadsheet with a given name

public function main() returns error? {

    // create a spreadsheet
    gsheets:Spreadsheet response = check spreadsheetClient->createSpreadsheet("NewSpreadsheet");

    // Add a new worksheet with given name to the Spreadsheet
    string spreadsheetId = response.spreadsheetId;
    gsheets:Sheet sheet = check spreadsheetClient->addSheet(spreadsheetId, "NewWorksheet");
}

Step 4: Run the Ballerina application

bal run

Examples

The Google Sheets connector provides practical examples illustrating usage in various scenarios. Explore these examples, covering use cases like creating, reading, and appending rows.

  1. Cell operations - Operations associated with a cell, such as clearing, setting, and deleting cell values.

  2. Grid filtering - Demonstrate filtering sheet values using a grid range.

  3. Sheet modifying - Basic operations associated with sheets such as creating, reading, and appending rows.

Build from the source

Prerequisites

  1. Download and install Java SE Development Kit (JDK) version 17. You can download it from either of the following sources:

    Note: After installation, remember to set the JAVA_HOME environment variable to the directory where JDK was installed.

  2. Download and install Ballerina Swan Lake.

  3. Download and install Docker.

    Note: Ensure that the Docker daemon is running before executing any tests.

Build options

Execute the commands below to build from the source.

  1. To build the package:

    ./gradlew clean build
  2. To run the tests:

    ./gradlew clean test
  3. To build the without the tests:

    ./gradlew clean build -x test
  4. To run tests against different environments:

    ./gradlew clean test -Pgroups=<Comma separated groups/test cases>
  5. To debug the package with a remote debugger:

    ./gradlew clean build -Pdebug=<port>
  6. To debug with the Ballerina language:

    ./gradlew clean build -PbalJavaDebug=<port>
  7. Publish the generated artifacts to the local Ballerina Central repository:

    ./gradlew clean build -PpublishToLocalCentral=true
  8. Publish the generated artifacts to the Ballerina Central repository:

    ./gradlew clean build -PpublishToCentral=true

Contribute to Ballerina

As an open-source project, Ballerina welcomes contributions from the community.

For more information, go to the contribution guidelines.

Code of conduct

All the contributors are encouraged to read the Ballerina Code of Conduct.

Useful links