Skip to content

XpressAI/xai-gspread

Repository files navigation

Component LibrariesProject Templates
DocsInstallTutorialsDeveloper GuidesContributeBlogDiscord

Xircuits Component Library for GSpread – Simplify your Google Sheets operations directly in Xircuits workflows.


Xircuits Component Library for GSpread

Integrate Google Sheets functionalities into Xircuits workflows effortlessly. Perform operations like spreadsheet creation, cell updates, and worksheet management using the GSpread library.

Table of Contents

Preview

SimpleGSpread Example

SimpleGSpread_example

SimpleGSpread Result

SimpleGSpread

Prerequisites

Before you begin, you will need:

  1. Python 3.9+.
  2. Xircuits installed.
  3. Google Service Account JSON key for authentication.

Main Xircuits Components

GspreadAuth Component:

Authenticates using a Google Service Account JSON key and initializes a GSpread client.

GspreadAuth

OpenSpreadsheet Component:

Opens a Google Spreadsheet by its title and selects a specified worksheet or defaults to the first worksheet.

OpenSpreadsheet

OpenSpreadsheetFromUrl Component:

Opens a Google Spreadsheet using its URL and selects a worksheet.

OpenWorksheet Component:

Selects a worksheet by its title or defaults to the first worksheet.

CreateSpreadsheet Component:

Creates a new Google Spreadsheet and optionally shares it with an email.

CreateWorksheet Component:

Adds a new worksheet to an existing spreadsheet with customizable rows and columns.

UpdateRange Component:

Updates a range of cells in the worksheet with specified values.

GetAllValues Component:

Retrieves all values from a worksheet as a list of lists.

GetAllRecords Component:

Retrieves all records from a worksheet as a list of dictionaries.

FindAllStringMatches Component:

Finds all cells containing a specific string value.

FindAllRegexMatches Component:

Finds all cells matching a specified regular expression.

Try the Examples

We have provided example workflows to help you get started with the GSpread component library. These examples demonstrate how to authenticate, read, and update Google Sheets within a Xircuits workflow.

SimpleGSpread

This example demonstrates how to authenticate, read a cell's value, and update it with new data.

Installation

To use this component library, ensure that you have an existing Xircuits setup. You can then install the GSpread library using the component library interface, or through the CLI using:

xircuits install gspread

Alternatively, you can install it manually by cloning and installing it:

# base Xircuits directory
git clone https://github.com/XpressAI/xai-gspread xai_components/xai_gspread
pip install -r xai_components/xai_gspread/requirements.txt

Authentication and Service Credentials Setup

To access spreadsheets via Google Sheets API, you need to authenticate and authorize your application. If you plan to access spreadsheets on behalf of a bot account, use a Service Account. Below are the steps to set up service credentials.

  1. Enable API Access:
    1. Go to Google Developers Console and create a new project (or select an existing one).
    2. In "Search for APIs and Services," enable both "Google Drive API" and "Google Sheets API."
  2. Create Service Account Credentials:
    1. Navigate to "APIs & Services > Credentials" and click "Create credentials > Service account key."
    2. Fill out the form and select JSON key type.
    3. Download the JSON file containing your credentials.
  3. Share Spreadsheet Access:
    • Share your spreadsheet with the client_email listed in the JSON file.
  4. Configure Credentials:
    • Place the downloaded JSON file in a location accessible to your application.
    • Optionally, move the file to ~/.config/gspread/service_account.json (Linux/Mac) or %APPDATA%\gspread\service_account.json (Windows).

For more details, refer to the GSpread Authentication Guide.

Notes

  1. Ensure the Google Service Account is shared with the spreadsheets you intend to access.
  2. Keep your JSON key secure and do not expose it publicly.