diff --git a/CHANGELOG.md b/CHANGELOG.md index 00e7748c0e..4bc5ec0a42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Support to read Xlsm templates with form elements, macros, printer settings, protected elements and back compatibility drawing, and save result without losing important elements of document - [#435](https://github.com/PHPOffice/PhpSpreadsheet/issues/435) - Expose sheet title maximum length as `Worksheet::SHEET_TITLE_MAXIMUM_LENGTH` - [#482](https://github.com/PHPOffice/PhpSpreadsheet/issues/482) - Allow escape character to be set in CSV reader – [#492](https://github.com/PHPOffice/PhpSpreadsheet/issues/492) +- Preserve workbook bookview attributes on update - [#523](https://github.com/PHPOffice/PhpSpreadsheet/issues/523) ### Fixed diff --git a/src/PhpSpreadsheet/Reader/Xlsx.php b/src/PhpSpreadsheet/Reader/Xlsx.php index f1694b21c8..c694233201 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx.php +++ b/src/PhpSpreadsheet/Reader/Xlsx.php @@ -1988,6 +1988,12 @@ public function load($pFilename) } if ((!$this->readDataOnly) || (!empty($this->loadSheetsOnly))) { + if ($xmlWorkbook->bookViews->workbookView) { + foreach ($xmlWorkbook->bookViews->workbookView->attributes() as $attr => $value) { + $excel->setWorkbookViewAttribute((string) $attr, (string) $value); + } + } + // active sheet index $activeTab = (int) ($xmlWorkbook->bookViews->workbookView['activeTab']); // refers to old sheet index diff --git a/src/PhpSpreadsheet/Spreadsheet.php b/src/PhpSpreadsheet/Spreadsheet.php index ff63223a8f..d67eb9539e 100644 --- a/src/PhpSpreadsheet/Spreadsheet.php +++ b/src/PhpSpreadsheet/Spreadsheet.php @@ -123,6 +123,13 @@ class Spreadsheet */ private $unparsedLoadedData = []; + /** + * workbookViewAttributes : null if workbook isn't Excel 2007 or doesn't have any workbook bookviews. + * + * @var null|array + */ + private $workbookViewAttributes; + /** * The workbook has macros ? * @@ -1216,4 +1223,36 @@ public function getID() { return $this->uniqueID; } + + /** + * Get a workbook bookview attribute. + * + * @param string $attr Name of attribute to read + * @param null|string $default Value to return if attribute doesn't exist + * + * @return string Value of attribute + */ + public function getWorkbookViewAttribute($attr, $default = null) + { + if ($this->workbookViewAttributes && isset($this->workbookViewAttributes[$attr])) { + return $this->workbookViewAttributes[$attr]; + } + + return $default; + } + + /** + * Set a workbook bookview attribute. + * + * @param string $attr Name of attribute to set + * @param string $value Value of the attribute + */ + public function setWorkbookViewAttribute($attr, $value) + { + if ($this->workbookViewAttributes === null) { + $this->workbookViewAttributes = []; + } + + $this->workbookViewAttributes[$attr] = $value; + } } diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Workbook.php b/src/PhpSpreadsheet/Writer/Xlsx/Workbook.php index cede7edb58..43811663fb 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Workbook.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Workbook.php @@ -117,14 +117,38 @@ private function writeBookViews(XMLWriter $objWriter, Spreadsheet $spreadsheet) $objWriter->startElement('workbookView'); $objWriter->writeAttribute('activeTab', $spreadsheet->getActiveSheetIndex()); - $objWriter->writeAttribute('autoFilterDateGrouping', '1'); - $objWriter->writeAttribute('firstSheet', '0'); - $objWriter->writeAttribute('minimized', '0'); - $objWriter->writeAttribute('showHorizontalScroll', '1'); - $objWriter->writeAttribute('showSheetTabs', '1'); - $objWriter->writeAttribute('showVerticalScroll', '1'); - $objWriter->writeAttribute('tabRatio', '600'); - $objWriter->writeAttribute('visibility', 'visible'); + $objWriter->writeAttribute( + 'autoFilterDateGrouping', + $spreadsheet->getWorkbookViewAttribute('autoFilterDateGrouping', '1') + ); + $objWriter->writeAttribute( + 'firstSheet', + $spreadsheet->getWorkbookViewAttribute('firstSheet', '0') + ); + $objWriter->writeAttribute( + 'minimized', + $spreadsheet->getWorkbookViewAttribute('minimized', '0') + ); + $objWriter->writeAttribute( + 'showHorizontalScroll', + $spreadsheet->getWorkbookViewAttribute('showHorizontalScroll', '1') + ); + $objWriter->writeAttribute( + 'showSheetTabs', + $spreadsheet->getWorkbookViewAttribute('showSheetTabs', '1') + ); + $objWriter->writeAttribute( + 'showVerticalScroll', + $spreadsheet->getWorkbookViewAttribute('showVerticalScroll', '1') + ); + $objWriter->writeAttribute( + 'tabRatio', + $spreadsheet->getWorkbookViewAttribute('tabRatio', '600') + ); + $objWriter->writeAttribute( + 'visibility', + $spreadsheet->getWorkbookViewAttribute('visibility', 'visible') + ); $objWriter->endElement(); diff --git a/tests/PhpSpreadsheetTests/Writer/Xlsx/HiddenTabsTest.php b/tests/PhpSpreadsheetTests/Writer/Xlsx/HiddenTabsTest.php new file mode 100644 index 0000000000..842630356b --- /dev/null +++ b/tests/PhpSpreadsheetTests/Writer/Xlsx/HiddenTabsTest.php @@ -0,0 +1,32 @@ +load($sourceFilename); + + $targetFilename = tempnam(sys_get_temp_dir(), 'tst'); + $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($excel); + $writer->save($targetFilename); + + try { + $excel2 = $reader->load($targetFilename); + $this->assertEquals('0', $excel2->getWorkbookViewAttribute('showSheetTabs')); + } finally { + @unlink($targetFilename); + } + } +} diff --git a/tests/data/Writer/XLSX/hidden_tabs.xlsx b/tests/data/Writer/XLSX/hidden_tabs.xlsx new file mode 100644 index 0000000000..b81f8b64b8 Binary files /dev/null and b/tests/data/Writer/XLSX/hidden_tabs.xlsx differ