Skip to content

Commit e1abdb0

Browse files
committed
This closes qax-os#809, and add new fn: HARMEAN
1 parent ab2c1c8 commit e1abdb0

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

calc.go

+35
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ var tokenPriority = map[string]int{
282282
// GAMMA
283283
// GAMMALN
284284
// GCD
285+
// HARMEAN
285286
// HEX2BIN
286287
// HEX2DEC
287288
// HEX2OCT
@@ -3927,6 +3928,40 @@ func (fn *formulaFuncs) GAMMALN(argsList *list.List) formulaArg {
39273928
return newErrorFormulaArg(formulaErrorVALUE, "GAMMALN requires 1 numeric argument")
39283929
}
39293930

3931+
// HARMEAN function calculates the harmonic mean of a supplied set of values.
3932+
// The syntax of the function is:
3933+
//
3934+
// HARMEAN(number1,[number2],...)
3935+
//
3936+
func (fn *formulaFuncs) HARMEAN(argsList *list.List) formulaArg {
3937+
if argsList.Len() < 1 {
3938+
return newErrorFormulaArg(formulaErrorVALUE, "HARMEAN requires at least 1 argument")
3939+
}
3940+
if min := fn.MIN(argsList); min.Number < 0 {
3941+
return newErrorFormulaArg(formulaErrorNA, formulaErrorNA)
3942+
}
3943+
number, val, cnt := 0.0, 0.0, 0.0
3944+
for token := argsList.Front(); token != nil; token = token.Next() {
3945+
arg := token.Value.(formulaArg)
3946+
switch arg.Type {
3947+
case ArgString:
3948+
num := arg.ToNumber()
3949+
if num.Type != ArgNumber {
3950+
continue
3951+
}
3952+
number = num.Number
3953+
case ArgNumber:
3954+
number = arg.Number
3955+
}
3956+
if number <= 0 {
3957+
return newErrorFormulaArg(formulaErrorNA, formulaErrorNA)
3958+
}
3959+
val += (1 / number)
3960+
cnt++
3961+
}
3962+
return newNumberFormulaArg(1 / (val / cnt))
3963+
}
3964+
39303965
// KURT function calculates the kurtosis of a supplied set of values. The
39313966
// syntax of the function is:
39323967
//

calc_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,9 @@ func TestCalcCellValue(t *testing.T) {
605605
// GAMMALN
606606
"=GAMMALN(4.5)": "2.453736570842443",
607607
"=GAMMALN(INT(1))": "0",
608+
// HARMEAN
609+
"=HARMEAN(2.5,3,0.5,1,3)": "1.229508196721312",
610+
"=HARMEAN(\"2.5\",3,0.5,1,INT(3),\"\")": "1.229508196721312",
608611
// KURT
609612
"=KURT(F1:F9)": "-1.033503502551368",
610613
"=KURT(F1,F2:F9)": "-1.033503502551368",
@@ -1393,6 +1396,10 @@ func TestCalcCellValue(t *testing.T) {
13931396
"=GAMMALN(F1)": "GAMMALN requires 1 numeric argument",
13941397
"=GAMMALN(0)": "#N/A",
13951398
"=GAMMALN(INT(0))": "#N/A",
1399+
// HARMEAN
1400+
"=HARMEAN()": "HARMEAN requires at least 1 argument",
1401+
"=HARMEAN(-1)": "#N/A",
1402+
"=HARMEAN(0)": "#N/A",
13961403
// KURT
13971404
"=KURT()": "KURT requires at least 1 argument",
13981405
"=KURT(F1,INT(1))": "#DIV/0!",

styles.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2472,8 +2472,8 @@ func newAlignment(style *Style) *xlsxAlignment {
24722472
func newProtection(style *Style) *xlsxProtection {
24732473
var protection xlsxProtection
24742474
if style.Protection != nil {
2475-
protection.Hidden = style.Protection.Hidden
2476-
protection.Locked = style.Protection.Locked
2475+
protection.Hidden = &style.Protection.Hidden
2476+
protection.Locked = &style.Protection.Locked
24772477
}
24782478
return &protection
24792479
}

xmlStyles.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ type xlsxAlignment struct {
4949
// set. The cell protection properties do not take effect unless the sheet has
5050
// been protected.
5151
type xlsxProtection struct {
52-
Hidden bool `xml:"hidden,attr"`
53-
Locked bool `xml:"locked,attr"`
52+
Hidden *bool `xml:"hidden,attr"`
53+
Locked *bool `xml:"locked,attr"`
5454
}
5555

5656
// xlsxLine expresses a single set of cell border.

0 commit comments

Comments
 (0)