11import { XmlHelper } from '../helper/xml-helper' ;
22import ModifyXmlHelper from '../helper/modify-xml-helper' ;
3- import { TableData , TableRow , TableRowStyle } from '../types/table-types' ;
3+ import {
4+ ModifyTableParams ,
5+ TableData ,
6+ TableRow ,
7+ TableRowStyle ,
8+ } from '../types/table-types' ;
49import { Border , Modification , ModificationTags } from '../types/modify-types' ;
510import ModifyTextHelper from '../helper/modify-text-helper' ;
611import { ModifyColorHelper } from '../index' ;
@@ -11,25 +16,38 @@ export class ModifyTable {
1116 data : TableData ;
1217 table : ModifyXmlHelper ;
1318 xml : XmlDocument | XmlElement ;
19+ maxCols : number = 0 ;
20+ params : ModifyTableParams ;
1421
1522 constructor ( table : XmlDocument | XmlElement , data ?: TableData ) {
1623 this . data = data ;
1724
1825 this . table = new ModifyXmlHelper ( table ) ;
1926 this . xml = table ;
27+
28+ this . data . body . forEach ( ( row ) => {
29+ this . maxCols =
30+ row . values . length > this . maxCols ? row . values . length : this . maxCols ;
31+ } ) ;
2032 }
2133
22- modify ( ) : ModifyTable {
34+ modify ( params ?: ModifyTableParams ) : ModifyTable {
35+ this . params = params ;
36+
2337 this . setRows ( ) ;
2438 this . setGridCols ( ) ;
2539
26- // this.sliceRows();
27- // this.sliceCols();
40+ this . sliceRows ( ) ;
41+ this . sliceCols ( ) ;
2842
2943 return this ;
3044 }
3145
3246 setRows ( ) {
47+ const alreadyExpanded = this . params ?. expand ?. find (
48+ ( expand ) => expand . mode === 'column' ,
49+ ) ;
50+
3351 this . data . body . forEach ( ( row : TableRow , r : number ) => {
3452 row . values . forEach ( ( cell : number | string , c : number ) => {
3553 const rowStyles = row . styles && row . styles [ c ] ? row . styles [ c ] : { } ;
@@ -42,12 +60,29 @@ export class ModifyTable {
4260 modify : ModifyXmlHelper . attribute ( 'val' , r ) ,
4361 } ,
4462 } ) ;
63+
64+ if ( ! alreadyExpanded ) {
65+ this . expandOtherMergedCellsInColumn ( c , r ) ;
66+ }
4567 } ) ;
4668 } ) ;
4769 }
4870
71+ expandOtherMergedCellsInColumn ( c : number , r : number ) {
72+ const rows = this . xml . getElementsByTagName ( 'a:tr' ) ;
73+ for ( let rs = 0 ; rs < rows . length ; rs ++ ) {
74+ // Skip current row
75+ if ( r !== rs ) {
76+ const row = rows . item ( r ) ;
77+ const columns = row . getElementsByTagName ( 'a:tc' ) ;
78+ const sourceCell = columns . item ( c ) ;
79+ this . expandGridSpan ( sourceCell ) ;
80+ }
81+ }
82+ }
83+
4984 setGridCols ( ) {
50- this . data . body [ 0 ] ?. values . forEach ( ( cell , c : number ) => {
85+ for ( let c = 0 ; c <= this . maxCols ; c ++ ) {
5186 this . table . modify ( {
5287 'a:gridCol' : {
5388 index : c ,
@@ -57,7 +92,7 @@ export class ModifyTable {
5792 modify : ModifyXmlHelper . attribute ( 'val' , c ) ,
5893 } ,
5994 } ) ;
60- } ) ;
95+ }
6196 }
6297
6398 sliceRows ( ) {
@@ -68,7 +103,7 @@ export class ModifyTable {
68103
69104 sliceCols ( ) {
70105 this . table . modify ( {
71- 'a:tblGrid' : this . slice ( 'a:gridCol' , this . data . body [ 0 ] ?. values . length ) ,
106+ 'a:tblGrid' : this . slice ( 'a:gridCol' , this . maxCols ) ,
72107 } ) ;
73108 }
74109
@@ -86,6 +121,7 @@ export class ModifyTable {
86121 'a:tc' : {
87122 index : index ,
88123 children : children ,
124+ fromPrevious : true ,
89125 } ,
90126 } ;
91127 } ;
@@ -270,25 +306,20 @@ export class ModifyTable {
270306 sourceCell : XmlElement ,
271307 colId : number ,
272308 ) : XmlElement {
273- const gridSpan = sourceCell . getAttribute ( 'gridSpan' ) ;
274- const hMerge = sourceCell . getAttribute ( 'hMerge' ) ;
275-
276- if ( gridSpan ) {
277- const incrementGridSpan = Number ( gridSpan ) + 1 ;
278- sourceCell . setAttribute ( 'gridSpan' , String ( incrementGridSpan ) ) ;
309+ const hasGridSpan = this . expandGridSpan ( sourceCell ) ;
310+ if ( hasGridSpan ) {
279311 return columns . item ( colId + 1 ) . cloneNode ( true ) as XmlElement ;
280312 }
281313
314+ const hMerge = sourceCell . getAttribute ( 'hMerge' ) ;
282315 if ( hMerge ) {
283316 for ( let findCol = colId - 1 ; colId >= 0 ; colId -- ) {
284317 const previousSibling = columns . item ( findCol ) ;
285318 if ( ! previousSibling ) {
286319 break ;
287320 }
288- const hasSpan = previousSibling . getAttribute ( 'gridSpan' ) ;
289- if ( hasSpan ) {
290- const incrementGridSpan = Number ( hasSpan ) + 1 ;
291- previousSibling . setAttribute ( 'gridSpan' , String ( incrementGridSpan ) ) ;
321+ const siblingHasSpan = this . expandGridSpan ( previousSibling ) ;
322+ if ( siblingHasSpan ) {
292323 break ;
293324 }
294325 }
@@ -297,6 +328,15 @@ export class ModifyTable {
297328 return sourceCell . cloneNode ( true ) as XmlElement ;
298329 }
299330
331+ expandGridSpan ( sourceCell : XmlElement ) {
332+ const gridSpan = sourceCell . getAttribute ( 'gridSpan' ) ;
333+ if ( gridSpan ) {
334+ const incrementGridSpan = Number ( gridSpan ) + 1 ;
335+ sourceCell . setAttribute ( 'gridSpan' , String ( incrementGridSpan ) ) ;
336+ return true ;
337+ }
338+ }
339+
300340 expandGrid = ( count : number , colId : number , gridSpan : number ) => {
301341 const tblGrid = this . xml . getElementsByTagName ( 'a:tblGrid' ) . item ( 0 ) ;
302342 for ( let cs = 1 ; cs <= count ; cs ++ ) {
0 commit comments