Transform Excel and Google Sheets formulas into production-ready TypeScript or Python code
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.
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.
Advanced Chevrotain-based parser that understands complex nested formulas, array operations, and all standard Excel and Google Sheets syntax
Intelligent topological sorting ensures formulas are calculated in the correct order, with automatic circular dependency detection
Support for SUM, VLOOKUP, IF, INDEX/MATCH, SUMIF, array formulas, and most common Excel and Google Sheets functions
Automatically resolves named ranges and converts them to readable variable names in generated code
Handles cross-sheet references, automatically fetches referenced sheets, and maintains sheet relationships
Generated code includes error handling, type safety, and can be executed directly from command line
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.
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.
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.
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'];
# 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
# Watch mode - automatically regenerate on changes
npm run cli -- convert --config config.json --watch
npm run cli -- convert --config config.json --watch --watch-interval 60
# Run the generated code
node generated-code.js --json
# 1. Go to Google Sheets
# 2. File -> Import -> Upload your .xlsx file
# 3. Use the Google Sheets URL with the tool
The project includes ready-to-use XLSX files and configurations for common use cases:
# Financial modeling with loan calculations (PMT, NPV, IRR)
# Includes financial-model.xlsx ready for Google Sheets import
npm run cli -- convert --config examples/financial-model.json
# Statistical data analysis with 100 rows of sample data
# Includes data-analysis.xlsx with MEDIAN, PERCENTILE, SUMIFS
npm run cli -- convert --config examples/data-analysis.json
# Complete inventory management system
# Includes inventory-tracking.xlsx with EOQ and ABC classification
npm run cli -- convert --config examples/inventory-tracking.json
# Watch mode for automatic regeneration
npm run cli -- convert --config examples/financial-model.json --watch
Sheet2Code handles a comprehensive range of Excel and Google Sheets formulas and functions that work identically in both platforms:
Automatically regenerate code when your Google Sheet changes. Perfect for iterative development with configurable refresh intervals (--watch flag with --watch-interval option).
Intelligently handles sheets that contain both static data and formulas. Automatically detects and loads non-formula cells from output sheets when needed.
Full support for column-only ranges like A:A or D:D, commonly used in functions like SUMIF and COUNTIF for dynamic data ranges.
Automatically fetches and resolves named ranges from your Google Sheets, converting them to their actual cell references in the generated code.
Sheet2Code detects circular references and handles them gracefully, generating code with #REF! errors and clear warnings, just like Excel and Google Sheets would display.
Properly handles sheet names with spaces and special characters, like 'Q4 Sales Data'!A1 or 'John''s Report'!B2:B10.
Understands the difference between $A$1, A$1, $A1, and A1 reference styles and converts them appropriately.
Generated code can be executed directly from the command line with support for custom inputs and JSON output.
Sheet2Code is built with modern TypeScript and follows clean architecture principles:
Need help converting complex Excel or Google Sheets? We offer professional consulting services for:
Contact us at info@sheet2code.com