Sheet2Code

Transform Excel and Google Sheets formulas into production-ready TypeScript or Python code

What is Sheet2Code?

Sheet2Code is an open-source tool that automatically converts both Microsoft Excel and Google Sheets formulas, calculations, and business logic into clean, executable TypeScript or Python code. It's perfect for developers who need to migrate spreadsheet-based calculations into production applications, automate spreadsheet workflows, or understand complex spreadsheet formulas programmatically.

Whether you're working with Excel (.xlsx) files or Google Sheets documents, dealing with financial models, data analysis spreadsheets, or complex VLOOKUP chains, Sheet2Code transforms your spreadsheet logic into maintainable, testable, and version-controlled code. The tool preserves the exact calculation logic from either Excel or Google Sheets, ensuring accuracy in the conversion process.

Excel & Google Sheets Compatibility

Sheet2Code works seamlessly with both Microsoft Excel and Google Sheets:

The conversion process preserves the exact calculation logic whether your source is an Excel workbook or a Google Sheets document, making it the perfect tool for organizations using either or both platforms.

Key Features

🎯 Formula Parser

Advanced Chevrotain-based parser that understands complex nested formulas, array operations, and all standard Excel and Google Sheets syntax

🔄 Dependency Analysis

Intelligent topological sorting ensures formulas are calculated in the correct order, with automatic circular dependency detection

📊 100+ Functions

Support for SUM, VLOOKUP, IF, INDEX/MATCH, SUMIF, array formulas, and most common Excel and Google Sheets functions

🏷️ Named Ranges

Automatically resolves named ranges and converts them to readable variable names in generated code

📁 Multi-Sheet Support

Handles cross-sheet references, automatically fetches referenced sheets, and maintains sheet relationships

⚡ Production Ready

Generated code includes error handling, type safety, and can be executed directly from command line

How It Works

1. Authentication & Data Extraction

For Google Sheets, Sheet2Code uses the Google Sheets API to read your spreadsheet data. It supports both OAuth2 for interactive use and Service Account authentication for automation. For Excel files, you can export to Google Sheets or use the Excel-to-CSV export feature. The tool fetches all formulas, values, and metadata from specified sheets.

2. Formula Parsing & AST Generation

Using a sophisticated Chevrotain-based parser, Sheet2Code tokenizes and parses each formula into an Abstract Syntax Tree (AST). This handles everything from simple arithmetic to complex nested functions with multiple arguments.

=IF(VLOOKUP(A2,Sheet2!A:D,4,FALSE)>100,SUM(B2:B10)*1.1,B2)

Gets transformed into a structured AST that represents the formula's logic programmatically.

3. Dependency Graph Construction

The dependency analyzer builds a directed graph of all cell relationships. It identifies which cells depend on others, detects circular references, and determines the optimal calculation order using topological sorting.

4. Code Generation

Finally, the code generator traverses the dependency graph and AST to produce clean, readable code in your chosen language:

// TypeScript Output
cells['Results!A1'] = vlookup(cells['Input!A2'], getRange('Sheet2!A:D', cells), 4, false) > 100 
  ? sum(...getRange('Input!B2:B10', cells)) * 1.1 
  : cells['Input!B2'];

Installation & Quick Start

For Google Sheets:

# Clone the repository
git clone https://github.com/joemastersemison/google-sheets-to-code.git
cd google-sheets-to-code

# Install dependencies
npm install

# Set up Google Sheets API credentials
npm run cli -- setup

# Convert your Google Sheets document
npm run cli -- convert \
  --url "https://docs.google.com/spreadsheets/d/YOUR_SHEET_ID/edit" \
  --input-tabs "Input,Parameters" \
  --output-tabs "Results,Summary" \
  --language typescript \
  --output-file generated-code.ts

# Run the generated code
node generated-code.js --json

For Excel Files:

# 1. Go to Google Sheets
# 2. File -> Import -> Upload your .xlsx file
# 3. Use the Google Sheets URL with the tool

Use Cases

Supported Formula Types

Sheet2Code handles a comprehensive range of Excel and Google Sheets formulas and functions that work identically in both platforms:

Advanced Features

Circular Dependency Handling

Sheet2Code detects circular references and handles them gracefully, generating code with #REF! errors and clear warnings, just like Excel and Google Sheets would display.

Quoted Sheet Names

Properly handles sheet names with spaces and special characters, like 'Q4 Sales Data'!A1 or 'John''s Report'!B2:B10.

Absolute & Relative References

Understands the difference between $A$1, A$1, $A1, and A1 reference styles and converts them appropriately.

CLI Execution Support

Generated code can be executed directly from the command line with support for custom inputs and JSON output.

Technical Architecture

Sheet2Code is built with modern TypeScript and follows clean architecture principles:

Why Choose Sheet2Code?

Professional Services

Need help converting complex Excel or Google Sheets? We offer professional consulting services for:

Contact us at