-
-
Notifications
You must be signed in to change notification settings - Fork 0
Add utils to process formula cells and formula calculation engine with advanced recursive dependency analysis #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: npv2k1 <73846954+npv2k1@users.noreply.github.com>
Co-authored-by: npv2k1 <73846954+npv2k1@users.noreply.github.com>
|
@copilot Please build engine tính toán công thức để tính toán các ô chưa có giá trị cache |
I've implemented a comprehensive formula calculation engine that can compute values for cells that don't have cached results. The engine includes: Features:
New Methods:
See the working example at All 36 tests pass, including comprehensive tests for the new calculation engine. |
Co-authored-by: npv2k1 <73846954+npv2k1@users.noreply.github.com>
| } | ||
| } | ||
|
|
||
| private getRangeValues(range: string): any[] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot if cell is formular you need calculate it first before get value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed the formula calculation to calculate dependent formulas recursively. Now when the engine encounters a formula cell while evaluating ranges or cell references, it automatically calculates the formula first if no cached result exists.
The fix updates both getCellValue and getRangeValues methods to:
- Check if a cell contains a formula
- If it has a cached result, use it
- If not, recursively calculate the formula and cache the result
- Return the calculated value
This ensures formulas like =SUM(A1:C1) work correctly even when the range contains other formula cells that need calculation first. All tests pass including new dependency tests in commit 8a2ab8b.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Co-authored-by: npv2k1 <73846954+npv2k1@users.noreply.github.com>
|
@copilot if cell still formular please calculate all value of it recursive |
Implemented full recursive formula calculation. Now when calculating any formula cell, all its dependencies are automatically calculated and cached recursively. Key improvements:
Example: Calculating a complex formula now ensures all dependencies are resolved: cursor.move('B4').setFormula('=SUM(B1:B3)'); // Where B2, B3 are also formulas
cursor.getCalculatedValue('B4'); // Automatically calculates B2, B3 first, then B4All 43 tests pass including new comprehensive recursive calculation tests in commit 95fb594. |
Co-authored-by: npv2k1 <73846954+npv2k1@users.noreply.github.com>
|


This PR adds comprehensive utilities for processing Excel formula cells, extracting their values, and a complete formula calculation engine that can compute values for cells that don't have cached results, with advanced recursive dependency analysis and automatic calculation of all formula dependencies.
New Functionality
ExcelCursor Methods
isFormulaCell(address?)- Check if a cell contains a formulagetFormula(address?)- Extract the formula string from a cellgetFormulaCellValue(address?)- Get calculated values from formula cells or raw values for regular cellsprocessFormulaCell(address?)- Get detailed analysis of any cell including formula status, formula string, calculated results, and raw valuesFormula Calculation Engine
calculateFormula(formula)- Calculate any formula directlygetCalculatedValue(address?)- Get computed values with full recursive dependency calculationcalculateAndUpdateFormulaCell(address?)- Calculate and cache formula resultcalculateAllFormulas()- Calculate all formulas in the worksheetcalculateFormulasRecursively(addresses)- Calculate formulas with complete dependency analysisextractCellReferences(formula)- Extract all cell references from a formula for dependency analysisUtility Functions
isFormulaCell(cell)- Static utility for formula detectiongetFormulaFromCell(cell)- Static utility for formula extractiongetFormulaCellValue(cell)- Static utility for value extractionprocessFormulaCell(cell)- Static utility for complete processingUsage Example
Key Features
Supported Operations
The formula calculation engine supports:
A1+B2,A1*2)Implementation Details
The utilities properly distinguish between:
The formula calculation engine includes a custom
FormulaEngineclass that can evaluate Excel-like formulas without external dependencies, properly handling cell references, ranges, and common functions. The engine now features advanced recursive dependency analysis - when evaluating any formula, it automatically identifies all dependencies, calculates them in the correct order, and caches all intermediate results.Advanced Dependency Handling: The enhanced
getCalculatedValue()method now performs complete dependency analysis usingcalculateFormulasRecursively(), ensuring that when calculating any formula, ALL its dependencies are properly resolved and cached. ThegetCellValue()andgetRangeValues()methods detect when a cell contains a formula and recursively calculate it with circular reference protection, ensuring accurate calculations for the most complex formula chains and range operations.Performance & Correctness: Results are automatically cached in cells for optimal performance, and the dependency analysis ensures that complex spreadsheet scenarios with multi-level formula relationships work correctly. All methods support working with the current cursor position when no address is provided, maintaining consistency with the existing API patterns.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.