From 3a40834a447c54d955b2c26fc1eecfe41170f09e Mon Sep 17 00:00:00 2001 From: Clemente Date: Wed, 14 Sep 2022 17:28:08 -0300 Subject: [PATCH 01/11] Revert "[t-lTBFjwvE] Upgrade travis PHP to diaginose deprecated warnings" This reverts commit 98579343409e4807df3cebd819eb8b82574ac6b9. --- .travis.yml | 6 +++--- Test/XLSXWriterTest.php | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9476f34d9..5bb05ddb6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,16 @@ -dist: bionic +dist: trusty sudo: required language: php php: - - 7.4.28 + - 7.1 install: - composer validate --no-check-all --no-check-publish - composer install -script: ./vendor/bin/phpunit Test/ +script: ./vendor/bin/phpunit --coverage-clover=coverage.xml Test/ after_success: - bash <(curl -s https://codecov.io/bash) diff --git a/Test/XLSXWriterTest.php b/Test/XLSXWriterTest.php index 36b163640..e861a5158 100644 --- a/Test/XLSXWriterTest.php +++ b/Test/XLSXWriterTest.php @@ -152,6 +152,7 @@ public function testFreezeCells($freeze_cols, $freeze_rows, $expected_active_cel $selections = $sheet_view->selection; for ($i = 0; $i < count($expected_active_cells); $i++) { + $this->assertNotEmpty($selections[$i]); $this->assertEquals($expected_active_cells[$i]['cell'], $selections[$i]['activeCell']); $this->assertEquals($expected_active_cells[$i]['cell'], $selections[$i]['sqref']); $this->assertEquals($expected_active_cells[$i]['pane'], $selections[$i]['pane']); From c99ba72cdc192e97323efbb22602dab11d17e09e Mon Sep 17 00:00:00 2001 From: Clemente Date: Wed, 14 Sep 2022 17:28:14 -0300 Subject: [PATCH 02/11] Revert "[t-C65jJpnq] Remove array and string offset access syntax with curly braces" This reverts commit 26d8a73181fed3062802e9e9ef6bc0aa00b282bb. --- xlsxwriter.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xlsxwriter.class.php b/xlsxwriter.class.php index ee1ae2998..78f3ea138 100644 --- a/xlsxwriter.class.php +++ b/xlsxwriter.class.php @@ -428,9 +428,9 @@ protected function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $col $file->write(''.self::convert_date_time($value).''); } elseif (!is_string($value)) { $file->write(''.($value*1).'');//int,float, etc - } elseif ($value[0]!='0' && filter_var($value, FILTER_VALIDATE_INT)){ //excel wants to trim leading zeros + } elseif ($value{0}!='0' && filter_var($value, FILTER_VALIDATE_INT)){ //excel wants to trim leading zeros $file->write(''.($value).'');//numeric string - } elseif ($value[0]=='='){ + } elseif ($value{0}=='='){ $file->write(''.self::xmlspecialchars($value).''); } elseif ($value!==''){ $file->write(''.self::xmlspecialchars($this->setSharedString($value)).''); From 62b9adb5d420b223151e5edecd6f68a40e9c5d26 Mon Sep 17 00:00:00 2001 From: Clemente Date: Wed, 14 Sep 2022 17:28:16 -0300 Subject: [PATCH 03/11] Revert "[t:DCWIbRRX] added row borders" This reverts commit 9868fb35259cbe9f085ccd39b596c55c8224028d. --- Test/XLSXWriterTest.php | 108 +------------------ xlsxwriter.class.php | 223 ++++++++-------------------------------- 2 files changed, 44 insertions(+), 287 deletions(-) diff --git a/Test/XLSXWriterTest.php b/Test/XLSXWriterTest.php index e861a5158..6c77099fb 100644 --- a/Test/XLSXWriterTest.php +++ b/Test/XLSXWriterTest.php @@ -12,8 +12,8 @@ class _XLSXWriter_ extends XLSXWriter { - public function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $column_number, $value, $cell_format, $cell_style_idx = null) { - return call_user_func_array('parent::writeCell', [&$file, $row_number, $column_number, $value, $cell_format, $cell_style_idx = null]); + public function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $column_number, $value, $cell_format) { + return call_user_func_array('parent::writeCell', [&$file, $row_number, $column_number, $value, $cell_format]); } } //Just a simple test, by no means comprehensive @@ -364,59 +364,6 @@ public function testRowCollapsed() { @unlink($filename); } - public function testAddBorder() { - $filename = tempnam("/tmp", "xlsx_writer"); - - $header = ["0"=>"string", "1"=>"string", "2"=>"string", "3"=>"string"]; - $sheet = [ - ["55", "66", "77", "88"], - ["10", "11", "12", "13"], - ]; - - $expected_borders = ["right", "left", "top", "bottom"]; - $expected_border_style = "thick"; - $expected_border_color_base = "ff99cc"; - $expected_border_color = "FFFF99CC"; - - $row_options = [ - "border" => implode(",", $expected_borders), - "border-style" => $expected_border_style, - "border-color" => "#$expected_border_color_base" , - ]; - - $xlsx_writer = new XLSXWriter(); - $xlsx_writer->writeSheetHeader("mysheet", $header); - $xlsx_writer->writeSheetRow("mysheet", $sheet[0], $format = "xlsx", $delimiter = ";", $row_options); - $xlsx_writer->writeSheetRow("mysheet", $sheet[1]); - $xlsx_writer->writeToFile($filename); - - $zip = new ZipArchive(); - $r = $zip->open($filename); - $xml = $this->extractSheetXml($zip); - $styles = $this->extractStyleXml($zip); - - $this->assertTrue($r); - $this->assertNotEmpty(($zip->numFiles)); - $this->assertNotEmpty($xml); - $this->assertNotEmpty($styles); - - $border_styles = $styles->borders; - $this->assertBorderStyle($expected_border_style, $expected_border_color, $border = $border_styles->border[1]); - $this->assertFillStyle($expected_pattern = "solid", $expected_bg_color = "FF003300", $styles->fills->fill[2]); - $this->assertFontStyle($expected_font_name = "Arial", $expected_is_bold = "true", $styles->fonts->font[4]); - - $cell_styles = $styles->cellXfs->xf; - $this->assertCellStyle($expected_apply_border_string = "false", $expected_border_id = 0, $expected_fill_id = 2, $expected_font_id = 4, $cell_styles[6]); - $this->assertCellStyle($expected_apply_border_string = "true", $expected_border_id = 1, $expected_fill_id = 0, $expected_font_id = 0, $cell_styles[7]); - - $rows = $xml->sheetData->row; - $this->assertRowHasStyleIndex($rows[0], $expected_header_style = 6); - $this->assertRowHasStyleIndex($rows[1], $expected_style = 7); - - $zip->close(); - @unlink($filename); - } - private function stripCellsFromSheetXML($sheet_xml) { $output = []; @@ -464,61 +411,10 @@ private function extractSheetXml($zip) { return null; } - private function extractStyleXml($zip) { - for($z=0; $z < $zip->numFiles; $z++) { - $inside_zip_filename = $zip->getNameIndex($z); - $xml = $zip->getFromName($inside_zip_filename); - if (preg_match("/styles.xml/", basename($inside_zip_filename))) { - return new SimpleXMLElement($xml); - } - } - - return null; - } - private function assertRowProperties($expected_height, $expected_custom_height, $expected_hidden, $expected_collapsed, $row) { $this->assertEquals($expected_height, (string)$row['ht']); $this->assertEquals($expected_custom_height, filter_var($row['customHeight'], FILTER_VALIDATE_BOOLEAN)); $this->assertEquals($expected_hidden, filter_var($row['hidden'], FILTER_VALIDATE_BOOLEAN)); $this->assertEquals($expected_collapsed, filter_var($row['collapsed'], FILTER_VALIDATE_BOOLEAN)); } - - private function assertCellStyle($expected_apply_border_string, $expected_border_id, $expected_fill_id, $expected_font_id, $cell_style) { - $this->assertEquals($expected_apply_border_string, $cell_style["applyBorder"]); - $this->assertEquals($expected_border_id, (int)$cell_style["borderId"]); - $this->assertEquals($expected_fill_id, (int)$cell_style["fillId"]); - $this->assertEquals($expected_font_id, (int)$cell_style["fontId"]); - } - - private function assertBorderStyle($expected_border_style, $expected_border_color, $border) { - $this->assertEquals($expected_border_style, $border->left["style"]); - $this->assertEquals($expected_border_style, $border->right["style"]); - $this->assertEquals($expected_border_style, $border->top["style"]); - $this->assertEquals($expected_border_style, $border->bottom["style"]); - - $this->assertEquals($expected_border_color, $border->left->color["rgb"]); - $this->assertEquals($expected_border_color, $border->right->color["rgb"]); - $this->assertEquals($expected_border_color, $border->top->color["rgb"]); - $this->assertEquals($expected_border_color, $border->bottom->color["rgb"]); - } - - private function assertFillStyle($expected_pattern, $expected_bg_color, $fill) { - $this->assertEquals($expected_pattern, $fill->patternFill["patternType"]); - if (!empty($expected_bg_color)) { - $this->assertEquals($expected_bg_color, $fill->patternFill->bgColor["rgb"]); - } - } - - private function assertFontStyle($expected_font_name, $expected_is_bold, $font) { - $this->assertEquals($expected_font_name, $font->name["val"]); - if (!empty($expected_is_bold)) { - $this->assertEquals($expected_is_bold, $font->b["val"]); - } - } - - private function assertRowHasStyleIndex($row, $expected_style) { - foreach ($row->c as $cell) { - $this->assertEquals($expected_style, (int)$cell["s"]); - } - } } diff --git a/xlsxwriter.class.php b/xlsxwriter.class.php index 78f3ea138..03c1fcd0f 100644 --- a/xlsxwriter.class.php +++ b/xlsxwriter.class.php @@ -26,40 +26,6 @@ class XLSXWriter protected $description; protected $keywords = []; - protected $cell_styles = []; - - private const CELL_STYLES = [ - 'money' => 1, - 'dollar' => 1, - 'datetime' => 2, - 'date' => 3, - 'string' => 0, - 'number.2' => 4, - 'number.4' => 5, - 'blackheader' => 6 - ]; - private const ALLOWED_BORDERS = [ - 'left', - 'right', - 'top', - 'bottom', - ]; - private const ALLOWED_BORDER_STYLES = [ - 'thin', - 'medium', - 'thick', - 'dashDot', - 'dashDotDot', - 'dashed', - 'dotted', - 'double', - 'hair', - 'mediumDashDot', - 'mediumDashDotDot', - 'mediumDashed', - 'slantDashDot', - ]; - public function __construct() { if(!ini_get('date.timezone')) @@ -67,19 +33,6 @@ public function __construct() //using date functions can kick out warning if this isn't set date_default_timezone_set('UTC'); } - - - $this->addCellStyle($number_format='GENERAL', $style_string=null); - $this->addCellStyle($number_format='GENERAL', $style_string=null); - $this->addCellStyle($number_format='GENERAL', $style_string=null); - $this->addCellStyle($number_format='GENERAL', $style_string=null); - $this->addCellStyle($number_format='string', $style_string=null); - $this->addCellStyle($number_format='money', $style_string=null); - $this->addCellStyle($number_format='dollar', $style_string=null); - $this->addCellStyle($number_format='datetime', $style_string=null); - $this->addCellStyle($number_format='date', $style_string=null); - $this->addCellStyle($number_format='number.2', $style_string=null); - $this->addCellStyle($number_format='number.4', $style_string=null); } public function setAuthor($author='') { $this->author=$author; } @@ -267,11 +220,8 @@ public function writeSheetHeader($sheet_name, array $header_types, $format = 'xl $header_row = array_keys($header_types); $sheet->file_writer->write(''); $sheet->row_count++; @@ -294,17 +244,16 @@ public function writeSheetRow($sheet_name, array $row, $format = 'xlsx', $delimi $ht = array_key_exists('height', $row_options) ? floatval($row_options['height']) : 12.1; $customHt = array_key_exists('height', $row_options) ? 'true' : 'false'; - $hidden = (array_key_exists('hidden', $row_options) && $row_options['hidden']) ? 'true' : 'false'; - $collapsed = (array_key_exists('collapsed', $row_options) && $row_options['collapsed']) ? 'true' : 'false'; + $hidden = array_key_exists('hidden', $row_options) ? (string)($row_options['hidden']) : 'false'; + $collapsed = array_key_exists('collapsed', $row_options) ? (string)($row_options['collapsed']) : 'false'; $sheet->file_writer->write(''); @@ -399,50 +348,42 @@ public function writeSheet(array $data, $sheet_name='', array $header_types=arra $this->finalizeSheet($sheet_name); } - protected function getCellFormat($cell_format) { - return isset(self::CELL_STYLES[$cell_format]) ? self::CELL_STYLES[$cell_format] : '0'; - } - - private function addCellStyle($cell_format, $cell_style_string) { - $cell_format_idx = $this->getCellFormat($cell_format); - $lookup_string = $cell_format_idx.";".$cell_style_string; - $cell_style_idx = self::add_to_list_get_index($this->cell_styles, $lookup_string); - - return $cell_style_idx; - } - - protected function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $column_number, $value, $cell_format, $cell_style_idx = null) + protected function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $column_number, $value, $cell_format) { - $cell_name = self::xlsCell($row_number, $column_number); - if (is_null($cell_style_idx)) { - $cell_style_idx = $this->addCellStyle($cell_format, null); - } + static $styles = array( + 'money' => 1, + 'dollar' => 1, + 'datetime' => 2, + 'date' => 3, + 'string' => 0, + 'number.2' => 4, + 'number.4' => 5, + 'blackheader' => 6 + ); + $cell = self::xlsCell($row_number, $column_number); + $s = isset($styles[$cell_format]) ? $styles[$cell_format] : '0'; if (!is_scalar($value) || $value==='') { //objects, array, empty - $file->write(''); + $file->write(''); } elseif (preg_match('#^number\.[0-9]$#', $cell_format)) { - $file->write(''.$value.''); + $file->write(''.$value.''); } elseif ($cell_format=='date') { - $file->write(''.intval(self::convert_date_time($value)).''); + $file->write(''.intval(self::convert_date_time($value)).''); } elseif ($cell_format=='datetime') { - $file->write(''.self::convert_date_time($value).''); + $file->write(''.self::convert_date_time($value).''); } elseif (!is_string($value)) { - $file->write(''.($value*1).'');//int,float, etc + $file->write(''.($value*1).'');//int,float, etc } elseif ($value{0}!='0' && filter_var($value, FILTER_VALIDATE_INT)){ //excel wants to trim leading zeros - $file->write(''.($value).'');//numeric string + $file->write(''.($value).'');//numeric string } elseif ($value{0}=='='){ - $file->write(''.self::xmlspecialchars($value).''); + $file->write(''.self::xmlspecialchars($value).''); } elseif ($value!==''){ - $file->write(''.self::xmlspecialchars($this->setSharedString($value)).''); + $file->write(''.self::xmlspecialchars($this->setSharedString($value)).''); } } protected function writeStylesXML() { - $styles = $this->spreadStyles(); - $borders = $styles['borders']; - $cell_styles = $styles['styles']; - $temporary_filename = $this->tempFilename(); $file = new XLSXWriter_BuffererWriter($temporary_filename); $file->write(''."\n"); @@ -467,32 +408,8 @@ protected function writeStylesXML() $file->write(' '); $file->write(' '); $file->write(''); - - $file->write(''); - $file->write( ''); - foreach($borders as $border) { - if (empty($border)) { - continue; - } - - $pieces = json_decode($border,true); - - $border_style = !empty($pieces['style']) ? $pieces['style'] : 'hair'; - $border_color = !empty($pieces['color']) ? '' : ''; - - $file->write(''); - - foreach (self::ALLOWED_BORDERS as $side) { - $show_side = in_array($side,$pieces['side']) ? true : false; - $file->write($show_side ? "<$side style=\"$border_style\">$border_color" : "<$side/>"); - } - - $file->write( ''); - $file->write(''); - } - $file->write(''); - - $file->write(''); + $file->write(''); + $file->write( ''); $file->write( ''); $file->write( ''); $file->write( ''); - - $file->write(''); - foreach($cell_styles as $cell_style) { - $applyAlignment = isset($cell_style['alignment']) ? 'true' : 'false'; - $wrapText = !empty($cell_style['wrap_text']) ? 'true' : 'false'; - $horizAlignment = isset($cell_style['halign']) ? $cell_style['halign'] : 'general'; - $vertAlignment = isset($cell_style['valign']) ? $cell_style['valign'] : 'bottom'; - $applyBorder = isset($cell_style['border_idx']) ? 'true' : 'false'; - $applyFont = 'true'; - $borderIdx = isset($cell_style['border_idx']) ? intval($cell_style['border_idx']) : 0; - $fillIdx = isset($cell_style['fill_idx']) ? intval($cell_style['fill_idx']) : 0; - $fontIdx = isset($cell_style['font_idx']) ? intval($cell_style['font_idx']) : 0; - $file->write(''); - $file->write(' '); - $file->write(' '); - } - $file->write(''); - + $file->write( ''); + $file->write( ''); + $file->write( ''); + $file->write( ''); + $file->write( ''); + $file->write( ''); + $file->write( ''); + $file->write( ''); + $file->write( ''); + $file->write( ''); $file->write( ''); $file->write( ''); $file->write( ''); @@ -545,46 +452,10 @@ protected function writeStylesXML() $file->write( ''); $file->write( ''); $file->write(''); - $file->close(); return $temporary_filename; } - protected function spreadStyles() { - $borders = [''];//1 placeholder for static xml later - $style_indexes = []; - - foreach ($this->cell_styles as $i => $cell_style_string) { - [$number_format_idx, $style_json_string] = explode(";", $cell_style_string, 2); - $style = json_decode($style_json_string, $as_assoc = true); - - $style_indexes[$i] = $style ?? []; - $style_indexes[$i]['num_fmt_idx'] = $number_format_idx; - if (isset($style['border']) && is_string($style['border'])) { //border is a comma delimited str - $border_value['side'] = array_intersect(explode(",", $style['border']), self::ALLOWED_BORDERS); - - if (isset($style['border-style']) && in_array($style['border-style'], self::ALLOWED_BORDER_STYLES)) { - $border_value['style'] = $style['border-style']; - } - - if (isset($style['border-color']) && is_string($style['border-color']) && $style['border-color'][0] == '#') { - $hexa_border_color = substr($style['border-color'], 1, 6); - $hexa_border_color = strlen($hexa_border_color) == 3 ? $hexa_border_color[0] . $hexa_border_color[0] . - $hexa_border_color[1] . $hexa_border_color[1] . - $hexa_border_color[2] . $hexa_border_color[2] : $hexa_border_color;// expand cf0 => ccff00 - $border_value['color'] = "FF" . strtoupper($hexa_border_color); - } - - $style_indexes[$i]['border_idx'] = self::add_to_list_get_index($borders, json_encode($border_value)); - } - } - - return [ - 'borders' => $borders, - 'styles' => $style_indexes, - ]; - } - protected function setSharedString($v) { if (isset($this->shared_strings[$v])) @@ -815,16 +686,6 @@ public static function convert_date_time($date_input) //thanks to Excel::Writer: return $days + $seconds; } //------------------------------------------------------------------ - public static function add_to_list_get_index(&$haystack, $needle) { - $existing_idx = array_search($needle, $haystack, $strict=true); - - if ($existing_idx === false) { - $existing_idx = count($haystack); - $haystack[] = $needle; - } - - return $existing_idx; - } } class XLSXWriter_BuffererWriter From a5b0b2c772754da18abc2759e167bf2a45ed2e36 Mon Sep 17 00:00:00 2001 From: Clemente Date: Wed, 14 Sep 2022 17:28:17 -0300 Subject: [PATCH 04/11] Revert "[t:DCWIbRRX] added row options (height, collapsed and hidden)" This reverts commit 01590d6228701bfdef6c7aee9e90e1f13d6fe95c. --- Test/XLSXWriterTest.php | 225 +++++++++++----------------------------- xlsxwriter.class.php | 10 +- 2 files changed, 61 insertions(+), 174 deletions(-) diff --git a/Test/XLSXWriterTest.php b/Test/XLSXWriterTest.php index 6c77099fb..a46e8e837 100644 --- a/Test/XLSXWriterTest.php +++ b/Test/XLSXWriterTest.php @@ -98,15 +98,20 @@ public function testMarkMergedCells() { $zip = new ZipArchive(); $r = $zip->open($filename); - $xml = $this->extractSheetXml($zip); - $this->assertTrue($r); + $this->assertNotEmpty(($zip->numFiles)); - $this->assertNotEmpty($xml); - $merged_cell_range = $xml->mergeCells->mergeCell["ref"][0]; + for($z=0; $z < $zip->numFiles; $z++) { + $inside_zip_filename = $zip->getNameIndex($z); + $sheet_xml = $zip->getFromName($inside_zip_filename); + if (preg_match("/sheet(\d+).xml/", basename($inside_zip_filename))) { + $xml = new SimpleXMLElement($sheet_xml); + $merged_cell_range = $xml->mergeCells->mergeCell["ref"][0]; - $this->assertEquals($expected_merged_range, $merged_cell_range); + $this->assertEquals($expected_merged_range, $merged_cell_range); + } + } $zip->close(); @unlink($filename); @@ -134,30 +139,35 @@ public function testFreezeCells($freeze_cols, $freeze_rows, $expected_active_cel $zip = new ZipArchive(); $r = $zip->open($filename); - $xml = $this->extractSheetXml($zip); - $this->assertTrue($r); + $this->assertNotEmpty(($zip->numFiles)); - $this->assertNotEmpty($xml); - $sheet_view = $xml->sheetViews->sheetView; + for($z=0; $z < $zip->numFiles; $z++) { + $inside_zip_filename = $zip->getNameIndex($z); + $sheet_xml = $zip->getFromName($inside_zip_filename); + if (preg_match("/sheet(\d+).xml/", basename($inside_zip_filename))) { + $xml = new SimpleXMLElement($sheet_xml); + $sheet_view = $xml->sheetViews->sheetView; + + if (!empty($expected_pane)) { + $pane = $sheet_view->pane; + foreach ($expected_pane as $expected_key => $expected_value) { + $attribute = (string) $pane[0][$expected_key]; + $this->assertEquals($expected_value, $attribute); + } + } - if (!empty($expected_pane)) { - $pane = $sheet_view->pane; - foreach ($expected_pane as $expected_key => $expected_value) { - $attribute = (string) $pane[0][$expected_key]; - $this->assertEquals($expected_value, $attribute); + $selections = $sheet_view->selection; + for ($i = 0; $i < count($expected_active_cells); $i++) { + $this->assertNotEmpty($selections[$i]); + $this->assertEquals($expected_active_cells[$i]['cell'], $selections[$i]['activeCell']); + $this->assertEquals($expected_active_cells[$i]['cell'], $selections[$i]['sqref']); + $this->assertEquals($expected_active_cells[$i]['pane'], $selections[$i]['pane']); + } } } - $selections = $sheet_view->selection; - for ($i = 0; $i < count($expected_active_cells); $i++) { - $this->assertNotEmpty($selections[$i]); - $this->assertEquals($expected_active_cells[$i]['cell'], $selections[$i]['activeCell']); - $this->assertEquals($expected_active_cells[$i]['cell'], $selections[$i]['sqref']); - $this->assertEquals($expected_active_cells[$i]['pane'], $selections[$i]['pane']); - } - $zip->close(); @unlink($filename); } @@ -230,135 +240,37 @@ public function testColumnsWidths() { $zip = new ZipArchive(); $r = $zip->open($filename); - $xml = $this->extractSheetXml($zip); - $this->assertTrue($r); - $this->assertNotEmpty(($zip->numFiles)); - $this->assertNotEmpty($xml); - - $cols = $xml->cols->col; - foreach ($widths as $col_index => $col_width) { - $col = $cols[$col_index]; - $this->assertFalse(filter_var($col["collapsed"], FILTER_VALIDATE_BOOLEAN)); - $this->assertFalse(filter_var($col["hidden"], FILTER_VALIDATE_BOOLEAN)); - $this->assertTrue(filter_var($col["customWidth"], FILTER_VALIDATE_BOOLEAN)); - $this->assertEquals($col_index + 1, (string) $col["max"]); - $this->assertEquals($col_index + 1, (string) $col["min"]); - $this->assertEquals("0", (string) $col["style"]); - $this->assertEquals($col_width, (string) $col["width"]); - } - $last_col_index = count($widths); - $last_col = $cols[$last_col_index]; - $this->assertFalse(filter_var($last_col["collapsed"], FILTER_VALIDATE_BOOLEAN)); - $this->assertFalse(filter_var($last_col["hidden"], FILTER_VALIDATE_BOOLEAN)); - $this->assertFalse(filter_var($last_col["customWidth"], FILTER_VALIDATE_BOOLEAN)); - $this->assertEquals("1024", (string) $last_col["max"]); - $this->assertEquals($last_col_index + 1, (string) $last_col["min"]); - $this->assertEquals("0", (string) $last_col["style"]); - $this->assertEquals("11.5", (string) $last_col["width"]); - - $zip->close(); - @unlink($filename); - } - - public function testRowHeight() { - $filename = tempnam("/tmp", "xlsx_writer"); - $sheet = [ - ['55','66','77','88'], - ['10','11','12','13'], - ]; - - $custom_height = 20.5; - - $row_options = ['height' => $custom_height]; - - $xlsx_writer = new XLSXWriter(); - $xlsx_writer->writeSheetRow('mysheet', $sheet[0], $format = 'xlsx', $delimiter = ';', $row_options); - $xlsx_writer->writeSheetRow('mysheet', $sheet[1]); - $xlsx_writer->writeToFile($filename); - - $zip = new ZipArchive(); - $r = $zip->open($filename); - $xml = $this->extractSheetXml($zip); - - $this->assertTrue($r); $this->assertNotEmpty(($zip->numFiles)); - $this->assertNotEmpty($xml); - - $rows = $xml->sheetData->row; - $this->assertRowProperties($custom_height, $expected_custom_height = true, $expected_hidden = false, $expected_collapsed = false, $rows[0]); - $this->assertRowProperties($expected_height = 12.1, $expected_custom_height = false, $expected_hidden = false, $expected_collapsed = false, $rows[1]); - - $zip->close(); - @unlink($filename); - } - - public function testRowHidden() { - $filename = tempnam("/tmp", "xlsx_writer"); - - $sheet = [ - ['55','66','77','88'], - ['10','11','12','13'], - ]; - $row_options = ['hidden' => true]; - - $expected_height = 12.1; - $expected_custom_height = false; - $expected_collapsed = false; - - $xlsx_writer = new XLSXWriter(); - $xlsx_writer->writeSheetRow('mysheet', $sheet[0], $format = 'xlsx', $delimiter = ';', $row_options); - $xlsx_writer->writeSheetRow('mysheet', $sheet[1]); - $xlsx_writer->writeToFile($filename); - - $zip = new ZipArchive(); - $r = $zip->open($filename); - $xml = $this->extractSheetXml($zip); - - $this->assertTrue($r); - $this->assertNotEmpty(($zip->numFiles)); - $this->assertNotEmpty($xml); - - $rows = $xml->sheetData->row; - $this->assertRowProperties($expected_height, $expected_custom_height, $expected_hidden = true, $expected_collapsed, $rows[0]); - $this->assertRowProperties($expected_height, $expected_custom_height, $expected_hidden = false, $expected_collapsed, $rows[1]); - - $zip->close(); - @unlink($filename); - } - - public function testRowCollapsed() { - $filename = tempnam("/tmp", "xlsx_writer"); - - $sheet = [ - ['55','66','77','88'], - ['10','11','12','13'], - ]; - - $row_options = ['collapsed' => true]; - - $expected_height = 12.1; - $expected_custom_height = false; - $expected_hidden = false; - - $xlsx_writer = new XLSXWriter(); - $xlsx_writer->writeSheetRow('mysheet', $sheet[0], $format = 'xlsx', $delimiter = ';', $row_options); - $xlsx_writer->writeSheetRow('mysheet', $sheet[1]); - $xlsx_writer->writeToFile($filename); - - $zip = new ZipArchive(); - $r = $zip->open($filename); - $xml = $this->extractSheetXml($zip); - - $this->assertTrue($r); - $this->assertNotEmpty(($zip->numFiles)); - $this->assertNotEmpty($xml); - - $rows = $xml->sheetData->row; - $this->assertRowProperties($expected_height, $expected_custom_height, $expected_hidden, $expected_collapsed = true, $rows[0]); - $this->assertRowProperties($expected_height, $expected_custom_height, $expected_hidden, $expected_collapsed = false, $rows[1]); + for($z=0; $z < $zip->numFiles; $z++) { + $inside_zip_filename = $zip->getNameIndex($z); + $sheet_xml = $zip->getFromName($inside_zip_filename); + if (preg_match("/sheet(\d+).xml/", basename($inside_zip_filename))) { + $xml = new SimpleXMLElement($sheet_xml); + $cols = $xml->cols->col; + foreach ($widths as $col_index => $col_width) { + $col = $cols[$col_index]; + $this->assertFalse(filter_var($col["collapsed"], FILTER_VALIDATE_BOOLEAN)); + $this->assertFalse(filter_var($col["hidden"], FILTER_VALIDATE_BOOLEAN)); + $this->assertTrue(filter_var($col["customWidth"], FILTER_VALIDATE_BOOLEAN)); + $this->assertEquals($col_index + 1, (string) $col["max"]); + $this->assertEquals($col_index + 1, (string) $col["min"]); + $this->assertEquals("0", (string) $col["style"]); + $this->assertEquals($col_width, (string) $col["width"]); + } + $last_col_index = count($widths); + $last_col = $cols[$last_col_index]; + $this->assertFalse(filter_var($last_col["collapsed"], FILTER_VALIDATE_BOOLEAN)); + $this->assertFalse(filter_var($last_col["hidden"], FILTER_VALIDATE_BOOLEAN)); + $this->assertFalse(filter_var($last_col["customWidth"], FILTER_VALIDATE_BOOLEAN)); + $this->assertEquals("1024", (string) $last_col["max"]); + $this->assertEquals($last_col_index + 1, (string) $last_col["min"]); + $this->assertEquals("0", (string) $last_col["style"]); + $this->assertEquals("11.5", (string) $last_col["width"]); + } + } $zip->close(); @unlink($filename); @@ -398,23 +310,4 @@ public static function array_diff_assoc_recursive($array1, $array2) { return empty($difference) ? [] : $difference; } - - private function extractSheetXml($zip) { - for($z=0; $z < $zip->numFiles; $z++) { - $inside_zip_filename = $zip->getNameIndex($z); - $sheet_xml = $zip->getFromName($inside_zip_filename); - if (preg_match("/sheet(\d+).xml/", basename($inside_zip_filename))) { - return new SimpleXMLElement($sheet_xml); - } - } - - return null; - } - - private function assertRowProperties($expected_height, $expected_custom_height, $expected_hidden, $expected_collapsed, $row) { - $this->assertEquals($expected_height, (string)$row['ht']); - $this->assertEquals($expected_custom_height, filter_var($row['customHeight'], FILTER_VALIDATE_BOOLEAN)); - $this->assertEquals($expected_hidden, filter_var($row['hidden'], FILTER_VALIDATE_BOOLEAN)); - $this->assertEquals($expected_collapsed, filter_var($row['collapsed'], FILTER_VALIDATE_BOOLEAN)); - } } diff --git a/xlsxwriter.class.php b/xlsxwriter.class.php index 03c1fcd0f..7ea4ce443 100644 --- a/xlsxwriter.class.php +++ b/xlsxwriter.class.php @@ -228,7 +228,7 @@ public function writeSheetHeader($sheet_name, array $header_types, $format = 'xl $this->current_sheet = $sheet_name; } - public function writeSheetRow($sheet_name, array $row, $format = 'xlsx', $delimiter = ';', array $row_options = []) { + public function writeSheetRow($sheet_name, array $row, $format = 'xlsx', $delimiter = ';') { if (empty($sheet_name) || empty($row)) { return; } @@ -242,12 +242,7 @@ public function writeSheetRow($sheet_name, array $row, $format = 'xlsx', $delimi $sheet->cell_formats = array_fill(0, count($row), 'string'); } - $ht = array_key_exists('height', $row_options) ? floatval($row_options['height']) : 12.1; - $customHt = array_key_exists('height', $row_options) ? 'true' : 'false'; - $hidden = array_key_exists('hidden', $row_options) ? (string)($row_options['hidden']) : 'false'; - $collapsed = array_key_exists('collapsed', $row_options) ? (string)($row_options['collapsed']) : 'false'; - $sheet->file_writer->write('