@@ -197,8 +197,10 @@ var (
197
197
// calcContext defines the formula execution context.
198
198
type calcContext struct {
199
199
sync.Mutex
200
- entry string
201
- iterations map[string]uint
200
+ entry string
201
+ maxCalcIterations uint
202
+ iterations map[string]uint
203
+ iterationsCache map[string]formulaArg
202
204
}
203
205
204
206
// cellRef defines the structure of a cell reference.
@@ -774,8 +776,10 @@ func (f *File) CalcCellValue(sheet, cell string, opts ...Options) (result string
774
776
token formulaArg
775
777
)
776
778
if token, err = f.calcCellValue(&calcContext{
777
- entry: fmt.Sprintf("%s!%s", sheet, cell),
778
- iterations: make(map[string]uint),
779
+ entry: fmt.Sprintf("%s!%s", sheet, cell),
780
+ maxCalcIterations: getOptions(opts...).MaxCalcIterations,
781
+ iterations: make(map[string]uint),
782
+ iterationsCache: make(map[string]formulaArg),
779
783
}, sheet, cell); err != nil {
780
784
return
781
785
}
@@ -1527,17 +1531,6 @@ func (f *File) cellResolver(ctx *calcContext, sheet, cell string) (formulaArg, e
1527
1531
value string
1528
1532
err error
1529
1533
)
1530
- ref := fmt.Sprintf("%s!%s", sheet, cell)
1531
- if formula, _ := f.GetCellFormula(sheet, cell); len(formula) != 0 {
1532
- ctx.Lock()
1533
- if ctx.entry != ref && ctx.iterations[ref] <= f.options.MaxCalcIterations {
1534
- ctx.iterations[ref]++
1535
- ctx.Unlock()
1536
- arg, _ = f.calcCellValue(ctx, sheet, cell)
1537
- return arg, nil
1538
- }
1539
- ctx.Unlock()
1540
- }
1541
1534
if value, err = f.GetCellValue(sheet, cell, Options{RawCellValue: true}); err != nil {
1542
1535
return arg, err
1543
1536
}
@@ -1551,8 +1544,25 @@ func (f *File) cellResolver(ctx *calcContext, sheet, cell string) (formulaArg, e
1551
1544
return newEmptyFormulaArg(), err
1552
1545
}
1553
1546
return arg.ToNumber(), err
1554
- default :
1547
+ case CellTypeInlineString, CellTypeSharedString :
1555
1548
return arg, err
1549
+ case CellTypeFormula:
1550
+ ref := fmt.Sprintf("%s!%s", sheet, cell)
1551
+ if ctx.entry != ref {
1552
+ ctx.Lock()
1553
+ if ctx.iterations[ref] <= ctx.maxCalcIterations {
1554
+ ctx.iterations[ref]++
1555
+ ctx.Unlock()
1556
+ arg, _ = f.calcCellValue(ctx, sheet, cell)
1557
+ ctx.iterationsCache[ref] = arg
1558
+ return arg, nil
1559
+ }
1560
+ ctx.Unlock()
1561
+ return ctx.iterationsCache[ref], nil
1562
+ }
1563
+ fallthrough
1564
+ default:
1565
+ return newEmptyFormulaArg(), err
1556
1566
}
1557
1567
}
1558
1568
@@ -7746,7 +7756,7 @@ func (fn *formulaFuncs) COUNTBLANK(argsList *list.List) formulaArg {
7746
7756
}
7747
7757
var count float64
7748
7758
for _, cell := range argsList.Front().Value.(formulaArg).ToList() {
7749
- if cell.Value() == "" {
7759
+ if cell.Type == ArgEmpty {
7750
7760
count++
7751
7761
}
7752
7762
}
0 commit comments