diff --git a/Test/XLSXWriterTest.php b/Test/XLSXWriterTest.php index 0b5433dc9..a46e8e837 100644 --- a/Test/XLSXWriterTest.php +++ b/Test/XLSXWriterTest.php @@ -219,6 +219,63 @@ public static function getFreezeCellsScenarios() { ]; } + public function testColumnsWidths() { + $filename = tempnam("/tmp", "xlsx_writer"); + + $header = ['0'=>'string','1'=>'string','2'=>'string','3'=>'string']; + $sheet = [ + ['55','66','77','88'], + ['10','11','12','13'], + ]; + + $widths = [10, 20, 30, 40]; + + $col_options = ['widths' => $widths]; + + $xlsx_writer = new XLSXWriter(); + $xlsx_writer->writeSheetHeader('mysheet', $header, $format = 'xlsx', $delimiter = ';', $subheader = NULL, $col_options); + $xlsx_writer->writeSheetRow('mysheet', $sheet[0]); + $xlsx_writer->writeSheetRow('mysheet', $sheet[1]); + $xlsx_writer->writeToFile($filename); + + $zip = new ZipArchive(); + $r = $zip->open($filename); + $this->assertTrue($r); + + $this->assertNotEmpty(($zip->numFiles)); + + 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); + } + private function stripCellsFromSheetXML($sheet_xml) { $output = []; diff --git a/xlsxwriter.class.php b/xlsxwriter.class.php index c4b05c845..7ea4ce443 100644 --- a/xlsxwriter.class.php +++ b/xlsxwriter.class.php @@ -118,7 +118,7 @@ public function writeToFile($filename) $zip->close(); } - protected function initializeSheet($sheet_name, $freeze_rows=false, $freeze_columns=false) + protected function initializeSheet($sheet_name, $col_widths = [], $freeze_rows=false, $freeze_columns=false) { //if already initialized if ($this->current_sheet==$sheet_name || isset($this->sheets[$sheet_name])) @@ -172,7 +172,16 @@ protected function initializeSheet($sheet_name, $freeze_rows=false, $freeze_colu $sheet->file_writer->write( ''); $sheet->file_writer->write( ''); $sheet->file_writer->write( ''); - $sheet->file_writer->write( ''); + + $cols_count = count($col_widths); + + if (!empty($col_widths)) { + foreach($col_widths as $i => $column_width) { + $sheet->file_writer->write( ''); + } + } + $sheet->file_writer->write( ''); + $sheet->file_writer->write( ''); $sheet->file_writer->write( ''); } @@ -201,10 +210,11 @@ public function writeSheetHeader($sheet_name, array $header_types, $format = 'xl $start = 0; } + $col_widths = (!empty($col_options['widths'])) ? (array)$col_options['widths'] : []; $freeze_rows = (array_key_exists('freeze_rows', $col_options) && $col_options['freeze_rows'] !== false) ? intval($col_options['freeze_rows']) : false; $freeze_columns = (array_key_exists('freeze_columns', $col_options) && $col_options['freeze_columns'] !== false) ? intval($col_options['freeze_columns']) : false; - self::initializeSheet($sheet_name, $freeze_rows, $freeze_columns); + self::initializeSheet($sheet_name, $col_widths, $freeze_rows, $freeze_columns); $sheet = &$this->sheets[$sheet_name]; $sheet->cell_formats = array_values($header_types); $header_row = array_keys($header_types);