Add support for writing Worksheet Visibility for Ods

This commit is contained in:
MarkBaker 2022-05-23 13:38:08 +02:00
parent 7c1c896959
commit 2e5ebea110
7 changed files with 104 additions and 3 deletions

View File

@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).
Note that a ChartSheet is still only written as a normal Worksheet containing a single chart, not as an actual ChartSheet.
- Added Worksheet visibility in Ods Writer [PR #2850](https://github.com/PHPOffice/PhpSpreadsheet/pull/2850)
### Changed
- Memory and speed improvements, particularly for the Cell Collection, and the Writers.

View File

@ -10,12 +10,14 @@ use PhpOffice\PhpSpreadsheet\Style\Font;
use PhpOffice\PhpSpreadsheet\Style\Style as CellStyle;
use PhpOffice\PhpSpreadsheet\Worksheet\ColumnDimension;
use PhpOffice\PhpSpreadsheet\Worksheet\RowDimension;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class Style
{
public const CELL_STYLE_PREFIX = 'ce';
public const COLUMN_STYLE_PREFIX = 'co';
public const ROW_STYLE_PREFIX = 'ro';
public const TABLE_STYLE_PREFIX = 'ta';
private $writer;
@ -221,6 +223,26 @@ class Style
$this->writer->endElement(); // Close style:style
}
public function writeTableStyle(Worksheet $worksheet, int $sheetId): void
{
$this->writer->startElement('style:style');
$this->writer->writeAttribute('style:family', 'table');
$this->writer->writeAttribute(
'style:name',
sprintf('%s%d', self::TABLE_STYLE_PREFIX, $sheetId)
);
$this->writer->startElement('style:table-properties');
$this->writer->writeAttribute(
'table:display',
$worksheet->getSheetState() === Worksheet::SHEETSTATE_VISIBLE ? 'true' : 'false'
);
$this->writer->endElement(); // Close style:table-properties
$this->writer->endElement(); // Close style:style
}
public function write(CellStyle $style): void
{
$this->writer->startElement('style:style');

View File

@ -123,6 +123,7 @@ class Content extends WriterPart
for ($sheetIndex = 0; $sheetIndex < $sheetCount; ++$sheetIndex) {
$objWriter->startElement('table:table');
$objWriter->writeAttribute('table:name', $spreadsheet->getSheet($sheetIndex)->getTitle());
$objWriter->writeAttribute('table:style-name', Style::TABLE_STYLE_PREFIX . (string) ($sheetIndex + 1));
$objWriter->writeElement('office:forms');
foreach ($spreadsheet->getSheet($sheetIndex)->getColumnDimensions() as $columnDimension) {
$objWriter->startElement('table:table-column');
@ -289,6 +290,8 @@ class Content extends WriterPart
$sheetCount = $spreadsheet->getSheetCount();
for ($i = 0; $i < $sheetCount; ++$i) {
$worksheet = $spreadsheet->getSheet($i);
$styleWriter->writeTableStyle($worksheet, $i + 1);
$worksheet->calculateColumnWidths();
foreach ($worksheet->getColumnDimensions() as $columnDimension) {
if ($columnDimension->getWidth() !== -1.0) {

View File

@ -10,6 +10,7 @@ use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Font;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Writer\Ods;
use PhpOffice\PhpSpreadsheet\Writer\Ods\Content;
use PHPUnit\Framework\TestCase;
@ -106,4 +107,26 @@ class ContentTest extends TestCase
self::assertXmlStringEqualsXmlFile($this->samplesPath . '/content-with-data.xml', $xml);
}
public function testWriteWithHiddenWorksheet(): void
{
$workbook = new Spreadsheet();
// Worksheet 1
$worksheet1 = $workbook->getActiveSheet();
$worksheet1->setCellValue('A1', 1);
// Worksheet 2
$worksheet2 = $workbook->createSheet();
$worksheet2->setTitle('New Worksheet');
$worksheet2->setCellValue('A1', 2);
$worksheet2->setSheetState(Worksheet::SHEETSTATE_HIDDEN);
// Write
$content = new Content(new Ods($workbook));
$xml = $content->write();
self::assertXmlStringEqualsXmlFile($this->samplesPath . '/content-hidden-worksheet.xml', $xml);
}
}

View File

@ -3,6 +3,9 @@
<office:scripts />
<office:font-face-decls />
<office:automatic-styles>
<style:style style:family="table" style:name="ta1">
<style:table-properties table:display="true" />
</style:style>
<style:style style:name="ce0" style:family="table-cell" style:parent-style-name="Default">
<style:table-cell-properties style:rotation-align="none" style:vertical-align="bottom" />
<style:paragraph-properties fo:text-align="start" />
@ -12,7 +15,7 @@
<office:body>
<office:spreadsheet>
<table:calculation-settings />
<table:table table:name="Worksheet">
<table:table table:name="Worksheet" table:style-name="ta1">
<office:forms />
<table:table-row>
<table:table-cell table:style-name="ce0" />

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.2">
<office:scripts />
<office:font-face-decls />
<office:automatic-styles>
<style:style style:family="table" style:name="ta1">
<style:table-properties table:display="true" />
</style:style>
<style:style style:family="table" style:name="ta2">
<style:table-properties table:display="false"/>
</style:style>
<style:style style:name="ce0" style:family="table-cell" style:parent-style-name="Default">
<style:table-cell-properties style:rotation-align="none" style:vertical-align="bottom" />
<style:paragraph-properties fo:text-align="start" />
<style:text-properties fo:color="#000000" fo:font-family="Calibri" fo:font-size="11.0pt" />
</style:style>
</office:automatic-styles>
<office:body>
<office:spreadsheet>
<table:calculation-settings />
<table:table table:name="Worksheet" table:style-name="ta1">
<office:forms />
<table:table-row>
<table:table-cell office:value="1" office:value-type="float" table:style-name="ce0">
<text:p>1</text:p>
</table:table-cell>
<table:table-cell table:number-columns-repeated="1023"/>
</table:table-row>
</table:table>
<table:table table:name="New Worksheet" table:style-name="ta2">
<office:forms/>
<table:table-row>
<table:table-cell office:value="2" office:value-type="float" table:style-name="ce0">
<text:p>2</text:p>
</table:table-cell>
<table:table-cell table:number-columns-repeated="1023" />
</table:table-row>
</table:table>
<table:named-expressions />
</office:spreadsheet>
</office:body>
</office:document-content>

View File

@ -3,6 +3,12 @@
<office:scripts/>
<office:font-face-decls/>
<office:automatic-styles>
<style:style style:family="table" style:name="ta1">
<style:table-properties table:display="true" />
</style:style>
<style:style style:family="table" style:name="ta2">
<style:table-properties table:display="true" />
</style:style>
<style:style style:family="table-cell" style:name="ce0" style:parent-style-name="Default">
<style:table-cell-properties style:rotation-align="none" style:vertical-align="bottom" />
<style:paragraph-properties fo:text-align="start" />
@ -62,7 +68,7 @@
<office:body>
<office:spreadsheet>
<table:calculation-settings/>
<table:table table:name="Worksheet">
<table:table table:name="Worksheet" table:style-name="ta1">
<office:forms/>
<table:table-row>
<table:table-cell office:value="1" office:value-type="float" table:style-name="ce2">
@ -107,7 +113,7 @@
<table:table-cell table:number-columns-repeated="1017"/>
</table:table-row>
</table:table>
<table:table table:name="New Worksheet">
<table:table table:name="New Worksheet" table:style-name="ta2">
<office:forms/>
<table:table-row>
<table:table-cell office:value="2" office:value-type="float" table:style-name="ce0">