@@ -98,20 +98,15 @@ public function testMarkMergedCells() {
9898
9999 $ zip = new ZipArchive ();
100100 $ r = $ zip ->open ($ filename );
101- $ this ->assertTrue ( $ r );
101+ $ xml = $ this ->extractSheetXml ( $ zip );
102102
103+ $ this ->assertTrue ($ r );
103104 $ this ->assertNotEmpty (($ zip ->numFiles ));
105+ $ this ->assertNotEmpty ($ xml );
104106
105- for ($ z =0 ; $ z < $ zip ->numFiles ; $ z ++) {
106- $ inside_zip_filename = $ zip ->getNameIndex ($ z );
107- $ sheet_xml = $ zip ->getFromName ($ inside_zip_filename );
108- if (preg_match ("/sheet(\d+).xml/ " , basename ($ inside_zip_filename ))) {
109- $ xml = new SimpleXMLElement ($ sheet_xml );
110- $ merged_cell_range = $ xml ->mergeCells ->mergeCell ["ref " ][0 ];
107+ $ merged_cell_range = $ xml ->mergeCells ->mergeCell ["ref " ][0 ];
111108
112- $ this ->assertEquals ($ expected_merged_range , $ merged_cell_range );
113- }
114- }
109+ $ this ->assertEquals ($ expected_merged_range , $ merged_cell_range );
115110
116111 $ zip ->close ();
117112 @unlink ($ filename );
@@ -139,35 +134,30 @@ public function testFreezeCells($freeze_cols, $freeze_rows, $expected_active_cel
139134
140135 $ zip = new ZipArchive ();
141136 $ r = $ zip ->open ($ filename );
142- $ this ->assertTrue ( $ r );
137+ $ xml = $ this ->extractSheetXml ( $ zip );
143138
139+ $ this ->assertTrue ($ r );
144140 $ this ->assertNotEmpty (($ zip ->numFiles ));
141+ $ this ->assertNotEmpty ($ xml );
145142
146- for ($ z =0 ; $ z < $ zip ->numFiles ; $ z ++) {
147- $ inside_zip_filename = $ zip ->getNameIndex ($ z );
148- $ sheet_xml = $ zip ->getFromName ($ inside_zip_filename );
149- if (preg_match ("/sheet(\d+).xml/ " , basename ($ inside_zip_filename ))) {
150- $ xml = new SimpleXMLElement ($ sheet_xml );
151- $ sheet_view = $ xml ->sheetViews ->sheetView ;
152-
153- if (!empty ($ expected_pane )) {
154- $ pane = $ sheet_view ->pane ;
155- foreach ($ expected_pane as $ expected_key => $ expected_value ) {
156- $ attribute = (string ) $ pane [0 ][$ expected_key ];
157- $ this ->assertEquals ($ expected_value , $ attribute );
158- }
159- }
143+ $ sheet_view = $ xml ->sheetViews ->sheetView ;
160144
161- $ selections = $ sheet_view ->selection ;
162- for ($ i = 0 ; $ i < count ($ expected_active_cells ); $ i ++) {
163- $ this ->assertNotEmpty ($ selections [$ i ]);
164- $ this ->assertEquals ($ expected_active_cells [$ i ]['cell ' ], $ selections [$ i ]['activeCell ' ]);
165- $ this ->assertEquals ($ expected_active_cells [$ i ]['cell ' ], $ selections [$ i ]['sqref ' ]);
166- $ this ->assertEquals ($ expected_active_cells [$ i ]['pane ' ], $ selections [$ i ]['pane ' ]);
167- }
145+ if (!empty ($ expected_pane )) {
146+ $ pane = $ sheet_view ->pane ;
147+ foreach ($ expected_pane as $ expected_key => $ expected_value ) {
148+ $ attribute = (string ) $ pane [0 ][$ expected_key ];
149+ $ this ->assertEquals ($ expected_value , $ attribute );
168150 }
169151 }
170152
153+ $ selections = $ sheet_view ->selection ;
154+ for ($ i = 0 ; $ i < count ($ expected_active_cells ); $ i ++) {
155+ $ this ->assertNotEmpty ($ selections [$ i ]);
156+ $ this ->assertEquals ($ expected_active_cells [$ i ]['cell ' ], $ selections [$ i ]['activeCell ' ]);
157+ $ this ->assertEquals ($ expected_active_cells [$ i ]['cell ' ], $ selections [$ i ]['sqref ' ]);
158+ $ this ->assertEquals ($ expected_active_cells [$ i ]['pane ' ], $ selections [$ i ]['pane ' ]);
159+ }
160+
171161 $ zip ->close ();
172162 @unlink ($ filename );
173163 }
@@ -240,37 +230,135 @@ public function testColumnsWidths() {
240230
241231 $ zip = new ZipArchive ();
242232 $ r = $ zip ->open ($ filename );
233+ $ xml = $ this ->extractSheetXml ($ zip );
234+
243235 $ this ->assertTrue ($ r );
236+ $ this ->assertNotEmpty (($ zip ->numFiles ));
237+ $ this ->assertNotEmpty ($ xml );
238+
239+ $ cols = $ xml ->cols ->col ;
240+ foreach ($ widths as $ col_index => $ col_width ) {
241+ $ col = $ cols [$ col_index ];
242+ $ this ->assertFalse (filter_var ($ col ["collapsed " ], FILTER_VALIDATE_BOOLEAN ));
243+ $ this ->assertFalse (filter_var ($ col ["hidden " ], FILTER_VALIDATE_BOOLEAN ));
244+ $ this ->assertTrue (filter_var ($ col ["customWidth " ], FILTER_VALIDATE_BOOLEAN ));
245+ $ this ->assertEquals ($ col_index + 1 , (string ) $ col ["max " ]);
246+ $ this ->assertEquals ($ col_index + 1 , (string ) $ col ["min " ]);
247+ $ this ->assertEquals ("0 " , (string ) $ col ["style " ]);
248+ $ this ->assertEquals ($ col_width , (string ) $ col ["width " ]);
249+ }
250+ $ last_col_index = count ($ widths );
251+ $ last_col = $ cols [$ last_col_index ];
252+ $ this ->assertFalse (filter_var ($ last_col ["collapsed " ], FILTER_VALIDATE_BOOLEAN ));
253+ $ this ->assertFalse (filter_var ($ last_col ["hidden " ], FILTER_VALIDATE_BOOLEAN ));
254+ $ this ->assertFalse (filter_var ($ last_col ["customWidth " ], FILTER_VALIDATE_BOOLEAN ));
255+ $ this ->assertEquals ("1024 " , (string ) $ last_col ["max " ]);
256+ $ this ->assertEquals ($ last_col_index + 1 , (string ) $ last_col ["min " ]);
257+ $ this ->assertEquals ("0 " , (string ) $ last_col ["style " ]);
258+ $ this ->assertEquals ("11.5 " , (string ) $ last_col ["width " ]);
259+
260+ $ zip ->close ();
261+ @unlink ($ filename );
262+ }
263+
264+ public function testRowHeight () {
265+ $ filename = tempnam ("/tmp " , "xlsx_writer " );
244266
267+ $ sheet = [
268+ ['55 ' ,'66 ' ,'77 ' ,'88 ' ],
269+ ['10 ' ,'11 ' ,'12 ' ,'13 ' ],
270+ ];
271+
272+ $ custom_height = 20.5 ;
273+
274+ $ row_options = ['height ' => $ custom_height ];
275+
276+ $ xlsx_writer = new XLSXWriter ();
277+ $ xlsx_writer ->writeSheetRow ('mysheet ' , $ sheet [0 ], $ format = 'xlsx ' , $ delimiter = '; ' , $ row_options );
278+ $ xlsx_writer ->writeSheetRow ('mysheet ' , $ sheet [1 ]);
279+ $ xlsx_writer ->writeToFile ($ filename );
280+
281+ $ zip = new ZipArchive ();
282+ $ r = $ zip ->open ($ filename );
283+ $ xml = $ this ->extractSheetXml ($ zip );
284+
285+ $ this ->assertTrue ($ r );
245286 $ this ->assertNotEmpty (($ zip ->numFiles ));
287+ $ this ->assertNotEmpty ($ xml );
246288
247- for ($ z =0 ; $ z < $ zip ->numFiles ; $ z ++) {
248- $ inside_zip_filename = $ zip ->getNameIndex ($ z );
249- $ sheet_xml = $ zip ->getFromName ($ inside_zip_filename );
250- if (preg_match ("/sheet(\d+).xml/ " , basename ($ inside_zip_filename ))) {
251- $ xml = new SimpleXMLElement ($ sheet_xml );
252- $ cols = $ xml ->cols ->col ;
253- foreach ($ widths as $ col_index => $ col_width ) {
254- $ col = $ cols [$ col_index ];
255- $ this ->assertFalse (filter_var ($ col ["collapsed " ], FILTER_VALIDATE_BOOLEAN ));
256- $ this ->assertFalse (filter_var ($ col ["hidden " ], FILTER_VALIDATE_BOOLEAN ));
257- $ this ->assertTrue (filter_var ($ col ["customWidth " ], FILTER_VALIDATE_BOOLEAN ));
258- $ this ->assertEquals ($ col_index + 1 , (string ) $ col ["max " ]);
259- $ this ->assertEquals ($ col_index + 1 , (string ) $ col ["min " ]);
260- $ this ->assertEquals ("0 " , (string ) $ col ["style " ]);
261- $ this ->assertEquals ($ col_width , (string ) $ col ["width " ]);
262- }
263- $ last_col_index = count ($ widths );
264- $ last_col = $ cols [$ last_col_index ];
265- $ this ->assertFalse (filter_var ($ last_col ["collapsed " ], FILTER_VALIDATE_BOOLEAN ));
266- $ this ->assertFalse (filter_var ($ last_col ["hidden " ], FILTER_VALIDATE_BOOLEAN ));
267- $ this ->assertFalse (filter_var ($ last_col ["customWidth " ], FILTER_VALIDATE_BOOLEAN ));
268- $ this ->assertEquals ("1024 " , (string ) $ last_col ["max " ]);
269- $ this ->assertEquals ($ last_col_index + 1 , (string ) $ last_col ["min " ]);
270- $ this ->assertEquals ("0 " , (string ) $ last_col ["style " ]);
271- $ this ->assertEquals ("11.5 " , (string ) $ last_col ["width " ]);
272- }
273- }
289+ $ rows = $ xml ->sheetData ->row ;
290+ $ this ->assertRowProperties ($ custom_height , $ expected_custom_height = true , $ expected_hidden = false , $ expected_collapsed = false , $ rows [0 ]);
291+ $ this ->assertRowProperties ($ expected_height = 12.1 , $ expected_custom_height = false , $ expected_hidden = false , $ expected_collapsed = false , $ rows [1 ]);
292+
293+ $ zip ->close ();
294+ @unlink ($ filename );
295+ }
296+
297+ public function testRowHidden () {
298+ $ filename = tempnam ("/tmp " , "xlsx_writer " );
299+
300+ $ sheet = [
301+ ['55 ' ,'66 ' ,'77 ' ,'88 ' ],
302+ ['10 ' ,'11 ' ,'12 ' ,'13 ' ],
303+ ];
304+
305+ $ row_options = ['hidden ' => true ];
306+
307+ $ expected_height = 12.1 ;
308+ $ expected_custom_height = false ;
309+ $ expected_collapsed = false ;
310+
311+ $ xlsx_writer = new XLSXWriter ();
312+ $ xlsx_writer ->writeSheetRow ('mysheet ' , $ sheet [0 ], $ format = 'xlsx ' , $ delimiter = '; ' , $ row_options );
313+ $ xlsx_writer ->writeSheetRow ('mysheet ' , $ sheet [1 ]);
314+ $ xlsx_writer ->writeToFile ($ filename );
315+
316+ $ zip = new ZipArchive ();
317+ $ r = $ zip ->open ($ filename );
318+ $ xml = $ this ->extractSheetXml ($ zip );
319+
320+ $ this ->assertTrue ($ r );
321+ $ this ->assertNotEmpty (($ zip ->numFiles ));
322+ $ this ->assertNotEmpty ($ xml );
323+
324+ $ rows = $ xml ->sheetData ->row ;
325+ $ this ->assertRowProperties ($ expected_height , $ expected_custom_height , $ expected_hidden = true , $ expected_collapsed , $ rows [0 ]);
326+ $ this ->assertRowProperties ($ expected_height , $ expected_custom_height , $ expected_hidden = false , $ expected_collapsed , $ rows [1 ]);
327+
328+ $ zip ->close ();
329+ @unlink ($ filename );
330+ }
331+
332+ public function testRowCollapsed () {
333+ $ filename = tempnam ("/tmp " , "xlsx_writer " );
334+
335+ $ sheet = [
336+ ['55 ' ,'66 ' ,'77 ' ,'88 ' ],
337+ ['10 ' ,'11 ' ,'12 ' ,'13 ' ],
338+ ];
339+
340+ $ row_options = ['collapsed ' => true ];
341+
342+ $ expected_height = 12.1 ;
343+ $ expected_custom_height = false ;
344+ $ expected_hidden = false ;
345+
346+ $ xlsx_writer = new XLSXWriter ();
347+ $ xlsx_writer ->writeSheetRow ('mysheet ' , $ sheet [0 ], $ format = 'xlsx ' , $ delimiter = '; ' , $ row_options );
348+ $ xlsx_writer ->writeSheetRow ('mysheet ' , $ sheet [1 ]);
349+ $ xlsx_writer ->writeToFile ($ filename );
350+
351+ $ zip = new ZipArchive ();
352+ $ r = $ zip ->open ($ filename );
353+ $ xml = $ this ->extractSheetXml ($ zip );
354+
355+ $ this ->assertTrue ($ r );
356+ $ this ->assertNotEmpty (($ zip ->numFiles ));
357+ $ this ->assertNotEmpty ($ xml );
358+
359+ $ rows = $ xml ->sheetData ->row ;
360+ $ this ->assertRowProperties ($ expected_height , $ expected_custom_height , $ expected_hidden , $ expected_collapsed = true , $ rows [0 ]);
361+ $ this ->assertRowProperties ($ expected_height , $ expected_custom_height , $ expected_hidden , $ expected_collapsed = false , $ rows [1 ]);
274362
275363 $ zip ->close ();
276364 @unlink ($ filename );
@@ -310,4 +398,23 @@ public static function array_diff_assoc_recursive($array1, $array2) {
310398
311399 return empty ($ difference ) ? [] : $ difference ;
312400 }
401+
402+ private function extractSheetXml ($ zip ) {
403+ for ($ z =0 ; $ z < $ zip ->numFiles ; $ z ++) {
404+ $ inside_zip_filename = $ zip ->getNameIndex ($ z );
405+ $ sheet_xml = $ zip ->getFromName ($ inside_zip_filename );
406+ if (preg_match ("/sheet(\d+).xml/ " , basename ($ inside_zip_filename ))) {
407+ return new SimpleXMLElement ($ sheet_xml );
408+ }
409+ }
410+
411+ return null ;
412+ }
413+
414+ private function assertRowProperties ($ expected_height , $ expected_custom_height , $ expected_hidden , $ expected_collapsed , $ row ) {
415+ $ this ->assertEquals ($ expected_height , (string )$ row ['ht ' ]);
416+ $ this ->assertEquals ($ expected_custom_height , filter_var ($ row ['customHeight ' ], FILTER_VALIDATE_BOOLEAN ));
417+ $ this ->assertEquals ($ expected_hidden , filter_var ($ row ['hidden ' ], FILTER_VALIDATE_BOOLEAN ));
418+ $ this ->assertEquals ($ expected_collapsed , filter_var ($ row ['collapsed ' ], FILTER_VALIDATE_BOOLEAN ));
419+ }
313420}
0 commit comments