Skip to content

Commit

Permalink
Preserve workbook bookview attributes on update
Browse files Browse the repository at this point in the history
Fix for issue PHPOffice#523.  When reading then writing
a Xlsx spreadsheet, preserve the workbook bookview attributes for
the spreadsheet, (e.g., the attribute that hides worksheet tabs.)
Beforehand, these attributes were always being set to fixed constants.
  • Loading branch information
billblume committed May 31, 2018
1 parent dcc1832 commit 7c57e8b
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -1973,6 +1973,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

Expand Down
39 changes: 39 additions & 0 deletions src/PhpSpreadsheet/Spreadsheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?
*
Expand Down Expand Up @@ -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;
}
}
40 changes: 32 additions & 8 deletions src/PhpSpreadsheet/Writer/Xlsx/Workbook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
32 changes: 32 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Xlsx/HiddenTabsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;

use PhpOffice\PhpSpreadsheet\Settings;
use PHPUnit\Framework\TestCase;

class HiddenTabsTest extends TestCase
{
/**
* Test that the worksheet tabs remain hidden when reading and writing a XLSX document
* with hidden worksheets tabs.
*/
public function testUpdateWithHiddenTabs()
{
$sourceFilename = __DIR__ . '/../../../data/Writer/XLSX/hidden_tabs.xlsx';
Settings::setLibXmlLoaderOptions(null); // reset to default options
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$excel = $reader->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);
}
}
}
Binary file added tests/data/Writer/XLSX/hidden_tabs.xlsx
Binary file not shown.

0 comments on commit 7c57e8b

Please sign in to comment.