@@ -38,7 +38,7 @@ const (
38
38
// row: Index number of the row we're inserting/deleting before
39
39
// offset: Number of rows/column to insert/delete negative values indicate deletion
40
40
//
41
- // TODO: adjustPageBreaks, adjustComments, adjustDataValidations, adjustProtectedCells
41
+ // TODO: adjustComments, adjustDataValidations, adjustDrawings, adjustPageBreaks , adjustProtectedCells
42
42
func (f * File ) adjustHelper (sheet string , dir adjustDirection , num , offset int ) error {
43
43
ws , err := f .workSheetReader (sheet )
44
44
if err != nil {
@@ -66,6 +66,9 @@ func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int)
66
66
if err = f .adjustCalcChain (dir , num , offset , sheetID ); err != nil {
67
67
return err
68
68
}
69
+ if err = f .adjustVolatileDeps (dir , num , offset , sheetID ); err != nil {
70
+ return err
71
+ }
69
72
if ws .MergeCells != nil && len (ws .MergeCells .Cells ) == 0 {
70
73
ws .MergeCells = nil
71
74
}
@@ -498,7 +501,8 @@ func (f *File) adjustTable(ws *xlsxWorksheet, sheet string, dir adjustDirection,
498
501
t .AutoFilter .Ref = t .Ref
499
502
}
500
503
_ = f .setTableColumns (sheet , true , x1 , y1 , x2 , & t )
501
- t .TotalsRowCount = 0
504
+ // Currently doesn't support query table
505
+ t .TableType , t .TotalsRowCount , t .ConnectionID = "" , 0 , 0
502
506
table , _ := xml .Marshal (t )
503
507
f .saveFileList (tableXML , table )
504
508
}
@@ -578,15 +582,13 @@ func (f *File) adjustMergeCells(ws *xlsxWorksheet, dir adjustDirection, num, off
578
582
if dir == rows {
579
583
if y1 == num && y2 == num && offset < 0 {
580
584
f .deleteMergeCell (ws , i )
581
- i --
582
585
continue
583
586
}
584
587
585
588
y1 , y2 = f .adjustMergeCellsHelper (y1 , y2 , num , offset )
586
589
} else {
587
590
if x1 == num && x2 == num && offset < 0 {
588
591
f .deleteMergeCell (ws , i )
589
- i --
590
592
continue
591
593
}
592
594
@@ -642,18 +644,15 @@ func (f *File) deleteMergeCell(ws *xlsxWorksheet, idx int) {
642
644
}
643
645
}
644
646
645
- // adjustCalcChainRef update the cell reference in calculation chain when
646
- // inserting or deleting rows or columns.
647
- func ( f * File ) adjustCalcChainRef ( i , c , r , offset int , dir adjustDirection ) {
647
+ // adjustCellName returns updated cell name by giving column/row number and
648
+ // offset on inserting or deleting rows or columns.
649
+ func adjustCellName ( cell string , dir adjustDirection , c , r , offset int ) ( string , error ) {
648
650
if dir == rows {
649
651
if rn := r + offset ; rn > 0 {
650
- f . CalcChain . C [ i ]. R , _ = CoordinatesToCellName (c , rn )
652
+ return CoordinatesToCellName (c , rn )
651
653
}
652
- return
653
- }
654
- if nc := c + offset ; nc > 0 {
655
- f .CalcChain .C [i ].R , _ = CoordinatesToCellName (nc , r )
656
654
}
655
+ return CoordinatesToCellName (c + offset , r )
657
656
}
658
657
659
658
// adjustCalcChain provides a function to update the calculation chain when
@@ -665,7 +664,8 @@ func (f *File) adjustCalcChain(dir adjustDirection, num, offset, sheetID int) er
665
664
// If sheet ID is omitted, it is assumed to be the same as the i value of
666
665
// the previous cell.
667
666
var prevSheetID int
668
- for index , c := range f .CalcChain .C {
667
+ for i := 0 ; i < len (f .CalcChain .C ); i ++ {
668
+ c := f .CalcChain .C [i ]
669
669
if c .I == 0 {
670
670
c .I = prevSheetID
671
671
}
@@ -680,16 +680,72 @@ func (f *File) adjustCalcChain(dir adjustDirection, num, offset, sheetID int) er
680
680
if dir == rows && num <= rowNum {
681
681
if num == rowNum && offset == - 1 {
682
682
_ = f .deleteCalcChain (c .I , c .R )
683
+ i --
683
684
continue
684
685
}
685
- f .adjustCalcChainRef ( index , colNum , rowNum , offset , dir )
686
+ f .CalcChain . C [ i ]. R , _ = adjustCellName ( c . R , dir , colNum , rowNum , offset )
686
687
}
687
688
if dir == columns && num <= colNum {
688
689
if num == colNum && offset == - 1 {
689
690
_ = f .deleteCalcChain (c .I , c .R )
691
+ i --
690
692
continue
691
693
}
692
- f .adjustCalcChainRef (index , colNum , rowNum , offset , dir )
694
+ f .CalcChain .C [i ].R , _ = adjustCellName (c .R , dir , colNum , rowNum , offset )
695
+ }
696
+ }
697
+ return nil
698
+ }
699
+
700
+ // adjustVolatileDepsTopic updates the volatile dependencies topic when
701
+ // inserting or deleting rows or columns.
702
+ func (vt * xlsxVolTypes ) adjustVolatileDepsTopic (cell string , dir adjustDirection , indexes []int ) (int , error ) {
703
+ num , offset , i1 , i2 , i3 , i4 := indexes [0 ], indexes [1 ], indexes [2 ], indexes [3 ], indexes [4 ], indexes [5 ]
704
+ colNum , rowNum , err := CellNameToCoordinates (cell )
705
+ if err != nil {
706
+ return i4 , err
707
+ }
708
+ if dir == rows && num <= rowNum {
709
+ if num == rowNum && offset == - 1 {
710
+ vt .deleteVolTopicRef (i1 , i2 , i3 , i4 )
711
+ i4 --
712
+ return i4 , err
713
+ }
714
+ vt .VolType [i1 ].Main [i2 ].Tp [i3 ].Tr [i4 ].R , _ = adjustCellName (cell , dir , colNum , rowNum , offset )
715
+ }
716
+ if dir == columns && num <= colNum {
717
+ if num == colNum && offset == - 1 {
718
+ vt .deleteVolTopicRef (i1 , i2 , i3 , i4 )
719
+ i4 --
720
+ return i4 , err
721
+ }
722
+ if name , _ := adjustCellName (cell , dir , colNum , rowNum , offset ); name != "" {
723
+ vt .VolType [i1 ].Main [i2 ].Tp [i3 ].Tr [i4 ].R , _ = adjustCellName (cell , dir , colNum , rowNum , offset )
724
+ }
725
+ }
726
+ return i4 , err
727
+ }
728
+
729
+ // adjustVolatileDeps updates the volatile dependencies when inserting or
730
+ // deleting rows or columns.
731
+ func (f * File ) adjustVolatileDeps (dir adjustDirection , num , offset , sheetID int ) error {
732
+ volTypes , err := f .volatileDepsReader ()
733
+ if err != nil || volTypes == nil {
734
+ return err
735
+ }
736
+ for i1 := 0 ; i1 < len (volTypes .VolType ); i1 ++ {
737
+ for i2 := 0 ; i2 < len (volTypes .VolType [i1 ].Main ); i2 ++ {
738
+ for i3 := 0 ; i3 < len (volTypes .VolType [i1 ].Main [i2 ].Tp ); i3 ++ {
739
+ for i4 := 0 ; i4 < len (volTypes .VolType [i1 ].Main [i2 ].Tp [i3 ].Tr ); i4 ++ {
740
+ ref := volTypes .VolType [i1 ].Main [i2 ].Tp [i3 ].Tr [i4 ]
741
+ if ref .S != sheetID {
742
+ continue
743
+ }
744
+ if i4 , err = volTypes .adjustVolatileDepsTopic (ref .R , dir , []int {num , offset , i1 , i2 , i3 , i4 }); err != nil {
745
+ return err
746
+ }
747
+ }
748
+ }
693
749
}
694
750
}
695
751
return nil
0 commit comments