@@ -30,7 +30,10 @@ const (
30
30
)
31
31
32
32
// adjustHelperFunc defines functions to adjust helper.
33
- var adjustHelperFunc = [7 ]func (* File , * xlsxWorksheet , string , adjustDirection , int , int , int ) error {
33
+ var adjustHelperFunc = [8 ]func (* File , * xlsxWorksheet , string , adjustDirection , int , int , int ) error {
34
+ func (f * File , ws * xlsxWorksheet , sheet string , dir adjustDirection , num , offset , sheetID int ) error {
35
+ return f .adjustConditionalFormats (ws , sheet , dir , num , offset , sheetID )
36
+ },
34
37
func (f * File , ws * xlsxWorksheet , sheet string , dir adjustDirection , num , offset , sheetID int ) error {
35
38
return f .adjustDefinedNames (ws , sheet , dir , num , offset , sheetID )
36
39
},
@@ -231,30 +234,38 @@ func (f *File) adjustSingleRowFormulas(sheet, sheetN string, r *xlsxRow, num, of
231
234
}
232
235
233
236
// adjustCellRef provides a function to adjust cell reference.
234
- func (f * File ) adjustCellRef (ref string , dir adjustDirection , num , offset int ) (string , error ) {
237
+ func (f * File ) adjustCellRef (ref string , dir adjustDirection , num , offset int ) (string , bool , error ) {
235
238
if ! strings .Contains (ref , ":" ) {
236
239
ref += ":" + ref
237
240
}
241
+ var delete bool
238
242
coordinates , err := rangeRefToCoordinates (ref )
239
243
if err != nil {
240
- return ref , err
244
+ return ref , delete , err
241
245
}
242
246
if dir == columns {
247
+ if offset < 0 && coordinates [0 ] == coordinates [2 ] {
248
+ delete = true
249
+ }
243
250
if coordinates [0 ] >= num {
244
251
coordinates [0 ] += offset
245
252
}
246
253
if coordinates [2 ] >= num {
247
254
coordinates [2 ] += offset
248
255
}
249
256
} else {
257
+ if offset < 0 && coordinates [1 ] == coordinates [3 ] {
258
+ delete = true
259
+ }
250
260
if coordinates [1 ] >= num {
251
261
coordinates [1 ] += offset
252
262
}
253
263
if coordinates [3 ] >= num {
254
264
coordinates [3 ] += offset
255
265
}
256
266
}
257
- return f .coordinatesToRangeRef (coordinates )
267
+ ref , err = f .coordinatesToRangeRef (coordinates )
268
+ return ref , delete , err
258
269
}
259
270
260
271
// adjustFormula provides a function to adjust formula reference and shared
@@ -265,7 +276,7 @@ func (f *File) adjustFormula(sheet, sheetN string, formula *xlsxF, dir adjustDir
265
276
}
266
277
var err error
267
278
if formula .Ref != "" && sheet == sheetN {
268
- if formula .Ref , err = f .adjustCellRef (formula .Ref , dir , num , offset ); err != nil {
279
+ if formula .Ref , _ , err = f .adjustCellRef (formula .Ref , dir , num , offset ); err != nil {
269
280
return err
270
281
}
271
282
if si && formula .Si != nil {
@@ -770,6 +781,29 @@ func (f *File) adjustVolatileDeps(ws *xlsxWorksheet, sheet string, dir adjustDir
770
781
return nil
771
782
}
772
783
784
+ // adjustConditionalFormats updates the cell reference of the worksheet
785
+ // conditional formatting when inserting or deleting rows or columns.
786
+ func (f * File ) adjustConditionalFormats (ws * xlsxWorksheet , sheet string , dir adjustDirection , num , offset , sheetID int ) error {
787
+ for i := 0 ; i < len (ws .ConditionalFormatting ); i ++ {
788
+ cf := ws .ConditionalFormatting [i ]
789
+ if cf == nil {
790
+ continue
791
+ }
792
+ ref , del , err := f .adjustCellRef (cf .SQRef , dir , num , offset )
793
+ if err != nil {
794
+ return err
795
+ }
796
+ if del {
797
+ ws .ConditionalFormatting = append (ws .ConditionalFormatting [:i ],
798
+ ws .ConditionalFormatting [i + 1 :]... )
799
+ i --
800
+ continue
801
+ }
802
+ ws .ConditionalFormatting [i ].SQRef = ref
803
+ }
804
+ return nil
805
+ }
806
+
773
807
// adjustDrawings updates the starting anchor of the two cell anchor pictures
774
808
// and charts object when inserting or deleting rows or columns.
775
809
func (from * xlsxFrom ) adjustDrawings (dir adjustDirection , num , offset int , editAs string ) (bool , error ) {
0 commit comments