Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[t:DCWIbRRX] added custom columns widths #17

Merged
merged 1 commit into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Test/XLSXWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down
16 changes: 13 additions & 3 deletions xlsxwriter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down Expand Up @@ -172,7 +172,16 @@ protected function initializeSheet($sheet_name, $freeze_rows=false, $freeze_colu
$sheet->file_writer->write( '</sheetView>');
$sheet->file_writer->write( '</sheetViews>');
$sheet->file_writer->write( '<cols>');
$sheet->file_writer->write( '<col collapsed="false" hidden="false" max="1025" min="1" style="0" width="11.5"/>');

$cols_count = count($col_widths);

if (!empty($col_widths)) {
foreach($col_widths as $i => $column_width) {
$sheet->file_writer->write( '<col collapsed="false" hidden="false" max="'. ($i + 1) .'" min="'. ($i + 1) .'" style="0" customWidth="true" width="'.floatval($column_width).'"/>');
}
}
$sheet->file_writer->write( '<col collapsed="false" hidden="false" max="1024" min="'. ($cols_count + 1) .'" style="0" customWidth="false" width="11.5"/>');

$sheet->file_writer->write( '</cols>');
$sheet->file_writer->write( '<sheetData>');
}
Expand Down Expand Up @@ -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);
Expand Down