Skip to main content

Python implementation of rowsncolumns spreadsheet operations

Project description

Rows & Columns Spreadsheet - Python Library

A Python implementation of spreadsheet operations, providing data manipulation capabilities similar to the TypeScript version used in the main Rows & Columns spreadsheet application.

Features

  • Core Data Types: Pydantic models for sheets, cells, ranges, and selections
  • Row Operations: Insert and delete rows with proper data shifting
  • Column Operations: Insert and delete columns with proper data shifting
  • Cell Operations: Get and set cell values, work with ranges
  • Table Support: Automatic table range updates during row/column operations
  • Filter Support: Basic filter range updates
  • Merge Support: Merged cell range updates
  • History: Operation history for undo/redo functionality

Installation

cd python
pip install -e .

For development:

pip install -e ".[dev]"

Quick Start

from rowsncolumns_spreadsheet import Spreadsheet, GridRange

# Create a new spreadsheet
spreadsheet = Spreadsheet()

# Set some cell values
spreadsheet.set_cell_value(0, 0, 0, "Hello")  # Sheet 0, Row 0, Col 0
spreadsheet.set_cell_value(0, 0, 1, "World")  # Sheet 0, Row 0, Col 1

# Get cell values
value = spreadsheet.get_cell_value(0, 0, 0)  # Returns "Hello"

# Insert rows
spreadsheet.insert_rows(sheet_id=0, reference_row_index=1, num_rows=2)

# Insert columns
spreadsheet.insert_columns(sheet_id=0, reference_column_index=1, num_columns=1)

# Work with ranges
range_values = spreadsheet.get_range_values(
    sheet_id=0,
    range_spec=GridRange(
        start_row_index=0,
        end_row_index=2,
        start_column_index=0,
        end_column_index=2,
    )
)

# Set range values
spreadsheet.set_range_values(
    sheet_id=0,
    range_spec=GridRange(start_row_index=0, end_row_index=1, start_column_index=0, end_column_index=1),
    values=[["A1", "B1"], ["A2", "B2"]]
)

Architecture

The library is structured around these core concepts:

Data Types (types.py)

  • CellData: Individual cell with value, formula, and formatting
  • GridRange: Rectangular cell range specification
  • Sheet: Worksheet with metadata, dimensions, and features
  • SpreadsheetState: Complete spreadsheet state including all sheets and data

Operations (operations.py)

  • insert_row(): Insert rows with proper data shifting and metadata updates
  • delete_row(): Delete rows with proper cleanup
  • insert_column(): Insert columns with proper data shifting
  • delete_column(): Delete columns with proper cleanup

High-Level Interface (spreadsheet.py)

  • Spreadsheet: Main class providing convenient methods for common operations
  • Manages state internally and provides intuitive APIs

Utilities (utils.py)

  • Helper functions for moving ranges, cloning formatting, etc.

Compatibility with TypeScript Version

This Python implementation mirrors the core functionality of the TypeScript version:

  1. Data Structures: Similar models using Pydantic instead of TypeScript interfaces
  2. Operation Logic: Same algorithms for row/column insertion with proper shifting
  3. Metadata Handling: Proper updates to tables, filters, merges, and frozen areas
  4. History Support: Operation tracking for undo/redo functionality

Key differences:

  • Uses Python/Pydantic conventions (snake_case, etc.)
  • Simplified callback system (no React-specific patterns)
  • Immutable state updates using model copying instead of Immer

Testing

Run tests with pytest:

pytest tests/

Run with coverage:

pytest --cov=rowsncolumns_spreadsheet tests/

Development

Format code:

black rowsncolumns_spreadsheet/ tests/

Type checking:

mypy rowsncolumns_spreadsheet/

Example: Advanced Usage

from rowsncolumns_spreadsheet import (
    Spreadsheet,
    Sheet,
    GridRange,
    Table,
    FilterView,
    MergedCell
)

# Create a spreadsheet with custom configuration
custom_sheet = Sheet(
    sheet_id=1,
    name="Sales Data",
    index=0,
    row_count=5000,
    column_count=50,
    frozen_row_count=1,  # Freeze header row
    frozen_column_count=2,  # Freeze first two columns
)

spreadsheet = Spreadsheet()
spreadsheet._state.sheets.append(custom_sheet)

# Add a table
table = Table(
    sheet_id=1,
    range=GridRange(
        start_row_index=0,
        end_row_index=100,
        start_column_index=0,
        end_column_index=10,
    ),
    name="SalesTable"
)
spreadsheet._state.tables.append(table)

# Insert rows - table will automatically expand
spreadsheet.insert_rows(sheet_id=1, reference_row_index=50, num_rows=10)

# The table range will now include the new rows
updated_table = spreadsheet._state.tables[0]
print(f"Table now spans rows {updated_table.range.start_row_index}-{updated_table.range.end_row_index}")

This Python library provides a solid foundation for building spreadsheet applications or integrating spreadsheet functionality into Python applications, while maintaining compatibility with the existing TypeScript ecosystem.

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

rowsncolumns_spreadsheet-0.1.0.tar.gz (49.4 kB view details)

Uploaded Source

Built Distribution

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

rowsncolumns_spreadsheet-0.1.0-py3-none-any.whl (56.7 kB view details)

Uploaded Python 3

File details

Details for the file rowsncolumns_spreadsheet-0.1.0.tar.gz.

File metadata

  • Download URL: rowsncolumns_spreadsheet-0.1.0.tar.gz
  • Upload date:
  • Size: 49.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for rowsncolumns_spreadsheet-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dfd26706bb625e6b7175a5244c1f584eda2c491e8e71d53d77175f47c23f94cb
MD5 c13d2f64644a6e5a4c9cc2d3eef2b536
BLAKE2b-256 5e6771a5b81c2f9d18c05c9e5eddcfc8009aad98f49bdc7cbe3aa6cd2db60a1e

See more details on using hashes here.

File details

Details for the file rowsncolumns_spreadsheet-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for rowsncolumns_spreadsheet-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0a474957bb1f2c0171b05935afa847dcf9b01970fe42b65e7fe835c2bb507851
MD5 a25a3e7d1a6d19ab6208e5d1af9aae77
BLAKE2b-256 faeedfce29da1fdcbf3b2cee288ff735db48fb10dc430a13865d349cd26f2446

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