Skip to content

Commit

Permalink
Recognize 'Hidden' Attribute in Xml Spreadsheet
Browse files Browse the repository at this point in the history
Fix PHPOffice#3566; PhpSpreadsheet should now handle Hidden attribute for rows and columns in Xml spreadsheets. Also added the ability to load Xml spreadsheet from string rather than file, as can be done for Csv and Html.
  • Loading branch information
oleibman committed May 14, 2023
1 parent e9cf273 commit dcab826
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/PhpSpreadsheet/Reader/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,19 @@ public function listWorksheetInfo($filename)
return $worksheetInfo;
}

/**
* Loads Spreadsheet from string.
*/
public function loadSpreadsheetFromString(string $contents): Spreadsheet
{
// Create new Spreadsheet
$spreadsheet = new Spreadsheet();
$spreadsheet->removeSheetByIndex(0);

// Load into this instance
return $this->loadIntoExisting($contents, $spreadsheet, true);
}

/**
* Loads Spreadsheet from file.
*/
Expand All @@ -245,17 +258,19 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
}

/**
* Loads from file into Spreadsheet instance.
*
* @param string $filename
* Loads from file or contents into Spreadsheet instance.
*
* @return Spreadsheet
* @param string $filename file name if useContents is false else file contents
*/
public function loadIntoExisting($filename, Spreadsheet $spreadsheet)
public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet, bool $useContents = false): Spreadsheet
{
File::assertFile($filename);
if (!$this->canRead($filename)) {
throw new Exception($filename . ' is an Invalid Spreadsheet file.');
if ($useContents) {
$this->fileContents = $filename;
} else {
File::assertFile($filename);
if (!$this->canRead($filename)) {
throw new Exception($filename . ' is an Invalid Spreadsheet file.');
}
}

$xml = $this->trySimpleXMLLoadString($filename);
Expand Down Expand Up @@ -321,6 +336,10 @@ public function loadIntoExisting($filename, Spreadsheet $spreadsheet)
$columnWidth = $columnData_ss['Width'];
$spreadsheet->getActiveSheet()->getColumnDimension($columnID)->setWidth($columnWidth / 5.4);
}
if (isset($columnData_ss['Hidden'])) {
$columnVisible = ((string) $columnData_ss['Hidden']) !== '1';
$spreadsheet->getActiveSheet()->getColumnDimension($columnID)->setVisible($columnVisible);
}
++$columnID;
}
}
Expand All @@ -334,6 +353,10 @@ public function loadIntoExisting($filename, Spreadsheet $spreadsheet)
if (isset($row_ss['Index'])) {
$rowID = (int) $row_ss['Index'];
}
if (isset($row_ss['Hidden'])) {
$rowVisible = ((string) $row_ss['Hidden']) !== '1';
$spreadsheet->getActiveSheet()->getRowDimension($rowID)->setVisible($rowVisible);
}

$columnID = 'A';
foreach ($rowData->Cell as $cell) {
Expand Down
57 changes: 57 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xml/XmlColumnRowHiddenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xml;

use PhpOffice\PhpSpreadsheet\Reader\Xml;
use PHPUnit\Framework\TestCase;

class XmlColumnRowHiddenTest extends TestCase
{
public function testWriteThenRead(): void
{
$xmldata = <<< 'EOT'
<?xml version="1.0" encoding="UTF-8"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="1">
<ss:Table>
<Column ss:Hidden="1" />
<Column ss:Hidden="0" />
<Column />
<ss:Row ss:Hidden="1">
<ss:Cell>
<ss:Data ss:Type="String">hidden row and hidden column</ss:Data>
</ss:Cell>
<ss:Cell>
<ss:Data ss:Type="String">hidden row and visible column</ss:Data>
</ss:Cell>
</ss:Row>
<ss:Row ss:Hidden="0">
<ss:Cell>
<ss:Data ss:Type="String">visible row and hidden column</ss:Data>
</ss:Cell>
<ss:Cell>
<ss:Data ss:Type="String">visible row and visible column</ss:Data>
</ss:Cell>
</ss:Row>
<ss:Row />
</ss:Table>
</Worksheet>
</Workbook>
EOT;
$reader = new Xml();
$spreadsheet = $reader->loadSpreadsheetFromString($xmldata);
self::assertEquals(1, $spreadsheet->getSheetCount());

$sheet = $spreadsheet->getActiveSheet();
self::assertEquals('1', $sheet->getTitle());
self::assertFalse($sheet->getColumnDimension('A')->getVisible());
self::assertTrue($sheet->getColumnDimension('B')->getVisible());
self::assertTrue($sheet->getColumnDimension('C')->getVisible());
self::assertFalse($sheet->getRowDimension(1)->getVisible());
self::assertTrue($sheet->getRowDimension(2)->getVisible());
self::assertTrue($sheet->getRowDimension(3)->getVisible());

$spreadsheet->disconnectWorksheets();
}
}
1 change: 1 addition & 0 deletions tests/PhpSpreadsheetTests/Reader/Xml/XmlOddTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ public function testWriteThenRead(): void
$props = $spreadsheet->getProperties();
self::assertEquals('Xml2003 Short Workbook', $props->getTitle());
self::assertEquals('2', $props->getCustomPropertyValue('myאInt'));
$spreadsheet->disconnectWorksheets();
}
}

0 comments on commit dcab826

Please sign in to comment.