ballerinax/googleapis.gsheets Ballerina library

4.0.0

Overview

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

  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.

    FieldValue
    Application typeWeb Application
    NameSheets Client
    Authorized Redirect URIshttps://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.

Copy
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.

Copy
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

Copy
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

Copy
bal run

Examples

The Google Sheets connector provides practical examples illustrating usage in various scenarios. Explore these examples, covering use cases such as 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.

Import

import ballerinax/googleapis.gsheets;Copy

Metadata

Released date: 3 months ago

Version: 4.0.0

License: Apache-2.0


Compatibility

Platform: any

Ballerina version: 2201.8.5

GraalVM compatible: Yes


Pull count

Total: 0

Current verison: 0


Weekly downloads


Source repository


Keywords

Cost/Free

Vendor/Google


Contributors

Other versions

4.0.0