Skip to content

Commit

Permalink
Add function getFullCalcOnLoad
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkarex committed Oct 8, 2024
1 parent e94c280 commit 27fadb3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
23 changes: 23 additions & 0 deletions src/XlsxFastEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 27fadb3

Please sign in to comment.