@@ -239,6 +239,17 @@ func (f *File) adjustSingleRowFormulas(sheet, sheetN string, r *xlsxRow, num, of
239
239
// adjustCellRef provides a function to adjust cell reference.
240
240
func (f * File ) adjustCellRef (cellRef string , dir adjustDirection , num , offset int ) (string , error ) {
241
241
var SQRef []string
242
+ applyOffset := func (coordinates []int , idx1 , idx2 , maxVal int ) []int {
243
+ if coordinates [idx1 ] >= num {
244
+ coordinates [idx1 ] += offset
245
+ }
246
+ if coordinates [idx2 ] >= num {
247
+ if coordinates [idx2 ] += offset ; coordinates [idx2 ] > maxVal {
248
+ coordinates [idx2 ] = maxVal
249
+ }
250
+ }
251
+ return coordinates
252
+ }
242
253
for _ , ref := range strings .Split (cellRef , " " ) {
243
254
if ! strings .Contains (ref , ":" ) {
244
255
ref += ":" + ref
@@ -251,22 +262,12 @@ func (f *File) adjustCellRef(cellRef string, dir adjustDirection, num, offset in
251
262
if offset < 0 && coordinates [0 ] == coordinates [2 ] {
252
263
continue
253
264
}
254
- if coordinates [0 ] >= num {
255
- coordinates [0 ] += offset
256
- }
257
- if coordinates [2 ] >= num {
258
- coordinates [2 ] += offset
259
- }
265
+ coordinates = applyOffset (coordinates , 0 , 2 , MaxColumns )
260
266
} else {
261
267
if offset < 0 && coordinates [1 ] == coordinates [3 ] {
262
268
continue
263
269
}
264
- if coordinates [1 ] >= num {
265
- coordinates [1 ] += offset
266
- }
267
- if coordinates [3 ] >= num {
268
- coordinates [3 ] += offset
269
- }
270
+ coordinates = applyOffset (coordinates , 1 , 3 , TotalRows )
270
271
}
271
272
if ref , err = coordinatesToRangeRef (coordinates ); err != nil {
272
273
return "" , err
@@ -446,12 +447,8 @@ func (f *File) adjustFormulaRef(sheet, sheetN, formula string, keepRelative bool
446
447
val += operand
447
448
continue
448
449
}
449
- if isFunctionStartToken (token ) {
450
- val += token .TValue + string (efp .ParenOpen )
451
- continue
452
- }
453
- if isFunctionStopToken (token ) {
454
- val += token .TValue + string (efp .ParenClose )
450
+ if paren := transformParenthesesToken (token ); paren != "" {
451
+ val += transformParenthesesToken (token )
455
452
continue
456
453
}
457
454
if token .TType == efp .TokenTypeOperand && token .TSubType == efp .TokenSubTypeText {
@@ -463,6 +460,18 @@ func (f *File) adjustFormulaRef(sheet, sheetN, formula string, keepRelative bool
463
460
return val , nil
464
461
}
465
462
463
+ // transformParenthesesToken returns formula part with parentheses by given
464
+ // token.
465
+ func transformParenthesesToken (token efp.Token ) string {
466
+ if isFunctionStartToken (token ) || isBeginParenthesesToken (token ) {
467
+ return token .TValue + string (efp .ParenOpen )
468
+ }
469
+ if isFunctionStopToken (token ) || isEndParenthesesToken (token ) {
470
+ return token .TValue + string (efp .ParenClose )
471
+ }
472
+ return ""
473
+ }
474
+
466
475
// adjustRangeSheetName returns replaced range reference by given source and
467
476
// target sheet name.
468
477
func adjustRangeSheetName (rng , source , target string ) string {
@@ -551,12 +560,8 @@ func transformArrayFormula(tokens []efp.Token, afs []arrayFormulaOperandToken) s
551
560
if skip {
552
561
continue
553
562
}
554
- if isFunctionStartToken (token ) {
555
- val += token .TValue + string (efp .ParenOpen )
556
- continue
557
- }
558
- if isFunctionStopToken (token ) {
559
- val += token .TValue + string (efp .ParenClose )
563
+ if paren := transformParenthesesToken (token ); paren != "" {
564
+ val += transformParenthesesToken (token )
560
565
continue
561
566
}
562
567
if token .TType == efp .TokenTypeOperand && token .TSubType == efp .TokenSubTypeText {
@@ -985,14 +990,14 @@ func (f *File) adjustDataValidations(ws *xlsxWorksheet, sheet string, dir adjust
985
990
worksheet .DataValidations .DataValidation [i ].Sqref = ref
986
991
}
987
992
if worksheet .DataValidations .DataValidation [i ].Formula1 != nil {
988
- formula := unescapeDataValidationFormula (worksheet .DataValidations .DataValidation [i ].Formula1 .Content )
993
+ formula := formulaUnescaper . Replace (worksheet .DataValidations .DataValidation [i ].Formula1 .Content )
989
994
if formula , err = f .adjustFormulaRef (sheet , sheetN , formula , false , dir , num , offset ); err != nil {
990
995
return err
991
996
}
992
997
worksheet .DataValidations .DataValidation [i ].Formula1 = & xlsxInnerXML {Content : formulaEscaper .Replace (formula )}
993
998
}
994
999
if worksheet .DataValidations .DataValidation [i ].Formula2 != nil {
995
- formula := unescapeDataValidationFormula (worksheet .DataValidations .DataValidation [i ].Formula2 .Content )
1000
+ formula := formulaUnescaper . Replace (worksheet .DataValidations .DataValidation [i ].Formula2 .Content )
996
1001
if formula , err = f .adjustFormulaRef (sheet , sheetN , formula , false , dir , num , offset ); err != nil {
997
1002
return err
998
1003
}
0 commit comments