@@ -816,6 +816,7 @@ private function getRowColumns(array $row): array
816
816
*/
817
817
private function calculateColumnsWidth (iterable $ groups )
818
818
{
819
+ $ pass2 = count ($ this ->effectiveColumnWidths );
819
820
for ($ column = 0 ; $ column < $ this ->numberOfColumns ; ++$ column ) {
820
821
$ lengths = [];
821
822
foreach ($ groups as $ group ) {
@@ -843,28 +844,44 @@ private function calculateColumnsWidth(iterable $groups)
843
844
844
845
$ this ->effectiveColumnWidths [$ column ] = max ($ lengths ) + Helper::width ($ this ->style ->getCellRowContentFormat ()) - 2 ;
845
846
}
847
+
848
+ if ($ pass2 ) {
849
+ $ this ->effectiveColumnWidths = array_values ($ this ->effectiveColumnWidths );
850
+ $ this ->columnMaxWidths = array_values ($ this ->columnMaxWidths );
851
+ $ this ->columnStyles = array_values ($ this ->columnStyles );
852
+ $ this ->columnWidths = array_values ($ this ->columnWidths );
853
+ }
846
854
}
847
855
848
856
private function dropColumn (iterable $ groups )
849
857
{
850
858
$ effectiveColumnWidths = $ this ->effectiveColumnWidths ;
859
+ $ optionalColumns = [];
860
+
861
+ foreach ($ this ->optionalColumns as $ column ) {
862
+ $ optionalColumns [$ column ] = $ effectiveColumnWidths [$ column ];
863
+ }
851
864
852
865
for ($ column = $ this ->numberOfColumns ; $ this ->numberOfColumns > 0 ; --$ column ) {
853
866
if ($ this ->maxWidth && $ this ->maxWidth > array_sum ($ effectiveColumnWidths )) {
854
867
break ;
855
868
}
856
869
857
- $ droppedColumn = array_pop ($ this ->optionalColumns );
858
- unset($ effectiveColumnWidths [$ droppedColumn ]);
859
- $ this ->droppedColumns [] = $ droppedColumn ;
870
+ $ largestOptionalColumn = array_keys ($ optionalColumns , max ($ optionalColumns ))[0 ];
871
+ unset($ effectiveColumnWidths [$ largestOptionalColumn ], $ optionalColumns [$ largestOptionalColumn ]);
872
+ $ this ->droppedColumns [] = $ largestOptionalColumn ;
873
+
874
+ if (empty ($ optionalColumns )) {
875
+ break ;
876
+ }
860
877
}
861
878
862
879
if ($ this ->droppedColumns ) {
863
880
foreach ($ this ->droppedColumns as $ column ) {
864
881
foreach ($ groups as $ group ) {
865
882
foreach ($ group as $ row ) {
866
883
foreach ($ row as $ index => $ cell ) {
867
- if ($ cell instanceof TableCell && $ index + $ cell ->getColspan () >= $ column ) {
884
+ if ($ cell instanceof TableCell && $ index + $ cell ->getColspan () > $ column ) {
868
885
$ cell ->reduceColspan ();
869
886
}
870
887
}
@@ -879,11 +896,6 @@ private function dropColumn(iterable $groups)
879
896
);
880
897
}
881
898
882
- $ this ->effectiveColumnWidths = array_values ($ this ->effectiveColumnWidths );
883
- $ this ->columnMaxWidths = array_values ($ this ->columnMaxWidths );
884
- $ this ->columnStyles = array_values ($ this ->columnStyles );
885
- $ this ->columnWidths = array_values ($ this ->columnWidths );
886
-
887
899
$ this ->numberOfColumns -= count ($ this ->droppedColumns );
888
900
}
889
901
}
0 commit comments