Skip to main content

A Google Sheets manager for Python

Project description

GSheetManager

GSheetManager is a Python package that provides a convenient interface for managing Google Sheets. It offers features like batch updates, local caching, and automatic worksheet refreshing to optimize interactions with Google Sheets.

Installation

You can install GSheetManager using pip:

pip install gsheet-manager

Requirements

  • Python 3.6+
  • gspread library

Usage

Here's a basic example of how to use GSheetManager:

from gsheet_manager import GSheetManager

class MySheetManager(GSheetManager):
    @GSheetManager.batch_sync_with_remote
    def update_sales_data(self, product, quantity, price):
        # Find the row for the product
        product_column = 0
        quantity_column = 1
        price_column = 2
        total_column = 3

        for row, row_data in enumerate(self.local_sheet_values):
            if row_data[product_column] == product:
                # Update quantity
                self._set_buffer_cells(row, quantity_column, quantity)
                
                # Update price
                self._set_buffer_cells(row, price_column, price)
                
                # Calculate and update total
                total = quantity * price
                self._set_buffer_cells(row, total_column, total)
                
                print(f"Updated {product}: Quantity={quantity}, Price=${price}, Total=${total}")
                return

        # If product not found, add a new row
        new_row = len(self.local_sheet_values)
        self._set_buffer_cells(new_row, product_column, product)
        self._set_buffer_cells(new_row, quantity_column, quantity)
        self._set_buffer_cells(new_row, price_column, price)
        self._set_buffer_cells(new_row, total_column, quantity * price)
        print(f"Added new product {product}: Quantity={quantity}, Price=${price}, Total=${quantity * price}")

# Usage
manager = MySheetManager('path/to/key_file.json', 'Sales Sheet', 'Product Data')
manager.update_sales_data('Widget A', 100, 19.99)
manager.update_sales_data('Gadget B', 50, 24.99)

Features

  • Batch updates to minimize API calls
  • Local caching of sheet values
  • Automatic worksheet refreshing
  • Decorator for syncing operations with remote

API Reference

GSheetManager(key_file, doc_name, sheet_name)

Creates a new GSheetManager instance.

  • key_file: Path to your Google Service Account key file
  • doc_name: Name of your Google Sheet
  • sheet_name: Name of the worksheet

Methods

  • refresh_worksheet(): Refreshes the worksheet if the timeout has passed
  • sync_from_remote(): Syncs local values from the remote sheet
  • batch_update_remote(): Sends batched updates to the remote sheet
  • _set_buffer_cells(python_row_idx, python_col_idx, value): Sets a value in the local buffer

Decorators

  • @batch_sync_with_remote: Decorator for methods that need to sync with the remote sheet

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gsheet_manager-0.3.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gsheet_manager-0.3.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file gsheet_manager-0.3.0.tar.gz.

File metadata

  • Download URL: gsheet_manager-0.3.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.8.17

File hashes

Hashes for gsheet_manager-0.3.0.tar.gz
Algorithm Hash digest
SHA256 58454088f7a749d21a1c8440076fecffd53dd1781bbbe2730b0f0c8e6158c4c1
MD5 3a8b9c4e269419424db1888531443eae
BLAKE2b-256 67cdfe1713422cc1b1b6789cf602184a445cd7023ba33124c7e987597a89d152

See more details on using hashes here.

File details

Details for the file gsheet_manager-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: gsheet_manager-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.8.17

File hashes

Hashes for gsheet_manager-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 525807170d9e97433994c1ab26a84b7ea1e4895da8155fc06bf60b5443e6af4c
MD5 538981dc3676684aafb39d2b2fc90ee6
BLAKE2b-256 44dd8cb3e778b26a73e5525b71ba61085a60d294425eeb29e934d479a78fbb5e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page