From 27fadb302a2be45d7e9d963e9bbcf55560d0ff29 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Tue, 8 Oct 2024 15:36:05 +0200 Subject: [PATCH] Add function getFullCalcOnLoad --- README.md | 2 ++ src/XlsxFastEditor.php | 23 +++++++++++++++++++++++ tests/test.php | 2 ++ 3 files changed, 27 insertions(+) diff --git a/README.md b/README.md index 1380089..79eb5c8 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,10 @@ try { $nbWorksheets = $xlsxFastEditor->getWorksheetCount(); $worksheetName = $xlsxFastEditor->getWorksheetName(1); $worksheetId1 = $xlsxFastEditor->getWorksheetNumber('Sheet1'); + // If you want to force Excel to recalculate formulas on next load: $xlsxFastEditor->setFullCalcOnLoad($worksheetId1, true); + $fullCalcOnLoad = $xlsxFastEditor->getFullCalcOnLoad($worksheetId1); // Direct read/write access $fx = $xlsxFastEditor->readFormula($worksheetId1, 'A1'); diff --git a/src/XlsxFastEditor.php b/src/XlsxFastEditor.php index d27ed50..8d43f7f 100644 --- a/src/XlsxFastEditor.php +++ b/src/XlsxFastEditor.php @@ -358,6 +358,29 @@ public function setFullCalcOnLoad(int $sheetNumber, bool $value): void } } + /** + * Get the *Full calculation on load* policy for the specified worksheet. + * @param int $sheetNumber Worksheet number (base 1) + * @throws XlsxFastEditorFileFormatException + * @throws XlsxFastEditorXmlException + */ + public function getFullCalcOnLoad(int $sheetNumber): ?bool + { + $dom = $this->getDomFromPath(self::getWorksheetPath($sheetNumber)); + $sheetCalcPrs = $dom->getElementsByTagName('sheetCalcPr'); + if ($sheetCalcPrs->length > 0) { + $sheetCalcPr = $sheetCalcPrs[0]; + if ($sheetCalcPr instanceof \DOMElement) { + $fullCalcOnLoad = $sheetCalcPr->getAttribute('fullCalcOnLoad'); + if ($fullCalcOnLoad !== '') { + $fullCalcOnLoad = strtolower(trim($fullCalcOnLoad)); + return in_array($fullCalcOnLoad, ['true', '1'], true); + } + } + } + return null; + } + /** * Get the row of the given number in the given worksheet. * @param int $sheetNumber Worksheet number (base 1) diff --git a/tests/test.php b/tests/test.php index a6eef8c..66a688f 100644 --- a/tests/test.php +++ b/tests/test.php @@ -134,7 +134,9 @@ // Regex assert($xlsxFastEditor->textReplace('/Hello/i', 'World') > 0); + assert($xlsxFastEditor->getFullCalcOnLoad($sheet1) == null); $xlsxFastEditor->setFullCalcOnLoad($sheet1, true); + assert($xlsxFastEditor->getFullCalcOnLoad($sheet1) === true); $xlsxFastEditor->save();