From 13bf3d43618b835151ab80095d5ad4b867556a3a Mon Sep 17 00:00:00 2001 From: Paolo Agostinetto Date: Tue, 28 Feb 2017 21:05:18 +0100 Subject: [PATCH 1/7] Writer\Ods\Content: making use of the "use" statement rather than explicit namespaces --- src/PhpSpreadsheet/Writer/Ods/Content.php | 62 +++++++++++++---------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/Ods/Content.php b/src/PhpSpreadsheet/Writer/Ods/Content.php index 0d960d6d..a6d7767f 100644 --- a/src/PhpSpreadsheet/Writer/Ods/Content.php +++ b/src/PhpSpreadsheet/Writer/Ods/Content.php @@ -27,6 +27,14 @@ namespace PhpOffice\PhpSpreadsheet\Writer\Ods; * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL */ +use PhpOffice\PhpSpreadsheet\Cell; +use PhpOffice\PhpSpreadsheet\Shared\XMLWriter; +use PhpOffice\PhpSpreadsheet\Spreadsheet; +use PhpOffice\PhpSpreadsheet\Worksheet; +use PhpOffice\PhpSpreadsheet\Cell\DataType; +use PhpOffice\PhpSpreadsheet\Writer\Exception; +use PhpOffice\PhpSpreadsheet\Writer\Ods\Cell\Comment; + /** * @category PhpSpreadsheet * @@ -47,17 +55,17 @@ class Content extends WriterPart * * @return string XML Output */ - public function write(\PhpOffice\PhpSpreadsheet\SpreadSheet $spreadsheet = null) + public function write(Spreadsheet $spreadsheet = null) { if (!$spreadsheet) { - $spreadsheet = $this->getParentWriter()->getSpreadsheet(); /* @var $spreadsheet PhpSpreadsheet */ + $spreadsheet = $this->getParentWriter()->getSpreadsheet(); /* @var $spreadsheet Spreadsheet */ } $objWriter = null; if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); + $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); } else { - $objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_MEMORY); + $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); } // XML header @@ -118,11 +126,11 @@ class Content extends WriterPart /** * Write sheets. * - * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter + * @param XMLWriter $objWriter */ - private function writeSheets(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter) + private function writeSheets(XMLWriter $objWriter) { - $spreadsheet = $this->getParentWriter()->getSpreadsheet(); /* @var $spreadsheet PhpSpreadsheet */ + $spreadsheet = $this->getParentWriter()->getSpreadsheet(); /* @var $spreadsheet Spreadsheet */ $sheet_count = $spreadsheet->getSheetCount(); for ($i = 0; $i < $sheet_count; ++$i) { @@ -140,10 +148,10 @@ class Content extends WriterPart /** * Write rows of the specified sheet. * - * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter - * @param \PhpOffice\PhpSpreadsheet\Worksheet $sheet + * @param XMLWriter $objWriter + * @param Worksheet $sheet */ - private function writeRows(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter, \PhpOffice\PhpSpreadsheet\Worksheet $sheet) + private function writeRows(XMLWriter $objWriter, Worksheet $sheet) { $number_rows_repeated = self::NUMBER_ROWS_REPEATED_MAX; $span_row = 0; @@ -176,36 +184,36 @@ class Content extends WriterPart /** * Write cells of the specified row. * - * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter - * @param \PhpOffice\PhpSpreadsheet\Worksheet\Row $row + * @param XMLWriter $objWriter + * @param Worksheet\Row $row * - * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception + * @throws Exception */ - private function writeCells(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter, \PhpOffice\PhpSpreadsheet\Worksheet\Row $row) + private function writeCells(XMLWriter $objWriter, Worksheet\Row $row) { $number_cols_repeated = self::NUMBER_COLS_REPEATED_MAX; $prev_column = -1; $cells = $row->getCellIterator(); while ($cells->valid()) { $cell = $cells->current(); - $column = \PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($cell->getColumn()) - 1; + $column = Cell::columnIndexFromString($cell->getColumn()) - 1; $this->writeCellSpan($objWriter, $column, $prev_column); $objWriter->startElement('table:table-cell'); switch ($cell->getDataType()) { - case \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_BOOL: + case DataType::TYPE_BOOL: $objWriter->writeAttribute('office:value-type', 'boolean'); $objWriter->writeAttribute('office:value', $cell->getValue()); $objWriter->writeElement('text:p', $cell->getValue()); break; - case \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_ERROR: - throw new \PhpOffice\PhpSpreadsheet\Writer\Exception('Writing of error not implemented yet.'); + case DataType::TYPE_ERROR: + throw new Exception('Writing of error not implemented yet.'); break; - case \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_FORMULA: + case DataType::TYPE_FORMULA: try { $formula_value = $cell->getCalculatedValue(); - } catch (Exception $e) { + } catch (\Exception $e) { $formula_value = $cell->getValue(); } $objWriter->writeAttribute('table:formula', 'of:' . $cell->getValue()); @@ -217,20 +225,20 @@ class Content extends WriterPart $objWriter->writeAttribute('office:value', $formula_value); $objWriter->writeElement('text:p', $formula_value); break; - case \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_INLINE: - throw new \PhpOffice\PhpSpreadsheet\Writer\Exception('Writing of inline not implemented yet.'); + case DataType::TYPE_INLINE: + throw new Exception('Writing of inline not implemented yet.'); break; - case \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NUMERIC: + case DataType::TYPE_NUMERIC: $objWriter->writeAttribute('office:value-type', 'float'); $objWriter->writeAttribute('office:value', $cell->getValue()); $objWriter->writeElement('text:p', $cell->getValue()); break; - case \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING: + case DataType::TYPE_STRING: $objWriter->writeAttribute('office:value-type', 'string'); $objWriter->writeElement('text:p', $cell->getValue()); break; } - Cell\Comment::write($objWriter, $cell); + Comment::write($objWriter, $cell); $objWriter->endElement(); $prev_column = $column; $cells->next(); @@ -250,11 +258,11 @@ class Content extends WriterPart /** * Write span. * - * @param \PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter + * @param XMLWriter $objWriter * @param int $curColumn * @param int $prevColumn */ - private function writeCellSpan(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter $objWriter, $curColumn, $prevColumn) + private function writeCellSpan(XMLWriter $objWriter, $curColumn, $prevColumn) { $diff = $curColumn - $prevColumn - 1; if (1 === $diff) { From 8600e631234a51e4dae8ef0583b262b893727d46 Mon Sep 17 00:00:00 2001 From: Paolo Agostinetto Date: Sat, 4 Mar 2017 14:54:42 +0100 Subject: [PATCH 2/7] Writer\Ods\Content: added docblock and removed unnecessary method parameter --- src/PhpSpreadsheet/Writer/Ods/Content.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/Ods/Content.php b/src/PhpSpreadsheet/Writer/Ods/Content.php index a6d7767f..5fbf2ac8 100644 --- a/src/PhpSpreadsheet/Writer/Ods/Content.php +++ b/src/PhpSpreadsheet/Writer/Ods/Content.php @@ -33,10 +33,12 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet; use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Writer\Exception; +use PhpOffice\PhpSpreadsheet\Writer\Ods; use PhpOffice\PhpSpreadsheet\Writer\Ods\Cell\Comment; /** * @category PhpSpreadsheet + * @method Ods getParentWriter * * @copyright Copyright (c) 2006 - 2015 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet) * @author Alexander Pervakov @@ -49,18 +51,12 @@ class Content extends WriterPart /** * Write content.xml to XML format. * - * @param \PhpOffice\PhpSpreadsheet\Spreadsheet $spreadsheet - * * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception * * @return string XML Output */ - public function write(Spreadsheet $spreadsheet = null) + public function write() { - if (!$spreadsheet) { - $spreadsheet = $this->getParentWriter()->getSpreadsheet(); /* @var $spreadsheet Spreadsheet */ - } - $objWriter = null; if ($this->getParentWriter()->getUseDiskCaching()) { $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); @@ -114,7 +110,9 @@ class Content extends WriterPart $objWriter->startElement('office:body'); $objWriter->startElement('office:spreadsheet'); $objWriter->writeElement('table:calculation-settings'); + $this->writeSheets($objWriter); + $objWriter->writeElement('table:named-expressions'); $objWriter->endElement(); $objWriter->endElement(); From e60897ae5f6447324cd680a5f58dc02cecbef57c Mon Sep 17 00:00:00 2001 From: Paolo Agostinetto Date: Sat, 4 Mar 2017 15:02:10 +0100 Subject: [PATCH 3/7] Writer\Ods\Content: all variable names to camelcase --- src/PhpSpreadsheet/Writer/Ods/Content.php | 34 +++++++++++------------ 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/Ods/Content.php b/src/PhpSpreadsheet/Writer/Ods/Content.php index 5fbf2ac8..5ad90f95 100644 --- a/src/PhpSpreadsheet/Writer/Ods/Content.php +++ b/src/PhpSpreadsheet/Writer/Ods/Content.php @@ -130,8 +130,8 @@ class Content extends WriterPart { $spreadsheet = $this->getParentWriter()->getSpreadsheet(); /* @var $spreadsheet Spreadsheet */ - $sheet_count = $spreadsheet->getSheetCount(); - for ($i = 0; $i < $sheet_count; ++$i) { + $sheetCount = $spreadsheet->getSheetCount(); + for ($i = 0; $i < $sheetCount; ++$i) { $objWriter->startElement('table:table'); $objWriter->writeAttribute('table:name', $spreadsheet->getSheet($i)->getTitle()); $objWriter->writeElement('office:forms'); @@ -151,11 +151,11 @@ class Content extends WriterPart */ private function writeRows(XMLWriter $objWriter, Worksheet $sheet) { - $number_rows_repeated = self::NUMBER_ROWS_REPEATED_MAX; + $numberRowsRepeated = self::NUMBER_ROWS_REPEATED_MAX; $span_row = 0; $rows = $sheet->getRowIterator(); while ($rows->valid()) { - --$number_rows_repeated; + --$numberRowsRepeated; $row = $rows->current(); if ($row->getCellIterator()->valid()) { if ($span_row) { @@ -189,14 +189,14 @@ class Content extends WriterPart */ private function writeCells(XMLWriter $objWriter, Worksheet\Row $row) { - $number_cols_repeated = self::NUMBER_COLS_REPEATED_MAX; - $prev_column = -1; + $numberColsRepeated = self::NUMBER_COLS_REPEATED_MAX; + $prevColumn = -1; $cells = $row->getCellIterator(); while ($cells->valid()) { $cell = $cells->current(); $column = Cell::columnIndexFromString($cell->getColumn()) - 1; - $this->writeCellSpan($objWriter, $column, $prev_column); + $this->writeCellSpan($objWriter, $column, $prevColumn); $objWriter->startElement('table:table-cell'); switch ($cell->getDataType()) { @@ -210,18 +210,18 @@ class Content extends WriterPart break; case DataType::TYPE_FORMULA: try { - $formula_value = $cell->getCalculatedValue(); + $formulaValue = $cell->getCalculatedValue(); } catch (\Exception $e) { - $formula_value = $cell->getValue(); + $formulaValue = $cell->getValue(); } $objWriter->writeAttribute('table:formula', 'of:' . $cell->getValue()); - if (is_numeric($formula_value)) { + if (is_numeric($formulaValue)) { $objWriter->writeAttribute('office:value-type', 'float'); } else { $objWriter->writeAttribute('office:value-type', 'string'); } - $objWriter->writeAttribute('office:value', $formula_value); - $objWriter->writeElement('text:p', $formula_value); + $objWriter->writeAttribute('office:value', $formulaValue); + $objWriter->writeElement('text:p', $formulaValue); break; case DataType::TYPE_INLINE: throw new Exception('Writing of inline not implemented yet.'); @@ -238,14 +238,14 @@ class Content extends WriterPart } Comment::write($objWriter, $cell); $objWriter->endElement(); - $prev_column = $column; + $prevColumn = $column; $cells->next(); } - $number_cols_repeated = $number_cols_repeated - $prev_column - 1; - if ($number_cols_repeated > 0) { - if ($number_cols_repeated > 1) { + $numberColsRepeated = $numberColsRepeated - $prevColumn - 1; + if ($numberColsRepeated > 0) { + if ($numberColsRepeated > 1) { $objWriter->startElement('table:table-cell'); - $objWriter->writeAttribute('table:number-columns-repeated', $number_cols_repeated); + $objWriter->writeAttribute('table:number-columns-repeated', $numberColsRepeated); $objWriter->endElement(); } else { $objWriter->writeElement('table:table-cell'); From 562051099102ef442a13e59f0d6b490efa14238f Mon Sep 17 00:00:00 2001 From: Paolo Agostinetto Date: Sat, 4 Mar 2017 15:22:20 +0100 Subject: [PATCH 4/7] Writer\Ods\Content: basic unit tests --- .../Writer/Ods/ContentTest.php | 62 +++++++++++++++++++ tests/data/Writer/Ods/content-empty.xml | 20 ++++++ tests/data/Writer/Ods/content-with-data.xml | 60 ++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php create mode 100644 tests/data/Writer/Ods/content-empty.xml create mode 100644 tests/data/Writer/Ods/content-with-data.xml diff --git a/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php b/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php new file mode 100644 index 00000000..70d920dc --- /dev/null +++ b/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php @@ -0,0 +1,62 @@ +setParentWriter(new Ods(new Spreadsheet())); + + $xml = $content->write(); + + $this->assertXmlStringEqualsXmlFile($this->samplesPath . "/content-empty.xml", $xml); + } + + public function testWriteSpreadsheet() + { + $workbook = new Spreadsheet(); + + // Worksheet 1 + $worksheet1 = $workbook->getActiveSheet(); + $worksheet1->setCellValue('A1', 1); // Number + $worksheet1->setCellValue('B1', 12345.6789); // Number + $worksheet1->setCellValue('C1', "1"); // Number without cast + $worksheet1->setCellValueExplicit('D1', "01234", DataType::TYPE_STRING); // Number casted to string + $worksheet1->setCellValue('E1', "Lorem ipsum"); // String + + $worksheet1->setCellValue('A2', true); // Boolean + $worksheet1->setCellValue('B2', false); // Boolean + $worksheet1->setCellValue('C2', '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'); // Formula + + $worksheet1->setCellValue('D2', Date::PHPToExcel(1488635026)); // Date + $worksheet1->getStyle('D2') + ->getNumberFormat() + ->setFormatCode(NumberFormat::FORMAT_DATE_DATETIME); + + // Worksheet 2 + $worksheet2 = $workbook->createSheet(); + $worksheet2->setTitle('New Worksheet'); + $worksheet2->setCellValue('A1', 2); + + // Write + $content = new Content(); + $content->setParentWriter(new Ods($workbook)); + + $xml = $content->write(); + + $this->assertXmlStringEqualsXmlFile($this->samplesPath . "/content-with-data.xml", $xml); + } +} diff --git a/tests/data/Writer/Ods/content-empty.xml b/tests/data/Writer/Ods/content-empty.xml new file mode 100644 index 00000000..67cc35f2 --- /dev/null +++ b/tests/data/Writer/Ods/content-empty.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ 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 new file mode 100644 index 00000000..42f87553 --- /dev/null +++ b/tests/data/Writer/Ods/content-with-data.xml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + 1 + + + 12345.6789 + + + 1 + + + 01234 + + + Lorem ipsum + + + + + + 1 + + + + + + 1 TRUE + + + 42798.572060185 + + + + + + + + + + + 2 + + + + + + + + \ No newline at end of file From 4efda2a512af26d75635d9050f832362f91ec59c Mon Sep 17 00:00:00 2001 From: Paolo Agostinetto Date: Sat, 4 Mar 2017 16:18:56 +0100 Subject: [PATCH 5/7] Ods\Writer\Content: added support for xr styles (bold, italic, font family, font size, underline, bg color) --- src/PhpSpreadsheet/Writer/Ods/Content.php | 120 +++++++++++++++++- .../Writer/Ods/ContentTest.php | 16 +++ tests/data/Writer/Ods/content-empty.xml | 9 +- tests/data/Writer/Ods/content-with-data.xml | 69 ++++++++-- 4 files changed, 199 insertions(+), 15 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/Ods/Content.php b/src/PhpSpreadsheet/Writer/Ods/Content.php index 5ad90f95..a7c5556f 100644 --- a/src/PhpSpreadsheet/Writer/Ods/Content.php +++ b/src/PhpSpreadsheet/Writer/Ods/Content.php @@ -30,6 +30,8 @@ namespace PhpOffice\PhpSpreadsheet\Writer\Ods; use PhpOffice\PhpSpreadsheet\Cell; use PhpOffice\PhpSpreadsheet\Shared\XMLWriter; use PhpOffice\PhpSpreadsheet\Spreadsheet; +use PhpOffice\PhpSpreadsheet\Style\Fill; +use PhpOffice\PhpSpreadsheet\Style\Font; use PhpOffice\PhpSpreadsheet\Worksheet; use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Writer\Exception; @@ -47,6 +49,7 @@ class Content extends WriterPart { const NUMBER_COLS_REPEATED_MAX = 1024; const NUMBER_ROWS_REPEATED_MAX = 1048576; + const CELL_STYLE_PREFIX = 'ce'; /** * Write content.xml to XML format. @@ -105,7 +108,11 @@ class Content extends WriterPart $objWriter->writeElement('office:scripts'); $objWriter->writeElement('office:font-face-decls'); - $objWriter->writeElement('office:automatic-styles'); + + // Styles XF + $objWriter->startElement('office:automatic-styles'); + $this->writeXfStyles($objWriter, $this->getParentWriter()->getSpreadsheet()); + $objWriter->endElement(); $objWriter->startElement('office:body'); $objWriter->startElement('office:spreadsheet'); @@ -193,12 +200,20 @@ class Content extends WriterPart $prevColumn = -1; $cells = $row->getCellIterator(); while ($cells->valid()) { + + /** @var Cell $cell */ $cell = $cells->current(); $column = Cell::columnIndexFromString($cell->getColumn()) - 1; $this->writeCellSpan($objWriter, $column, $prevColumn); $objWriter->startElement('table:table-cell'); + // Style XF + $style = $cell->getXfIndex(); + if($style !== null){ + $objWriter->writeAttribute('table:style-name', self::CELL_STYLE_PREFIX.$style); + } + switch ($cell->getDataType()) { case DataType::TYPE_BOOL: $objWriter->writeAttribute('office:value-type', 'boolean'); @@ -271,4 +286,107 @@ class Content extends WriterPart $objWriter->endElement(); } } + + /** + * Write XF cell styles + * + * @param XMLWriter $writer + * @param Spreadsheet $spreadsheet + */ + private function writeXfStyles(XMLWriter $writer, Spreadsheet $spreadsheet) + { + foreach($spreadsheet->getCellXfCollection() as $style) { + + $writer->startElement('style:style'); + $writer->writeAttribute('style:name', self::CELL_STYLE_PREFIX . $style->getIndex()); + $writer->writeAttribute('style:family', 'table-cell'); + $writer->writeAttribute('style:parent-style-name', 'Default'); + + /* + * style:text-properties + */ + + // Font + $writer->startElement('style:text-properties'); + + $font = $style->getFont(); + + if($font->getBold()) { + $writer->writeAttribute('fo:font-weight', 'bold'); + $writer->writeAttribute('style:font-weight-complex', 'bold'); + $writer->writeAttribute('style:font-weight-asian', 'bold'); + } + + if($font->getItalic()) { + $writer->writeAttribute('fo:font-style', 'italic'); + } + + if($color = $font->getColor()) { + $writer->writeAttribute('fo:color', sprintf('#%s', $color->getRGB())); + } + + if($family = $font->getName()) { + $writer->writeAttribute('fo:font-family', $family); + } + + if($size = $font->getSize()) { + $writer->writeAttribute('fo:font-size', sprintf('%.1fpt', $size)); + } + + if($font->getUnderline() && $font->getUnderline() != Font::UNDERLINE_NONE) { + + $writer->writeAttribute('style:text-underline-style', 'solid'); + $writer->writeAttribute('style:text-underline-width', 'auto'); + $writer->writeAttribute('style:text-underline-color', 'font-color'); + + switch($font->getUnderline()){ + + case Font::UNDERLINE_DOUBLE: + $writer->writeAttribute('style:text-underline-type', 'double'); + break; + + case Font::UNDERLINE_SINGLE: + $writer->writeAttribute('style:text-underline-type', 'single'); + break; + } + } + + $writer->endElement(); // Close style:text-properties + + /* + * style:table-cell-properties + */ + + $writer->startElement('style:table-cell-properties'); + $writer->writeAttribute('style:rotation-align', 'none'); + + // Fill + if($fill = $style->getFill()) { + switch($fill->getFillType()) { + + case Fill::FILL_SOLID: + $writer->writeAttribute('fo:background-color', sprintf('#%s', + strtolower($fill->getStartColor()->getRGB()) + )); + break; + + case Fill::FILL_GRADIENT_LINEAR: + case Fill::FILL_GRADIENT_PATH: + /// TODO :: To be implemented + break; + + case Fill::FILL_NONE: + default: + } + } + + $writer->endElement(); // Close style:table-cell-properties + + /* + * End + */ + + $writer->endElement(); // Close style:style + } + } } diff --git a/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php b/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php index 70d920dc..baf22703 100644 --- a/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php @@ -6,6 +6,8 @@ use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Shared\Date; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Color; +use PhpOffice\PhpSpreadsheet\Style\Fill; +use PhpOffice\PhpSpreadsheet\Style\Font; use PhpOffice\PhpSpreadsheet\Style\NumberFormat; use PhpOffice\PhpSpreadsheet\Writer\Ods; use PhpOffice\PhpSpreadsheet\Writer\Ods\Content; @@ -46,6 +48,20 @@ class ContentTest extends \PHPUnit_Framework_TestCase ->getNumberFormat() ->setFormatCode(NumberFormat::FORMAT_DATE_DATETIME); + // Styles + $worksheet1->getStyle('A1')->getFont()->setBold(true); + $worksheet1->getStyle('B1')->getFont()->setItalic(true); + $worksheet1->getStyle('C1')->getFont()->setName("Courier"); + $worksheet1->getStyle('C1')->getFont()->setSize(14); + $worksheet1->getStyle('C1')->getFont()->setColor(new Color(Color::COLOR_BLUE)); + + $worksheet1->getStyle('C1')->getFill()->setFillType(Fill::FILL_SOLID); + $worksheet1->getStyle('C1')->getFill()->setStartColor(new Color(Color::COLOR_RED)); + + $worksheet1->getStyle('C1')->getFont()->setUnderline(Font::UNDERLINE_SINGLE); + $worksheet1->getStyle('C2')->getFont()->setUnderline(Font::UNDERLINE_DOUBLE); + $worksheet1->getStyle('D2')->getFont()->setUnderline(Font::UNDERLINE_NONE); + // Worksheet 2 $worksheet2 = $workbook->createSheet(); $worksheet2->setTitle('New Worksheet'); diff --git a/tests/data/Writer/Ods/content-empty.xml b/tests/data/Writer/Ods/content-empty.xml index 67cc35f2..87f756db 100644 --- a/tests/data/Writer/Ods/content-empty.xml +++ b/tests/data/Writer/Ods/content-empty.xml @@ -2,7 +2,12 @@ - + + + + + + @@ -10,7 +15,7 @@ - + diff --git a/tests/data/Writer/Ods/content-with-data.xml b/tests/data/Writer/Ods/content-with-data.xml index 42f87553..4ee12098 100644 --- a/tests/data/Writer/Ods/content-with-data.xml +++ b/tests/data/Writer/Ods/content-with-data.xml @@ -2,7 +2,52 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -10,37 +55,37 @@ - + 1 - + 12345.6789 - + 1 - + 01234 - + Lorem ipsum - + 1 - + - + 1 TRUE - + 42798.572060185 - + @@ -48,7 +93,7 @@ - + 2 From 93b5337bda0a1715586151eb66afe3ccb435e1b8 Mon Sep 17 00:00:00 2001 From: Paolo Agostinetto Date: Sat, 4 Mar 2017 16:35:53 +0100 Subject: [PATCH 6/7] Writer\Ods\Content: Fix CS --- src/PhpSpreadsheet/Writer/Ods/Content.php | 42 ++++++++----------- .../Writer/Ods/ContentTest.php | 13 +++--- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/Ods/Content.php b/src/PhpSpreadsheet/Writer/Ods/Content.php index a7c5556f..b8597028 100644 --- a/src/PhpSpreadsheet/Writer/Ods/Content.php +++ b/src/PhpSpreadsheet/Writer/Ods/Content.php @@ -2,7 +2,7 @@ namespace PhpOffice\PhpSpreadsheet\Writer\Ods; -/** +/* * PhpSpreadsheet. * * Copyright (c) 2006 - 2015 PhpSpreadsheet @@ -28,18 +28,19 @@ namespace PhpOffice\PhpSpreadsheet\Writer\Ods; */ use PhpOffice\PhpSpreadsheet\Cell; +use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Shared\XMLWriter; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Fill; use PhpOffice\PhpSpreadsheet\Style\Font; use PhpOffice\PhpSpreadsheet\Worksheet; -use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Writer\Exception; use PhpOffice\PhpSpreadsheet\Writer\Ods; use PhpOffice\PhpSpreadsheet\Writer\Ods\Cell\Comment; /** * @category PhpSpreadsheet + * * @method Ods getParentWriter * * @copyright Copyright (c) 2006 - 2015 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet) @@ -200,7 +201,6 @@ class Content extends WriterPart $prevColumn = -1; $cells = $row->getCellIterator(); while ($cells->valid()) { - /** @var Cell $cell */ $cell = $cells->current(); $column = Cell::columnIndexFromString($cell->getColumn()) - 1; @@ -210,8 +210,8 @@ class Content extends WriterPart // Style XF $style = $cell->getXfIndex(); - if($style !== null){ - $objWriter->writeAttribute('table:style-name', self::CELL_STYLE_PREFIX.$style); + if ($style !== null) { + $objWriter->writeAttribute('table:style-name', self::CELL_STYLE_PREFIX . $style); } switch ($cell->getDataType()) { @@ -288,15 +288,14 @@ class Content extends WriterPart } /** - * Write XF cell styles + * Write XF cell styles. * * @param XMLWriter $writer * @param Spreadsheet $spreadsheet */ private function writeXfStyles(XMLWriter $writer, Spreadsheet $spreadsheet) { - foreach($spreadsheet->getCellXfCollection() as $style) { - + foreach ($spreadsheet->getCellXfCollection() as $style) { $writer->startElement('style:style'); $writer->writeAttribute('style:name', self::CELL_STYLE_PREFIX . $style->getIndex()); $writer->writeAttribute('style:family', 'table-cell'); @@ -311,40 +310,37 @@ class Content extends WriterPart $font = $style->getFont(); - if($font->getBold()) { + if ($font->getBold()) { $writer->writeAttribute('fo:font-weight', 'bold'); $writer->writeAttribute('style:font-weight-complex', 'bold'); $writer->writeAttribute('style:font-weight-asian', 'bold'); } - if($font->getItalic()) { + if ($font->getItalic()) { $writer->writeAttribute('fo:font-style', 'italic'); } - if($color = $font->getColor()) { + if ($color = $font->getColor()) { $writer->writeAttribute('fo:color', sprintf('#%s', $color->getRGB())); } - if($family = $font->getName()) { + if ($family = $font->getName()) { $writer->writeAttribute('fo:font-family', $family); } - if($size = $font->getSize()) { + if ($size = $font->getSize()) { $writer->writeAttribute('fo:font-size', sprintf('%.1fpt', $size)); } - if($font->getUnderline() && $font->getUnderline() != Font::UNDERLINE_NONE) { - + if ($font->getUnderline() && $font->getUnderline() != Font::UNDERLINE_NONE) { $writer->writeAttribute('style:text-underline-style', 'solid'); $writer->writeAttribute('style:text-underline-width', 'auto'); $writer->writeAttribute('style:text-underline-color', 'font-color'); - switch($font->getUnderline()){ - + switch ($font->getUnderline()) { case Font::UNDERLINE_DOUBLE: $writer->writeAttribute('style:text-underline-type', 'double'); break; - case Font::UNDERLINE_SINGLE: $writer->writeAttribute('style:text-underline-type', 'single'); break; @@ -361,20 +357,18 @@ class Content extends WriterPart $writer->writeAttribute('style:rotation-align', 'none'); // Fill - if($fill = $style->getFill()) { - switch($fill->getFillType()) { - + if ($fill = $style->getFill()) { + switch ($fill->getFillType()) { case Fill::FILL_SOLID: - $writer->writeAttribute('fo:background-color', sprintf('#%s', + $writer->writeAttribute('fo:background-color', sprintf( + '#%s', strtolower($fill->getStartColor()->getRGB()) )); break; - case Fill::FILL_GRADIENT_LINEAR: case Fill::FILL_GRADIENT_PATH: /// TODO :: To be implemented break; - case Fill::FILL_NONE: default: } diff --git a/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php b/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php index baf22703..36d81233 100644 --- a/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php @@ -11,7 +11,6 @@ use PhpOffice\PhpSpreadsheet\Style\Font; use PhpOffice\PhpSpreadsheet\Style\NumberFormat; use PhpOffice\PhpSpreadsheet\Writer\Ods; use PhpOffice\PhpSpreadsheet\Writer\Ods\Content; -use PhpOffice\PhpSpreadsheetTests\Worksheet\WorksheetColumnTest; class ContentTest extends \PHPUnit_Framework_TestCase { @@ -24,7 +23,7 @@ class ContentTest extends \PHPUnit_Framework_TestCase $xml = $content->write(); - $this->assertXmlStringEqualsXmlFile($this->samplesPath . "/content-empty.xml", $xml); + $this->assertXmlStringEqualsXmlFile($this->samplesPath . '/content-empty.xml', $xml); } public function testWriteSpreadsheet() @@ -35,9 +34,9 @@ class ContentTest extends \PHPUnit_Framework_TestCase $worksheet1 = $workbook->getActiveSheet(); $worksheet1->setCellValue('A1', 1); // Number $worksheet1->setCellValue('B1', 12345.6789); // Number - $worksheet1->setCellValue('C1', "1"); // Number without cast - $worksheet1->setCellValueExplicit('D1', "01234", DataType::TYPE_STRING); // Number casted to string - $worksheet1->setCellValue('E1', "Lorem ipsum"); // String + $worksheet1->setCellValue('C1', '1'); // Number without cast + $worksheet1->setCellValueExplicit('D1', '01234', DataType::TYPE_STRING); // Number casted to string + $worksheet1->setCellValue('E1', 'Lorem ipsum'); // String $worksheet1->setCellValue('A2', true); // Boolean $worksheet1->setCellValue('B2', false); // Boolean @@ -51,7 +50,7 @@ class ContentTest extends \PHPUnit_Framework_TestCase // Styles $worksheet1->getStyle('A1')->getFont()->setBold(true); $worksheet1->getStyle('B1')->getFont()->setItalic(true); - $worksheet1->getStyle('C1')->getFont()->setName("Courier"); + $worksheet1->getStyle('C1')->getFont()->setName('Courier'); $worksheet1->getStyle('C1')->getFont()->setSize(14); $worksheet1->getStyle('C1')->getFont()->setColor(new Color(Color::COLOR_BLUE)); @@ -73,6 +72,6 @@ class ContentTest extends \PHPUnit_Framework_TestCase $xml = $content->write(); - $this->assertXmlStringEqualsXmlFile($this->samplesPath . "/content-with-data.xml", $xml); + $this->assertXmlStringEqualsXmlFile($this->samplesPath . '/content-with-data.xml', $xml); } } From 219e4e625877d57481e123200de324be85d3582c Mon Sep 17 00:00:00 2001 From: Paolo Agostinetto Date: Sat, 4 Mar 2017 17:13:10 +0100 Subject: [PATCH 7/7] Writer\Ods\Content: Set "CompatibilityMode" before and after running the tests in order to avoid conflicts with other tests --- .../Writer/Ods/ContentTest.php | 26 ++++++++++++++++++- tests/data/Writer/Ods/content-with-data.xml | 4 +-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php b/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php index 36d81233..9587a8e8 100644 --- a/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Ods/ContentTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Writer\Ods\Content; +use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Shared\Date; use PhpOffice\PhpSpreadsheet\Spreadsheet; @@ -16,6 +17,25 @@ class ContentTest extends \PHPUnit_Framework_TestCase { public $samplesPath = __DIR__ . '/../../../data/Writer/Ods'; + /** + * @var string + */ + protected $compatibilityMode; + + public function setUp() + { + parent::setUp(); + + $this->compatibilityMode = Functions::getCompatibilityMode(); + Functions::setCompatibilityMode(Functions::COMPATIBILITY_OPENOFFICE); + } + + protected function tearDown() + { + parent::tearDown(); + Functions::setCompatibilityMode($this->compatibilityMode); + } + public function testWriteEmptySpreadsheet() { $content = new Content(); @@ -40,7 +60,11 @@ class ContentTest extends \PHPUnit_Framework_TestCase $worksheet1->setCellValue('A2', true); // Boolean $worksheet1->setCellValue('B2', false); // Boolean - $worksheet1->setCellValue('C2', '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'); // Formula + $worksheet1->setCellValueExplicit( + 'C2', + '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))', + DataType::TYPE_FORMULA + ); // Formula $worksheet1->setCellValue('D2', Date::PHPToExcel(1488635026)); // Date $worksheet1->getStyle('D2') diff --git a/tests/data/Writer/Ods/content-with-data.xml b/tests/data/Writer/Ods/content-with-data.xml index 4ee12098..48184386 100644 --- a/tests/data/Writer/Ods/content-with-data.xml +++ b/tests/data/Writer/Ods/content-with-data.xml @@ -79,8 +79,8 @@ - - 1 TRUE + + 1 1 42798.572060185