diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6e383ed0..74773e34 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,6 +15,7 @@ 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 Reader [PR #2851](https://github.com/PHPOffice/PhpSpreadsheet/pull/2851)
+- Added Worksheet visibility in Ods Writer [PR #2850](https://github.com/PHPOffice/PhpSpreadsheet/pull/2850)
### Changed
diff --git a/src/PhpSpreadsheet/Writer/Ods/Cell/Style.php b/src/PhpSpreadsheet/Writer/Ods/Cell/Style.php
index 66194468..1bf2c463 100644
--- a/src/PhpSpreadsheet/Writer/Ods/Cell/Style.php
+++ b/src/PhpSpreadsheet/Writer/Ods/Cell/Style.php
@@ -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');
diff --git a/src/PhpSpreadsheet/Writer/Ods/Content.php b/src/PhpSpreadsheet/Writer/Ods/Content.php
index a8da1019..00ab0643 100644
--- a/src/PhpSpreadsheet/Writer/Ods/Content.php
+++ b/src/PhpSpreadsheet/Writer/Ods/Content.php
@@ -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) {
diff --git a/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php b/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php
index 917a0410..81c244a7 100644
--- a/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php
+++ b/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php
@@ -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);
+ }
}
diff --git a/tests/data/Writer/Ods/content-empty.xml b/tests/data/Writer/Ods/content-empty.xml
index 867cfa3e..84f4c239 100644
--- a/tests/data/Writer/Ods/content-empty.xml
+++ b/tests/data/Writer/Ods/content-empty.xml
@@ -3,6 +3,9 @@
+
+
+
@@ -12,7 +15,7 @@
-
+
diff --git a/tests/data/Writer/Ods/content-hidden-worksheet.xml b/tests/data/Writer/Ods/content-hidden-worksheet.xml
new file mode 100644
index 00000000..88a53257
--- /dev/null
+++ b/tests/data/Writer/Ods/content-hidden-worksheet.xml
@@ -0,0 +1,42 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 1
+
+
+
+
+
+
+
+
+ 2
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/tests/data/Writer/Ods/content-with-data.xml b/tests/data/Writer/Ods/content-with-data.xml
index fff47f65..12140fa9 100644
--- a/tests/data/Writer/Ods/content-with-data.xml
+++ b/tests/data/Writer/Ods/content-with-data.xml
@@ -3,6 +3,12 @@
+
+
+
+
+
+
@@ -62,7 +68,7 @@
-
+
@@ -107,7 +113,7 @@
-
+