@@ -14540,9 +14540,9 @@ func (fn *formulaFuncs) UNICODE(argsList *list.List) formulaArg {
14540
14540
// For syntax refer to
14541
14541
// https://support.microsoft.com/en-us/office/unique-function-c5ab87fd-30a3-4ce9-9d1a-40204fb85e1e.
14542
14542
func (fn *formulaFuncs) UNIQUE(argsList *list.List) formulaArg {
14543
- args := getFormulaUniqueArgs(argsList)
14544
- if args. errArg != nil {
14545
- return *args. errArg
14543
+ args, errArg := getFormulaUniqueArgs(argsList)
14544
+ if errArg != nil {
14545
+ return *errArg
14546
14546
}
14547
14547
14548
14548
if args.byColumn {
@@ -14561,18 +14561,14 @@ func (fn *formulaFuncs) UNIQUE(argsList *list.List) formulaArg {
14561
14561
}
14562
14562
14563
14563
uniqueAxes := [][]formulaArg{}
14564
- added := map[string]struct{}{}
14565
14564
14566
14565
for i := 0; i < len(args.cellRange); i += args.cols {
14567
14566
key := concatValues(args.cellRange[i : i+args.cols])
14568
- if _, ok := added[key]; ok {
14569
- continue
14570
- }
14571
- added[key] = struct{}{}
14572
14567
14573
14568
if (args.exactlyOnce && counts[key] == 1) || (!args.exactlyOnce && counts[key] >= 1) {
14574
14569
uniqueAxes = append(uniqueAxes, args.cellRange[i:i+args.cols])
14575
14570
}
14571
+ delete(counts, key)
14576
14572
}
14577
14573
14578
14574
if args.byColumn {
@@ -14622,70 +14618,67 @@ func concatValues(args []formulaArg) string {
14622
14618
return val
14623
14619
}
14624
14620
14625
- type formulaUniqueArgs struct {
14621
+ type uniqueArgs struct {
14626
14622
cellRange []formulaArg
14627
14623
cols int
14628
14624
rows int
14629
14625
byColumn bool
14630
14626
exactlyOnce bool
14631
- errArg *formulaArg
14632
14627
}
14633
14628
14634
- func getFormulaUniqueArgs(argsList *list.List) formulaUniqueArgs {
14629
+ func getFormulaUniqueArgs(argsList *list.List) (uniqueArgs, *formulaArg) {
14630
+ res := uniqueArgs{}
14631
+
14635
14632
argsLen := argsList.Len()
14636
14633
if argsLen == 0 {
14637
14634
errArg := newErrorFormulaArg(formulaErrorVALUE, "UNIQUE requires at least 1 argument")
14638
- return formulaUniqueArgs{errArg: &errArg}
14635
+ return res, &errArg
14639
14636
}
14640
14637
14641
- argRange := argsList.Front().Value.(formulaArg).ToList()
14642
- if argRange == nil || argsLen > 3 {
14638
+ if argsLen > 3 {
14643
14639
msg := fmt.Sprintf("UNIQUE takes at most 3 arguments, received %d arguments", argsLen)
14644
14640
errArg := newErrorFormulaArg(formulaErrorVALUE, msg)
14645
14641
14646
- return formulaUniqueArgs{errArg: &errArg}
14642
+ return res, &errArg
14643
+ }
14644
+
14645
+ firstArg := argsList.Front()
14646
+ res.cellRange = firstArg.Value.(formulaArg).ToList()
14647
+ if len(res.cellRange) == 0 {
14648
+ errArg := newErrorFormulaArg(formulaErrorVALUE, "missing first argument to UNIQUE")
14649
+ return res, &errArg
14650
+ }
14651
+ if res.cellRange[0].Type == ArgError {
14652
+ return res, &res.cellRange[0]
14647
14653
}
14648
14654
14649
14655
rmin, rmax := calcColsRowsMinMax(false, argsList)
14650
14656
cmin, cmax := calcColsRowsMinMax(true, argsList)
14651
- cols, rows : = cmax-cmin+1, rmax-rmin+1
14657
+ res. cols, res. rows = cmax-cmin+1, rmax-rmin+1
14652
14658
14653
- sndArg := argsList.Front().Next()
14654
- if sndArg == nil {
14655
- return formulaUniqueArgs{
14656
- cellRange: argRange,
14657
- cols: cols,
14658
- rows: rows,
14659
- }
14659
+ secondArg := firstArg.Next()
14660
+ if secondArg == nil {
14661
+ return res, nil
14660
14662
}
14661
14663
14662
- argByColumn := sndArg .Value.(formulaArg).ToBool()
14664
+ argByColumn := secondArg .Value.(formulaArg).ToBool()
14663
14665
if argByColumn.Type == ArgError {
14664
- return formulaUniqueArgs{errArg: &argByColumn}
14666
+ return res, &argByColumn
14665
14667
}
14668
+ res.byColumn = (argByColumn.Value() == "TRUE")
14666
14669
14667
- trdArg := argsList.Front().Next().Next()
14668
- if trdArg == nil {
14669
- return formulaUniqueArgs{
14670
- cellRange: argRange,
14671
- cols: cols,
14672
- rows: rows,
14673
- byColumn: argByColumn.Value() == "TRUE",
14674
- }
14670
+ thirdArg := secondArg.Next()
14671
+ if thirdArg == nil {
14672
+ return res, nil
14675
14673
}
14676
14674
14677
- argExactlyOnce := trdArg .Value.(formulaArg).ToBool()
14675
+ argExactlyOnce := thirdArg .Value.(formulaArg).ToBool()
14678
14676
if argExactlyOnce.Type == ArgError {
14679
- return formulaUniqueArgs{errArg: &argExactlyOnce}
14677
+ return res, &argExactlyOnce
14680
14678
}
14679
+ res.exactlyOnce = (argExactlyOnce.Value() == "TRUE")
14681
14680
14682
- return formulaUniqueArgs{
14683
- cellRange: argRange,
14684
- cols: cols,
14685
- rows: rows,
14686
- byColumn: argByColumn.Value() == "TRUE",
14687
- exactlyOnce: argExactlyOnce.Value() == "TRUE",
14688
- }
14681
+ return res, nil
14689
14682
}
14690
14683
14691
14684
// UPPER converts all characters in a supplied text string to upper case. The
0 commit comments