-
Notifications
You must be signed in to change notification settings - Fork 383
[Feature Request] Add support for reading shared formulas in Excel files #366
Description
Summary
OpenXLSX currently throws Shared formulas not supported error when attempting to read formulas from cells that use Excel's shared formula feature. This limitation significantly impacts applications that need to process Excel files with complex formulas.
Problem Description
When reading formulas from Excel cells using cell.formula().get(), OpenXLSX throws an XLFormulaError with the message "Shared formulas not supported" for cells that contain shared formulas. This forces developers to either:
- Skip formula processing entirely
- Implement complex workarounds
- Manually reconstruct formulas
Steps to Reproduce
- Create an Excel file with formulas like
=IF(AND(B14="",C14=""),"",ROUND(E14*F14,2)) - Copy this formula to multiple cells (Excel automatically creates shared formulas)
- Try to read the formula using OpenXLSX:
auto cell = worksheet.cell(row, col);
if (cell.hasFormula()) {
try {
std::string formula = cell.formula().get(); // Throws exception here
}
catch (const OpenXLSX::XLFormulaError& ex) {
// Error: "Shared formulas not supported"
}
}Expected Behavior
OpenXLSX should be able to read shared formulas and return either:
- The expanded formula for the specific cell (preferred)
- The master formula with appropriate cell reference adjustments
- At minimum, the formula structure without throwing an exception
Current Workaround
Currently, we have to manually reconstruct formulas:
catch (const OpenXLSX::XLFormulaError& ex) {
// Manual reconstruction - not ideal
if (col == 7) { // G column
rowInfo.fmt[col - 1].formula = "=IF(AND(B" + std::to_string(row) +
"=\"\",C" + std::to_string(row) + "=\"\"),\"\",ROUND(E" +
std::to_string(row) + "*F" + std::to_string(row) + ",2))";
}
}Impact
This limitation affects:
- Enterprise applications processing financial spreadsheets
- Data migration tools that need to preserve formula logic
- Spreadsheet analysis tools requiring complete formula information
- Document processing pipelines handling complex Excel files
Technical Context
Shared formulas in Excel are stored as:
- A master formula in the first cell of the range
- References to the master formula in subsequent cells
- The
xl/worksheets/sheet1.xmlcontains<f t="shared">elements
Suggested Implementation
- Parse shared formula references from the worksheet XML
- Expand formulas by adjusting cell references relative to the target cell
- Provide API options for getting either the master formula or expanded formula
- Maintain backward compatibility with existing non-shared formula handling
Related Libraries
Other Excel libraries handle this successfully:
- openpyxl (Python): Reads shared formulas transparently
- xlnt (C++): May have better shared formula support
- Apache POI (Java): Handles shared formulas correctly
Use Case Priority
This is a high-priority feature for applications dealing with:
- Financial reports with repeated calculation patterns
- Template-based spreadsheets
- Data processing workflows
- Business intelligence tools
Environment
- OpenXLSX Version: [Current version]
- Platform: Cross-platform (Windows/Linux/macOS)
- Compiler: Modern C++ (C++17/20)
Request for Timeline
This feature would significantly improve OpenXLSX's utility for enterprise applications. Could the maintainers provide:
- An estimated timeline for this feature
- Whether community contributions would be welcome
- Technical guidance for potential contributors
Thank you for considering this enhancement. Shared formula support would make OpenXLSX much more competitive with other Excel processing libraries.