calc_test.go 30.6 KB
Newer Older
1 2 3
package excelize

import (
4
	"container/list"
xurime's avatar
xurime 已提交
5
	"path/filepath"
6
	"strings"
7 8 9
	"testing"

	"github.com/stretchr/testify/assert"
10
	"github.com/xuri/efp"
11 12 13
)

func TestCalcCellValue(t *testing.T) {
xurime's avatar
xurime 已提交
14 15 16 17 18 19 20 21 22 23 24
	cellData := [][]interface{}{
		{1, 4, nil, "Month", "Team", "Sales"},
		{2, 5, nil, "Jan", "North 1", 36693},
		{3, nil, nil, "Jan", "North 2", 22100},
		{0, nil, nil, "Jan", "South 1", 53321},
		{nil, nil, nil, "Jan", "South 2", 34440},
		{nil, nil, nil, "Feb", "North 1", 29889},
		{nil, nil, nil, "Feb", "North 2", 50090},
		{nil, nil, nil, "Feb", "South 1", 32080},
		{nil, nil, nil, "Feb", "South 2", 45500},
	}
25 26
	prepareData := func() *File {
		f := NewFile()
xurime's avatar
xurime 已提交
27 28 29 30 31 32
		for r, row := range cellData {
			for c, value := range row {
				cell, _ := CoordinatesToCellName(c+1, r+1)
				assert.NoError(t, f.SetCellValue("Sheet1", cell, value))
			}
		}
33 34 35 36
		return f
	}

	mathCalc := map[string]string{
37 38 39 40 41 42 43 44 45 46 47 48
		"=2^3":  "8",
		"=1=1":  "TRUE",
		"=1=2":  "FALSE",
		"=1<2":  "TRUE",
		"=3<2":  "FALSE",
		"=2<=3": "TRUE",
		"=2<=1": "FALSE",
		"=2>1":  "TRUE",
		"=2>3":  "FALSE",
		"=2>=1": "TRUE",
		"=2>=3": "FALSE",
		"=1&2":  "12",
49 50 51 52 53 54
		// ABS
		"=ABS(-1)":    "1",
		"=ABS(-6.5)":  "6.5",
		"=ABS(6.5)":   "6.5",
		"=ABS(0)":     "0",
		"=ABS(2-4.5)": "2.5",
55 56
		// ACOS
		"=ACOS(-1)": "3.141592653589793",
57
		"=ACOS(0)":  "1.570796326794897",
58 59 60
		// ACOSH
		"=ACOSH(1)":   "0",
		"=ACOSH(2.5)": "1.566799236972411",
61
		"=ACOSH(5)":   "2.292431669561178",
62
		// ACOT
63
		"=_xlfn.ACOT(1)":  "0.785398163397448",
64
		"=_xlfn.ACOT(-2)": "2.677945044588987",
65
		"=_xlfn.ACOT(0)":  "1.570796326794897",
66
		// ACOTH
67 68 69
		"=_xlfn.ACOTH(-5)":  "-0.202732554054082",
		"=_xlfn.ACOTH(1.1)": "1.522261218861711",
		"=_xlfn.ACOTH(2)":   "0.549306144334055",
70 71 72 73 74 75
		// ARABIC
		`=_xlfn.ARABIC("IV")`:   "4",
		`=_xlfn.ARABIC("-IV")`:  "-4",
		`=_xlfn.ARABIC("MCXX")`: "1120",
		`=_xlfn.ARABIC("")`:     "0",
		// ASIN
76
		"=ASIN(-1)": "-1.570796326794897",
77 78 79
		"=ASIN(0)":  "0",
		// ASINH
		"=ASINH(0)":    "0",
80 81
		"=ASINH(-0.5)": "-0.481211825059604",
		"=ASINH(2)":    "1.44363547517881",
82
		// ATAN
83
		"=ATAN(-1)": "-0.785398163397448",
84
		"=ATAN(0)":  "0",
85
		"=ATAN(1)":  "0.785398163397448",
86
		// ATANH
87
		"=ATANH(-0.8)": "-1.09861228866811",
88
		"=ATANH(0)":    "0",
89
		"=ATANH(0.5)":  "0.549306144334055",
90
		// ATAN2
91 92
		"=ATAN2(1,1)":  "0.785398163397448",
		"=ATAN2(1,-1)": "-0.785398163397448",
93 94 95 96 97
		"=ATAN2(4,0)":  "0",
		// BASE
		"=BASE(12,2)":      "1100",
		"=BASE(12,2,8)":    "00001100",
		"=BASE(100000,16)": "186A0",
xurime's avatar
xurime 已提交
98 99 100 101 102 103 104 105 106
		// CEILING
		"=CEILING(22.25,0.1)":   "22.3",
		"=CEILING(22.25,0.5)":   "22.5",
		"=CEILING(22.25,1)":     "23",
		"=CEILING(22.25,10)":    "30",
		"=CEILING(22.25,20)":    "40",
		"=CEILING(-22.25,-0.1)": "-22.3",
		"=CEILING(-22.25,-1)":   "-23",
		"=CEILING(-22.25,-5)":   "-25",
xurime's avatar
xurime 已提交
107
		"=CEILING(22.25)":       "23",
xurime's avatar
xurime 已提交
108
		// _xlfn.CEILING.MATH
xurime's avatar
xurime 已提交
109 110 111 112 113 114 115 116
		"=_xlfn.CEILING.MATH(15.25,1)":      "16",
		"=_xlfn.CEILING.MATH(15.25,0.1)":    "15.3",
		"=_xlfn.CEILING.MATH(15.25,5)":      "20",
		"=_xlfn.CEILING.MATH(-15.25,1)":     "-15",
		"=_xlfn.CEILING.MATH(-15.25,1,1)":   "-15", // should be 16
		"=_xlfn.CEILING.MATH(-15.25,10)":    "-10",
		"=_xlfn.CEILING.MATH(-15.25)":       "-15",
		"=_xlfn.CEILING.MATH(-15.25,-5,-1)": "-10",
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
		// _xlfn.CEILING.PRECISE
		"=_xlfn.CEILING.PRECISE(22.25,0.1)": "22.3",
		"=_xlfn.CEILING.PRECISE(22.25,0.5)": "22.5",
		"=_xlfn.CEILING.PRECISE(22.25,1)":   "23",
		"=_xlfn.CEILING.PRECISE(22.25)":     "23",
		"=_xlfn.CEILING.PRECISE(22.25,10)":  "30",
		"=_xlfn.CEILING.PRECISE(22.25,0)":   "0",
		"=_xlfn.CEILING.PRECISE(-22.25,1)":  "-22",
		"=_xlfn.CEILING.PRECISE(-22.25,-1)": "-22",
		"=_xlfn.CEILING.PRECISE(-22.25,5)":  "-20",
		// COMBIN
		"=COMBIN(6,1)": "6",
		"=COMBIN(6,2)": "15",
		"=COMBIN(6,3)": "20",
		"=COMBIN(6,4)": "15",
		"=COMBIN(6,5)": "6",
		"=COMBIN(6,6)": "1",
xurime's avatar
xurime 已提交
134
		"=COMBIN(0,0)": "1",
135 136 137 138 139 140 141
		// _xlfn.COMBINA
		"=_xlfn.COMBINA(6,1)": "6",
		"=_xlfn.COMBINA(6,2)": "21",
		"=_xlfn.COMBINA(6,3)": "56",
		"=_xlfn.COMBINA(6,4)": "126",
		"=_xlfn.COMBINA(6,5)": "252",
		"=_xlfn.COMBINA(6,6)": "462",
xurime's avatar
xurime 已提交
142
		"=_xlfn.COMBINA(0,0)": "0",
143 144 145 146 147
		// COS
		"=COS(0.785398163)": "0.707106781467586",
		"=COS(0)":           "1",
		// COSH
		"=COSH(0)":   "1",
148 149
		"=COSH(0.5)": "1.127625965206381",
		"=COSH(-2)":  "3.762195691083632",
150
		// _xlfn.COT
151
		"=_xlfn.COT(0.785398163397448)": "0.999999999999999",
152
		// _xlfn.COTH
153
		"=_xlfn.COTH(-3.14159265358979)": "-0.99627207622075",
154
		// _xlfn.CSC
155
		"=_xlfn.CSC(-6)":              "3.578899547254406",
156 157
		"=_xlfn.CSC(1.5707963267949)": "1",
		// _xlfn.CSCH
158
		"=_xlfn.CSCH(-3.14159265358979)": "-0.086589537530047",
159
		// _xlfn.DECIMAL
xurime's avatar
xurime 已提交
160 161 162 163 164
		`=_xlfn.DECIMAL("1100",2)`:    "12",
		`=_xlfn.DECIMAL("186A0",16)`:  "100000",
		`=_xlfn.DECIMAL("31L0",32)`:   "100000",
		`=_xlfn.DECIMAL("70122",8)`:   "28754",
		`=_xlfn.DECIMAL("0x70122",8)`: "28754",
165 166 167 168 169 170 171 172 173 174 175 176
		// DEGREES
		"=DEGREES(1)":   "57.29577951308232",
		"=DEGREES(2.5)": "143.2394487827058",
		// EVEN
		"=EVEN(23)":   "24",
		"=EVEN(2.22)": "4",
		"=EVEN(0)":    "0",
		"=EVEN(-0.3)": "-2",
		"=EVEN(-11)":  "-12",
		"=EVEN(-4)":   "-4",
		// EXP
		"=EXP(100)": "2.6881171418161356E+43",
177
		"=EXP(0.1)": "1.105170918075648",
178
		"=EXP(0)":   "1",
179
		"=EXP(-5)":  "0.006737946999085",
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
		// FACT
		"=FACT(3)":  "6",
		"=FACT(6)":  "720",
		"=FACT(10)": "3.6288E+06",
		// FACTDOUBLE
		"=FACTDOUBLE(5)":  "15",
		"=FACTDOUBLE(8)":  "384",
		"=FACTDOUBLE(13)": "135135",
		// FLOOR
		"=FLOOR(26.75,0.1)":   "26.700000000000003",
		"=FLOOR(26.75,0.5)":   "26.5",
		"=FLOOR(26.75,1)":     "26",
		"=FLOOR(26.75,10)":    "20",
		"=FLOOR(26.75,20)":    "20",
		"=FLOOR(-26.75,-0.1)": "-26.700000000000003",
		"=FLOOR(-26.75,-1)":   "-26",
		"=FLOOR(-26.75,-5)":   "-25",
		// _xlfn.FLOOR.MATH
		"=_xlfn.FLOOR.MATH(58.55)":       "58",
		"=_xlfn.FLOOR.MATH(58.55,0.1)":   "58.5",
		"=_xlfn.FLOOR.MATH(58.55,5)":     "55",
		"=_xlfn.FLOOR.MATH(58.55,1,1)":   "58",
		"=_xlfn.FLOOR.MATH(-58.55,1)":    "-59",
		"=_xlfn.FLOOR.MATH(-58.55,1,-1)": "-58",
		"=_xlfn.FLOOR.MATH(-58.55,1,1)":  "-59", // should be -58
		"=_xlfn.FLOOR.MATH(-58.55,10)":   "-60",
		// _xlfn.FLOOR.PRECISE
		"=_xlfn.FLOOR.PRECISE(26.75,0.1)": "26.700000000000003",
		"=_xlfn.FLOOR.PRECISE(26.75,0.5)": "26.5",
		"=_xlfn.FLOOR.PRECISE(26.75,1)":   "26",
		"=_xlfn.FLOOR.PRECISE(26.75)":     "26",
		"=_xlfn.FLOOR.PRECISE(26.75,10)":  "20",
		"=_xlfn.FLOOR.PRECISE(26.75,0)":   "0",
		"=_xlfn.FLOOR.PRECISE(-26.75,1)":  "-27",
		"=_xlfn.FLOOR.PRECISE(-26.75,-1)": "-27",
		"=_xlfn.FLOOR.PRECISE(-26.75,-5)": "-30",
216
		// GCD
xurime's avatar
xurime 已提交
217 218 219
		"=GCD(0)":        "0",
		`=GCD("",1)`:     "1",
		"=GCD(1,0)":      "1",
220 221 222 223
		"=GCD(1,5)":      "1",
		"=GCD(15,10,25)": "5",
		"=GCD(0,8,12)":   "4",
		"=GCD(7,2)":      "1",
224 225 226 227 228 229 230 231 232 233 234 235 236 237
		// INT
		"=INT(100.9)":  "100",
		"=INT(5.22)":   "5",
		"=INT(5.99)":   "5",
		"=INT(-6.1)":   "-7",
		"=INT(-100.9)": "-101",
		// ISO.CEILING
		"=ISO.CEILING(22.25)":      "23",
		"=ISO.CEILING(22.25,1)":    "23",
		"=ISO.CEILING(22.25,0.1)":  "22.3",
		"=ISO.CEILING(22.25,10)":   "30",
		"=ISO.CEILING(-22.25,1)":   "-22",
		"=ISO.CEILING(-22.25,0.1)": "-22.200000000000003",
		"=ISO.CEILING(-22.25,5)":   "-20",
xurime's avatar
xurime 已提交
238
		"=ISO.CEILING(-22.25,0)":   "0",
239 240 241 242 243
		// LCM
		"=LCM(1,5)":      "5",
		"=LCM(15,10,25)": "150",
		"=LCM(1,8,12)":   "24",
		"=LCM(7,2)":      "14",
xurime's avatar
xurime 已提交
244 245 246
		"=LCM(7)":        "7",
		`=LCM("",1)`:     "1",
		`=LCM(0,0)`:      "0",
247 248 249
		// LN
		"=LN(1)":   "0",
		"=LN(100)": "4.605170185988092",
250
		"=LN(0.5)": "-0.693147180559945",
251 252 253 254
		// LOG
		"=LOG(64,2)":  "6",
		"=LOG(100)":   "2",
		"=LOG(4,0.5)": "-2",
255
		"=LOG(500)":   "2.698970004336019",
256 257 258 259
		// LOG10
		"=LOG10(100)":   "2",
		"=LOG10(1000)":  "3",
		"=LOG10(0.001)": "-3",
260
		"=LOG10(25)":    "1.397940008672038",
261
		// MOD
xurime's avatar
xurime 已提交
262 263 264
		"=MOD(6,4)":      "2",
		"=MOD(6,3)":      "0",
		"=MOD(6,2.5)":    "1",
265 266
		"=MOD(6,1.333)":  "0.668",
		"=MOD(-10.23,1)": "0.77",
267 268 269 270 271 272 273 274 275 276
		// MROUND
		"=MROUND(333.7,0.5)":   "333.5",
		"=MROUND(333.8,1)":     "334",
		"=MROUND(333.3,2)":     "334",
		"=MROUND(555.3,400)":   "400",
		"=MROUND(555,1000)":    "1000",
		"=MROUND(-555.7,-1)":   "-556",
		"=MROUND(-555.4,-1)":   "-555",
		"=MROUND(-1555,-1000)": "-2000",
		// MULTINOMIAL
xurime's avatar
xurime 已提交
277 278
		"=MULTINOMIAL(3,1,2,5)":    "27720",
		`=MULTINOMIAL("",3,1,2,5)`: "27720",
279 280 281 282 283 284 285 286 287 288 289 290
		// _xlfn.MUNIT
		"=_xlfn.MUNIT(4)": "", // not support currently
		// ODD
		"=ODD(22)":     "23",
		"=ODD(1.22)":   "3",
		"=ODD(1.22+4)": "7",
		"=ODD(0)":      "1",
		"=ODD(-1.3)":   "-3",
		"=ODD(-10)":    "-11",
		"=ODD(-3)":     "-3",
		// PI
		"=PI()": "3.141592653589793",
291 292 293
		// POWER
		"=POWER(4,2)": "16",
		// PRODUCT
xurime's avatar
xurime 已提交
294 295
		"=PRODUCT(3,6)":    "18",
		`=PRODUCT("",3,6)`: "18",
296 297 298 299 300
		// QUOTIENT
		"=QUOTIENT(5,2)":     "2",
		"=QUOTIENT(4.5,3.1)": "1",
		"=QUOTIENT(-10,3)":   "-3",
		// RADIANS
301
		"=RADIANS(50)":   "0.872664625997165",
302 303 304 305
		"=RADIANS(-180)": "-3.141592653589793",
		"=RADIANS(180)":  "3.141592653589793",
		"=RADIANS(360)":  "6.283185307179586",
		// ROMAN
xurime's avatar
xurime 已提交
306 307 308 309 310 311 312 313
		"=ROMAN(499,0)":   "CDXCIX",
		"=ROMAN(1999,0)":  "MCMXCIX",
		"=ROMAN(1999,1)":  "MLMVLIV",
		"=ROMAN(1999,2)":  "MXMIX",
		"=ROMAN(1999,3)":  "MVMIV",
		"=ROMAN(1999,4)":  "MIM",
		"=ROMAN(1999,-1)": "MCMXCIX",
		"=ROMAN(1999,5)":  "MIM",
314 315 316 317 318 319 320 321 322 323 324 325
		// ROUND
		"=ROUND(100.319,1)": "100.30000000000001",
		"=ROUND(5.28,1)":    "5.300000000000001",
		"=ROUND(5.9999,3)":  "6.000000000000002",
		"=ROUND(99.5,0)":    "100",
		"=ROUND(-6.3,0)":    "-6",
		"=ROUND(-100.5,0)":  "-101",
		"=ROUND(-22.45,1)":  "-22.5",
		"=ROUND(999,-1)":    "1000",
		"=ROUND(991,-1)":    "990",
		// ROUNDDOWN
		"=ROUNDDOWN(99.999,1)":   "99.9",
326
		"=ROUNDDOWN(99.999,2)":   "99.99000000000001",
327 328
		"=ROUNDDOWN(99.999,0)":   "99",
		"=ROUNDDOWN(99.999,-1)":  "90",
329
		"=ROUNDDOWN(-99.999,2)":  "-99.99000000000001",
330 331
		"=ROUNDDOWN(-99.999,-1)": "-90",
		// ROUNDUP
332
		"=ROUNDUP(11.111,1)":   "11.200000000000003",
333 334 335 336 337 338 339 340 341
		"=ROUNDUP(11.111,2)":   "11.120000000000003",
		"=ROUNDUP(11.111,0)":   "12",
		"=ROUNDUP(11.111,-1)":  "20",
		"=ROUNDUP(-11.111,2)":  "-11.120000000000003",
		"=ROUNDUP(-11.111,-1)": "-20",
		// SEC
		"=_xlfn.SEC(-3.14159265358979)": "-1",
		"=_xlfn.SEC(0)":                 "1",
		// SECH
342
		"=_xlfn.SECH(-3.14159265358979)": "0.086266738334055",
343
		"=_xlfn.SECH(0)":                 "1",
344 345 346 347 348 349
		// SIGN
		"=SIGN(9.5)":        "1",
		"=SIGN(-9.5)":       "-1",
		"=SIGN(0)":          "0",
		"=SIGN(0.00000001)": "1",
		"=SIGN(6-7)":        "-1",
350
		// SIN
351
		"=SIN(0.785398163)": "0.707106780905509",
352 353
		// SINH
		"=SINH(0)":   "0",
354
		"=SINH(0.5)": "0.521095305493747",
355
		"=SINH(-2)":  "-3.626860407847019",
356
		// SQRT
357 358
		"=SQRT(4)":  "2",
		`=SQRT("")`: "0",
359 360
		// SQRTPI
		"=SQRTPI(5)":   "3.963327297606011",
361
		"=SQRTPI(0.2)": "0.792665459521202",
362 363
		"=SQRTPI(100)": "17.72453850905516",
		"=SQRTPI(0)":   "0",
364
		// SUM
365
		"=SUM(1,2)":                           "3",
xurime's avatar
xurime 已提交
366
		`=SUM("",1,2)`:                        "3",
367 368 369 370 371 372 373 374 375 376 377 378
		"=SUM(1,2+3)":                         "6",
		"=SUM(SUM(1,2),2)":                    "5",
		"=(-2-SUM(-4+7))*5":                   "-25",
		"SUM(1,2,3,4,5,6,7)":                  "28",
		"=SUM(1,2)+SUM(1,2)":                  "6",
		"=1+SUM(SUM(1,2*3),4)":                "12",
		"=1+SUM(SUM(1,-2*3),4)":               "0",
		"=(-2-SUM(-4*(7+7)))*5":               "270",
		"=SUM(SUM(1+2/1)*2-3/2,2)":            "6.5",
		"=((3+5*2)+3)/5+(-6)/4*2+3":           "3.2",
		"=1+SUM(SUM(1,2*3),4)*-4/2+5+(4+2)*3": "2",
		"=1+SUM(SUM(1,2*3),4)*4/3+5+(4+2)*3":  "38.666666666666664",
xurime's avatar
xurime 已提交
379
		// SUMIF
380 381 382 383 384
		`=SUMIF(F1:F5, "")`:             "0",
		`=SUMIF(A1:A5, "3")`:            "3",
		`=SUMIF(F1:F5, "=36693")`:       "36693",
		`=SUMIF(F1:F5, "<100")`:         "0",
		`=SUMIF(F1:F5, "<=36693")`:      "93233",
xurime's avatar
xurime 已提交
385
		`=SUMIF(F1:F5, ">100")`:         "146554",
386 387 388
		`=SUMIF(F1:F5, ">=100")`:        "146554",
		`=SUMIF(F1:F5, ">=text")`:       "0",
		`=SUMIF(F1:F5, "*Jan",F2:F5)`:   "0",
xurime's avatar
xurime 已提交
389 390 391 392
		`=SUMIF(D3:D7,"Jan",F2:F5)`:     "112114",
		`=SUMIF(D2:D9,"Feb",F2:F9)`:     "157559",
		`=SUMIF(E2:E9,"North 1",F2:F9)`: "66582",
		`=SUMIF(E2:E9,"North*",F2:F9)`:  "138772",
xurime's avatar
xurime 已提交
393
		// SUMSQ
xurime's avatar
xurime 已提交
394 395 396
		"=SUMSQ(A1:A4)":            "14",
		"=SUMSQ(A1,B1,A2,B2,6)":    "82",
		`=SUMSQ("",A1,B1,A2,B2,6)`: "82",
397 398 399 400 401
		// TAN
		"=TAN(1.047197551)": "1.732050806782486",
		"=TAN(0)":           "0",
		// TANH
		"=TANH(0)":   "0",
402 403
		"=TANH(0.5)": "0.46211715726001",
		"=TANH(-2)":  "-0.964027580075817",
404 405 406 407 408 409 410
		// TRUNC
		"=TRUNC(99.999,1)":   "99.9",
		"=TRUNC(99.999,2)":   "99.99",
		"=TRUNC(99.999)":     "99",
		"=TRUNC(99.999,-1)":  "90",
		"=TRUNC(-99.999,2)":  "-99.99",
		"=TRUNC(-99.999,-1)": "-90",
411
		// Statistical Functions
xurime's avatar
xurime 已提交
412 413 414
		// COUNTA
		`=COUNTA()`:                       "0",
		`=COUNTA(A1:A5,B2:B5,"text",1,2)`: "8",
xurime's avatar
xurime 已提交
415 416 417
		// MEDIAN
		"=MEDIAN(A1:A5,12)": "2",
		"=MEDIAN(A1:A5)":    "1.5",
418
		// Information Functions
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
		// ISBLANK
		"=ISBLANK(A1)": "FALSE",
		"=ISBLANK(A5)": "TRUE",
		// ISERR
		"=ISERR(A1)":   "FALSE",
		"=ISERR(NA())": "FALSE",
		// ISERROR
		"=ISERROR(A1)":   "FALSE",
		"=ISERROR(NA())": "TRUE",
		// ISEVEN
		"=ISEVEN(A1)": "FALSE",
		"=ISEVEN(A2)": "TRUE",
		// ISNA
		"=ISNA(A1)":   "FALSE",
		"=ISNA(NA())": "TRUE",
		// ISNONTEXT
		"=ISNONTEXT(A1)":         "FALSE",
		"=ISNONTEXT(A5)":         "TRUE",
		`=ISNONTEXT("Excelize")`: "FALSE",
		"=ISNONTEXT(NA())":       "FALSE",
xurime's avatar
xurime 已提交
439 440 441
		// ISNUMBER
		"=ISNUMBER(A1)": "TRUE",
		"=ISNUMBER(D1)": "FALSE",
442 443 444 445 446
		// ISODD
		"=ISODD(A1)": "TRUE",
		"=ISODD(A2)": "FALSE",
		// NA
		"=NA()": "#N/A",
447
		// Logical Functions
448 449 450 451 452 453 454 455 456 457 458 459 460 461
		// AND
		"=AND(0)":               "FALSE",
		"=AND(1)":               "TRUE",
		"=AND(1,0)":             "FALSE",
		"=AND(0,1)":             "FALSE",
		"=AND(1=1)":             "TRUE",
		"=AND(1<2)":             "TRUE",
		"=AND(1>2,2<3,2>0,3>1)": "FALSE",
		"=AND(1=1),1=1":         "TRUE",
		// OR
		"=OR(1)":       "TRUE",
		"=OR(0)":       "FALSE",
		"=OR(1=2,2=2)": "TRUE",
		"=OR(1=2,2=3)": "FALSE",
462 463 464 465
		// Date and Time Functions
		// DATE
		"=DATE(2020,10,21)": "2020-10-21 00:00:00 +0000 UTC",
		"=DATE(1900,1,1)":   "1899-12-31 00:00:00 +0000 UTC",
466 467 468 469 470 471 472
		// Text Functions
		// CLEAN
		"=CLEAN(\"\u0009clean text\")": "clean text",
		"=CLEAN(0)":                    "0",
		// TRIM
		"=TRIM(\" trim text \")": "trim text",
		"=TRIM(0)":               "0",
473 474 475 476 477 478
	}
	for formula, expected := range mathCalc {
		f := prepareData()
		assert.NoError(t, f.SetCellFormula("Sheet1", "C1", formula))
		result, err := f.CalcCellValue("Sheet1", "C1")
		assert.NoError(t, err)
xurime's avatar
xurime 已提交
479
		assert.Equal(t, expected, result, formula)
480 481
	}
	mathCalcError := map[string]string{
482
		// ABS
xurime's avatar
xurime 已提交
483 484 485
		"=ABS()":    "ABS requires 1 numeric argument",
		`=ABS("X")`: "#VALUE!",
		"=ABS(~)":   `cannot convert cell "~" to coordinates: invalid cell name "~"`,
486
		// ACOS
xurime's avatar
xurime 已提交
487 488
		"=ACOS()":    "ACOS requires 1 numeric argument",
		`=ACOS("X")`: "#VALUE!",
489
		// ACOSH
xurime's avatar
xurime 已提交
490 491
		"=ACOSH()":    "ACOSH requires 1 numeric argument",
		`=ACOSH("X")`: "#VALUE!",
xurime's avatar
xurime 已提交
492
		// _xlfn.ACOT
xurime's avatar
xurime 已提交
493 494
		"=_xlfn.ACOT()":    "ACOT requires 1 numeric argument",
		`=_xlfn.ACOT("X")`: "#VALUE!",
xurime's avatar
xurime 已提交
495
		// _xlfn.ACOTH
xurime's avatar
xurime 已提交
496 497
		"=_xlfn.ACOTH()":    "ACOTH requires 1 numeric argument",
		`=_xlfn.ACOTH("X")`: "#VALUE!",
xurime's avatar
xurime 已提交
498
		// _xlfn.ARABIC
499
		"=_xlfn.ARABIC()": "ARABIC requires 1 numeric argument",
500
		// ASIN
xurime's avatar
xurime 已提交
501 502
		"=ASIN()":    "ASIN requires 1 numeric argument",
		`=ASIN("X")`: "#VALUE!",
503
		// ASINH
xurime's avatar
xurime 已提交
504 505
		"=ASINH()":    "ASINH requires 1 numeric argument",
		`=ASINH("X")`: "#VALUE!",
506
		// ATAN
xurime's avatar
xurime 已提交
507 508
		"=ATAN()":    "ATAN requires 1 numeric argument",
		`=ATAN("X")`: "#VALUE!",
509
		// ATANH
xurime's avatar
xurime 已提交
510 511
		"=ATANH()":    "ATANH requires 1 numeric argument",
		`=ATANH("X")`: "#VALUE!",
512
		// ATAN2
xurime's avatar
xurime 已提交
513 514 515
		"=ATAN2()":      "ATAN2 requires 2 numeric arguments",
		`=ATAN2("X",0)`: "#VALUE!",
		`=ATAN2(0,"X")`: "#VALUE!",
516 517 518
		// BASE
		"=BASE()":        "BASE requires at least 2 arguments",
		"=BASE(1,2,3,4)": "BASE allows at most 3 arguments",
519
		"=BASE(1,1)":     "radix must be an integer >= 2 and <= 36",
xurime's avatar
xurime 已提交
520 521 522
		`=BASE("X",2)`:   "#VALUE!",
		`=BASE(1,"X")`:   "#VALUE!",
		`=BASE(1,2,"X")`: "#VALUE!",
xurime's avatar
xurime 已提交
523 524 525 526
		// CEILING
		"=CEILING()":      "CEILING requires at least 1 argument",
		"=CEILING(1,2,3)": "CEILING allows at most 2 arguments",
		"=CEILING(1,-1)":  "negative sig to CEILING invalid",
xurime's avatar
xurime 已提交
527 528
		`=CEILING("X",0)`: "#VALUE!",
		`=CEILING(0,"X")`: "#VALUE!",
xurime's avatar
xurime 已提交
529 530 531
		// _xlfn.CEILING.MATH
		"=_xlfn.CEILING.MATH()":        "CEILING.MATH requires at least 1 argument",
		"=_xlfn.CEILING.MATH(1,2,3,4)": "CEILING.MATH allows at most 3 arguments",
xurime's avatar
xurime 已提交
532 533 534
		`=_xlfn.CEILING.MATH("X")`:     "#VALUE!",
		`=_xlfn.CEILING.MATH(1,"X")`:   "#VALUE!",
		`=_xlfn.CEILING.MATH(1,2,"X")`: "#VALUE!",
535 536 537
		// _xlfn.CEILING.PRECISE
		"=_xlfn.CEILING.PRECISE()":      "CEILING.PRECISE requires at least 1 argument",
		"=_xlfn.CEILING.PRECISE(1,2,3)": "CEILING.PRECISE allows at most 2 arguments",
xurime's avatar
xurime 已提交
538 539
		`=_xlfn.CEILING.PRECISE("X",2)`: "#VALUE!",
		`=_xlfn.CEILING.PRECISE(1,"X")`: "#VALUE!",
540
		// COMBIN
xurime's avatar
xurime 已提交
541 542 543 544
		"=COMBIN()":       "COMBIN requires 2 argument",
		"=COMBIN(-1,1)":   "COMBIN requires number >= number_chosen",
		`=COMBIN("X",1)`:  "#VALUE!",
		`=COMBIN(-1,"X")`: "#VALUE!",
545
		// _xlfn.COMBINA
xurime's avatar
xurime 已提交
546 547 548 549 550
		"=_xlfn.COMBINA()":       "COMBINA requires 2 argument",
		"=_xlfn.COMBINA(-1,1)":   "COMBINA requires number > number_chosen",
		"=_xlfn.COMBINA(-1,-1)":  "COMBIN requires number >= number_chosen",
		`=_xlfn.COMBINA("X",1)`:  "#VALUE!",
		`=_xlfn.COMBINA(-1,"X")`: "#VALUE!",
551
		// COS
xurime's avatar
xurime 已提交
552 553
		"=COS()":    "COS requires 1 numeric argument",
		`=COS("X")`: "#VALUE!",
554
		// COSH
xurime's avatar
xurime 已提交
555 556
		"=COSH()":    "COSH requires 1 numeric argument",
		`=COSH("X")`: "#VALUE!",
557
		// _xlfn.COT
xurime's avatar
xurime 已提交
558 559 560
		"=COT()":    "COT requires 1 numeric argument",
		`=COT("X")`: "#VALUE!",
		"=COT(0)":   "#DIV/0!",
561
		// _xlfn.COTH
xurime's avatar
xurime 已提交
562 563 564
		"=COTH()":    "COTH requires 1 numeric argument",
		`=COTH("X")`: "#VALUE!",
		"=COTH(0)":   "#DIV/0!",
565
		// _xlfn.CSC
xurime's avatar
xurime 已提交
566 567 568
		"=_xlfn.CSC()":    "CSC requires 1 numeric argument",
		`=_xlfn.CSC("X")`: "#VALUE!",
		"=_xlfn.CSC(0)":   "#DIV/0!",
569
		// _xlfn.CSCH
xurime's avatar
xurime 已提交
570 571 572
		"=_xlfn.CSCH()":    "CSCH requires 1 numeric argument",
		`=_xlfn.CSCH("X")`: "#VALUE!",
		"=_xlfn.CSCH(0)":   "#DIV/0!",
573 574
		// _xlfn.DECIMAL
		"=_xlfn.DECIMAL()":          "DECIMAL requires 2 numeric arguments",
xurime's avatar
xurime 已提交
575 576
		`=_xlfn.DECIMAL("X", 2)`:    "#VALUE!",
		`=_xlfn.DECIMAL(2000, "X")`: "#VALUE!",
577
		// DEGREES
xurime's avatar
xurime 已提交
578 579 580
		"=DEGREES()":    "DEGREES requires 1 numeric argument",
		`=DEGREES("X")`: "#VALUE!",
		"=DEGREES(0)":   "#DIV/0!",
581
		// EVEN
xurime's avatar
xurime 已提交
582 583
		"=EVEN()":    "EVEN requires 1 numeric argument",
		`=EVEN("X")`: "#VALUE!",
584
		// EXP
xurime's avatar
xurime 已提交
585 586
		"=EXP()":    "EXP requires 1 numeric argument",
		`=EXP("X")`: "#VALUE!",
587
		// FACT
xurime's avatar
xurime 已提交
588 589 590
		"=FACT()":    "FACT requires 1 numeric argument",
		`=FACT("X")`: "#VALUE!",
		"=FACT(-1)":  "#NUM!",
591
		// FACTDOUBLE
xurime's avatar
xurime 已提交
592 593 594
		"=FACTDOUBLE()":    "FACTDOUBLE requires 1 numeric argument",
		`=FACTDOUBLE("X")`: "#VALUE!",
		"=FACTDOUBLE(-1)":  "#NUM!",
595
		// FLOOR
xurime's avatar
xurime 已提交
596 597 598 599
		"=FLOOR()":       "FLOOR requires 2 numeric arguments",
		`=FLOOR("X",-1)`: "#VALUE!",
		`=FLOOR(1,"X")`:  "#VALUE!",
		"=FLOOR(1,-1)":   "#NUM!",
600 601 602
		// _xlfn.FLOOR.MATH
		"=_xlfn.FLOOR.MATH()":        "FLOOR.MATH requires at least 1 argument",
		"=_xlfn.FLOOR.MATH(1,2,3,4)": "FLOOR.MATH allows at most 3 arguments",
xurime's avatar
xurime 已提交
603 604 605
		`=_xlfn.FLOOR.MATH("X",2,3)`: "#VALUE!",
		`=_xlfn.FLOOR.MATH(1,"X",3)`: "#VALUE!",
		`=_xlfn.FLOOR.MATH(1,2,"X")`: "#VALUE!",
606 607 608
		// _xlfn.FLOOR.PRECISE
		"=_xlfn.FLOOR.PRECISE()":      "FLOOR.PRECISE requires at least 1 argument",
		"=_xlfn.FLOOR.PRECISE(1,2,3)": "FLOOR.PRECISE allows at most 2 arguments",
xurime's avatar
xurime 已提交
609 610
		`=_xlfn.FLOOR.PRECISE("X",2)`: "#VALUE!",
		`=_xlfn.FLOOR.PRECISE(1,"X")`: "#VALUE!",
611 612 613 614
		// GCD
		"=GCD()":     "GCD requires at least 1 argument",
		"=GCD(-1)":   "GCD only accepts positive arguments",
		"=GCD(1,-1)": "GCD only accepts positive arguments",
xurime's avatar
xurime 已提交
615
		`=GCD("X")`:  "#VALUE!",
616
		// INT
xurime's avatar
xurime 已提交
617 618
		"=INT()":    "INT requires 1 numeric argument",
		`=INT("X")`: "#VALUE!",
619 620 621
		// ISO.CEILING
		"=ISO.CEILING()":      "ISO.CEILING requires at least 1 argument",
		"=ISO.CEILING(1,2,3)": "ISO.CEILING allows at most 2 arguments",
xurime's avatar
xurime 已提交
622 623
		`=ISO.CEILING("X",2)`: "#VALUE!",
		`=ISO.CEILING(1,"X")`: "#VALUE!",
624 625 626 627
		// LCM
		"=LCM()":     "LCM requires at least 1 argument",
		"=LCM(-1)":   "LCM only accepts positive arguments",
		"=LCM(1,-1)": "LCM only accepts positive arguments",
xurime's avatar
xurime 已提交
628
		`=LCM("X")`:  "#VALUE!",
629
		// LN
xurime's avatar
xurime 已提交
630 631
		"=LN()":    "LN requires 1 numeric argument",
		`=LN("X")`: "#VALUE!",
632 633 634
		// LOG
		"=LOG()":      "LOG requires at least 1 argument",
		"=LOG(1,2,3)": "LOG allows at most 2 arguments",
xurime's avatar
xurime 已提交
635 636
		`=LOG("X",1)`: "#VALUE!",
		`=LOG(1,"X")`: "#VALUE!",
637 638 639 640
		"=LOG(0,0)":   "#NUM!",
		"=LOG(1,0)":   "#NUM!",
		"=LOG(1,1)":   "#DIV/0!",
		// LOG10
xurime's avatar
xurime 已提交
641 642
		"=LOG10()":    "LOG10 requires 1 numeric argument",
		`=LOG10("X")`: "#VALUE!",
643
		// MOD
xurime's avatar
xurime 已提交
644 645 646 647
		"=MOD()":      "MOD requires 2 numeric arguments",
		"=MOD(6,0)":   "#DIV/0!",
		`=MOD("X",0)`: "#VALUE!",
		`=MOD(6,"X")`: "#VALUE!",
648
		// MROUND
xurime's avatar
xurime 已提交
649 650 651 652 653 654 655
		"=MROUND()":      "MROUND requires 2 numeric arguments",
		"=MROUND(1,0)":   "#NUM!",
		"=MROUND(1,-1)":  "#NUM!",
		`=MROUND("X",0)`: "#VALUE!",
		`=MROUND(1,"X")`: "#VALUE!",
		// MULTINOMIAL
		`=MULTINOMIAL("X")`: "#VALUE!",
656
		// _xlfn.MUNIT
xurime's avatar
xurime 已提交
657 658
		"=_xlfn.MUNIT()":    "MUNIT requires 1 numeric argument", // not support currently
		`=_xlfn.MUNIT("X")`: "#VALUE!",                           // not support currently
659
		// ODD
xurime's avatar
xurime 已提交
660 661
		"=ODD()":    "ODD requires 1 numeric argument",
		`=ODD("X")`: "#VALUE!",
662 663
		// PI
		"=PI(1)": "PI accepts no arguments",
664
		// POWER
xurime's avatar
xurime 已提交
665 666 667 668 669 670 671
		`=POWER("X",1)`: "#VALUE!",
		`=POWER(1,"X")`: "#VALUE!",
		"=POWER(0,0)":   "#NUM!",
		"=POWER(0,-1)":  "#DIV/0!",
		"=POWER(1)":     "POWER requires 2 numeric arguments",
		// PRODUCT
		`=PRODUCT("X")`: "#VALUE!",
672
		// QUOTIENT
xurime's avatar
xurime 已提交
673 674 675 676
		`=QUOTIENT("X",1)`: "#VALUE!",
		`=QUOTIENT(1,"X")`: "#VALUE!",
		"=QUOTIENT(1,0)":   "#DIV/0!",
		"=QUOTIENT(1)":     "QUOTIENT requires 2 numeric arguments",
677
		// RADIANS
xurime's avatar
xurime 已提交
678 679
		`=RADIANS("X")`: "#VALUE!",
		"=RADIANS()":    "RADIANS requires 1 numeric argument",
680 681 682
		// RAND
		"=RAND(1)": "RAND accepts no arguments",
		// RANDBETWEEN
xurime's avatar
xurime 已提交
683 684 685 686
		`=RANDBETWEEN("X",1)`: "#VALUE!",
		`=RANDBETWEEN(1,"X")`: "#VALUE!",
		"=RANDBETWEEN()":      "RANDBETWEEN requires 2 numeric arguments",
		"=RANDBETWEEN(2,1)":   "#NUM!",
687 688 689
		// ROMAN
		"=ROMAN()":      "ROMAN requires at least 1 argument",
		"=ROMAN(1,2,3)": "ROMAN allows at most 2 arguments",
xurime's avatar
xurime 已提交
690 691
		`=ROMAN("X")`:   "#VALUE!",
		`=ROMAN("X",1)`: "#VALUE!",
692
		// ROUND
xurime's avatar
xurime 已提交
693 694 695
		"=ROUND()":      "ROUND requires 2 numeric arguments",
		`=ROUND("X",1)`: "#VALUE!",
		`=ROUND(1,"X")`: "#VALUE!",
696
		// ROUNDDOWN
xurime's avatar
xurime 已提交
697 698 699
		"=ROUNDDOWN()":      "ROUNDDOWN requires 2 numeric arguments",
		`=ROUNDDOWN("X",1)`: "#VALUE!",
		`=ROUNDDOWN(1,"X")`: "#VALUE!",
700
		// ROUNDUP
xurime's avatar
xurime 已提交
701 702 703
		"=ROUNDUP()":      "ROUNDUP requires 2 numeric arguments",
		`=ROUNDUP("X",1)`: "#VALUE!",
		`=ROUNDUP(1,"X")`: "#VALUE!",
704
		// SEC
xurime's avatar
xurime 已提交
705 706
		"=_xlfn.SEC()":    "SEC requires 1 numeric argument",
		`=_xlfn.SEC("X")`: "#VALUE!",
707
		// _xlfn.SECH
xurime's avatar
xurime 已提交
708 709
		"=_xlfn.SECH()":    "SECH requires 1 numeric argument",
		`=_xlfn.SECH("X")`: "#VALUE!",
710
		// SIGN
xurime's avatar
xurime 已提交
711 712
		"=SIGN()":    "SIGN requires 1 numeric argument",
		`=SIGN("X")`: "#VALUE!",
713
		// SIN
xurime's avatar
xurime 已提交
714 715
		"=SIN()":    "SIN requires 1 numeric argument",
		`=SIN("X")`: "#VALUE!",
716
		// SINH
xurime's avatar
xurime 已提交
717 718
		"=SINH()":    "SINH requires 1 numeric argument",
		`=SINH("X")`: "#VALUE!",
719
		// SQRT
xurime's avatar
xurime 已提交
720 721 722
		"=SQRT()":    "SQRT requires 1 numeric argument",
		`=SQRT("X")`: "#VALUE!",
		"=SQRT(-1)":  "#NUM!",
723
		// SQRTPI
xurime's avatar
xurime 已提交
724 725
		"=SQRTPI()":    "SQRTPI requires 1 numeric argument",
		`=SQRTPI("X")`: "#VALUE!",
xurime's avatar
xurime 已提交
726
		// SUM
xurime's avatar
xurime 已提交
727 728 729 730 731 732 733
		"=SUM((":    "formula not valid",
		"=SUM(-)":   "formula not valid",
		"=SUM(1+)":  "formula not valid",
		"=SUM(1-)":  "formula not valid",
		"=SUM(1*)":  "formula not valid",
		"=SUM(1/)":  "formula not valid",
		`=SUM("X")`: "#VALUE!",
xurime's avatar
xurime 已提交
734 735
		// SUMIF
		"=SUMIF()": "SUMIF requires at least 2 argument",
xurime's avatar
xurime 已提交
736 737
		// SUMSQ
		`=SUMSQ("X")`: "#VALUE!",
738
		// TAN
xurime's avatar
xurime 已提交
739 740
		"=TAN()":    "TAN requires 1 numeric argument",
		`=TAN("X")`: "#VALUE!",
741
		// TANH
xurime's avatar
xurime 已提交
742 743
		"=TANH()":    "TANH requires 1 numeric argument",
		`=TANH("X")`: "#VALUE!",
744
		// TRUNC
xurime's avatar
xurime 已提交
745 746 747
		"=TRUNC()":      "TRUNC requires at least 1 argument",
		`=TRUNC("X")`:   "#VALUE!",
		`=TRUNC(1,"X")`: "#VALUE!",
748
		// Statistical Functions
xurime's avatar
xurime 已提交
749 750
		// MEDIAN
		"=MEDIAN()": "MEDIAN requires at least 1 argument",
751
		// Information Functions
752 753 754 755 756 757 758
		// ISBLANK
		"=ISBLANK(A1,A2)": "ISBLANK requires 1 argument",
		// ISERR
		"=ISERR()": "ISERR requires 1 argument",
		// ISERROR
		"=ISERROR()": "ISERROR requires 1 argument",
		// ISEVEN
759 760
		"=ISEVEN()":       "ISEVEN requires 1 argument",
		`=ISEVEN("text")`: "#VALUE!",
761 762 763 764
		// ISNA
		"=ISNA()": "ISNA requires 1 argument",
		// ISNONTEXT
		"=ISNONTEXT()": "ISNONTEXT requires 1 argument",
xurime's avatar
xurime 已提交
765 766
		// ISNUMBER
		"=ISNUMBER()": "ISNUMBER requires 1 argument",
767
		// ISODD
768 769
		"=ISODD()":       "ISODD requires 1 argument",
		`=ISODD("text")`: "#VALUE!",
770 771
		// NA
		"=NA(1)": "NA accepts no arguments",
772
		// Logical Functions
773 774 775 776 777 778 779 780 781 782
		// AND
		`=AND("text")`: "#VALUE!",
		`=AND(A1:B1)`:  "#VALUE!",
		"=AND()":       "AND requires at least 1 argument",
		"=AND(1" + strings.Repeat(",1", 30) + ")": "AND accepts at most 30 arguments",
		// OR
		`=OR("text")`:                            "#VALUE!",
		`=OR(A1:B1)`:                             "#VALUE!",
		"=OR()":                                  "OR requires at least 1 argument",
		"=OR(1" + strings.Repeat(",1", 30) + ")": "OR accepts at most 30 arguments",
783 784 785 786 787 788
		// Date and Time Functions
		// DATE
		"=DATE()":               "DATE requires 3 number arguments",
		`=DATE("text",10,21)`:   "DATE requires 3 number arguments",
		`=DATE(2020,"text",21)`: "DATE requires 3 number arguments",
		`=DATE(2020,10,"text")`: "DATE requires 3 number arguments",
789 790 791 792 793 794 795
		// Text Functions
		// CLEAN
		"=CLEAN()":    "CLEAN requires 1 argument",
		"=CLEAN(1,2)": "CLEAN requires 1 argument",
		// TRIM
		"=TRIM()":    "TRIM requires 1 argument",
		"=TRIM(1,2)": "TRIM requires 1 argument",
796 797 798 799 800 801
	}
	for formula, expected := range mathCalcError {
		f := prepareData()
		assert.NoError(t, f.SetCellFormula("Sheet1", "C1", formula))
		result, err := f.CalcCellValue("Sheet1", "C1")
		assert.EqualError(t, err, expected)
xurime's avatar
xurime 已提交
802
		assert.Equal(t, "", result, formula)
803 804 805
	}

	referenceCalc := map[string]string{
806 807
		// MDETERM
		"=MDETERM(A1:B2)": "-3",
808 809 810
		// PRODUCT
		"=PRODUCT(Sheet1!A1:Sheet1!A1:A2,A2)": "4",
		// SUM
811
		"=A1/A3":                          "0.333333333333333",
812 813 814 815 816
		"=SUM(A1:A2)":                     "3",
		"=SUM(Sheet1!A1,A2)":              "3",
		"=(-2-SUM(-4+A2))*5":              "0",
		"=SUM(Sheet1!A1:Sheet1!A1:A2,A2)": "5",
		"=SUM(A1,A2,A3)*SUM(2,3)":         "30",
817 818
		"=1+SUM(SUM(A1+A2/A3)*(2-3),2)":   "1.333333333333334",
		"=A1/A2/SUM(A1:A2:B1)":            "0.041666666666667",
xurime's avatar
xurime 已提交
819
		"=A1/A2/SUM(A1:A2:B1)*A3":         "0.125",
820 821 822 823 824 825
	}
	for formula, expected := range referenceCalc {
		f := prepareData()
		assert.NoError(t, f.SetCellFormula("Sheet1", "C1", formula))
		result, err := f.CalcCellValue("Sheet1", "C1")
		assert.NoError(t, err)
xurime's avatar
xurime 已提交
826
		assert.Equal(t, expected, result, formula)
827 828 829
	}

	referenceCalcError := map[string]string{
830 831 832
		// MDETERM
		"=MDETERM(A1:B3)": "#VALUE!",
		// SUM
833 834 835 836 837 838 839
		"=1+SUM(SUM(A1+A2/A4)*(2-3),2)": "#DIV/0!",
	}
	for formula, expected := range referenceCalcError {
		f := prepareData()
		assert.NoError(t, f.SetCellFormula("Sheet1", "C1", formula))
		result, err := f.CalcCellValue("Sheet1", "C1")
		assert.EqualError(t, err, expected)
xurime's avatar
xurime 已提交
840
		assert.Equal(t, "", result, formula)
841 842
	}

xurime's avatar
xurime 已提交
843 844 845 846 847 848 849 850 851 852 853
	volatileFuncs := []string{
		"=RAND()",
		"=RANDBETWEEN(1,2)",
	}
	for _, formula := range volatileFuncs {
		f := prepareData()
		assert.NoError(t, f.SetCellFormula("Sheet1", "C1", formula))
		_, err := f.CalcCellValue("Sheet1", "C1")
		assert.NoError(t, err)
	}

854 855 856 857 858 859 860 861 862 863 864 865 866 867
	// Test get calculated cell value on not formula cell.
	f := prepareData()
	result, err := f.CalcCellValue("Sheet1", "A1")
	assert.NoError(t, err)
	assert.Equal(t, "", result)
	// Test get calculated cell value on not exists worksheet.
	f = prepareData()
	_, err = f.CalcCellValue("SheetN", "A1")
	assert.EqualError(t, err, "sheet SheetN is not exist")
	// Test get calculated cell value with not support formula.
	f = prepareData()
	assert.NoError(t, f.SetCellFormula("Sheet1", "A1", "=UNSUPPORT(A1)"))
	_, err = f.CalcCellValue("Sheet1", "A1")
	assert.EqualError(t, err, "not support UNSUPPORT function")
xurime's avatar
xurime 已提交
868
	assert.NoError(t, f.SaveAs(filepath.Join("test", "TestCalcCellValue.xlsx")))
J
jaby 已提交
869 870 871

}

872 873 874 875 876 877 878 879 880 881 882 883
func TestCalculate(t *testing.T) {
	err := `strconv.ParseFloat: parsing "string": invalid syntax`
	opd := NewStack()
	opd.Push(efp.Token{TValue: "string"})
	opt := efp.Token{TValue: "-", TType: efp.TokenTypeOperatorPrefix}
	assert.EqualError(t, calculate(opd, opt), err)
	opd.Push(efp.Token{TValue: "string"})
	opd.Push(efp.Token{TValue: "string"})
	opt = efp.Token{TValue: "-", TType: efp.TokenTypeOperatorInfix}
	assert.EqualError(t, calculate(opd, opt), err)
}

J
jaby 已提交
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
func TestCalcCellValueWithDefinedName(t *testing.T) {
	cellData := [][]interface{}{
		{"A1 value", "B1 value", nil},
	}
	prepareData := func() *File {
		f := NewFile()
		for r, row := range cellData {
			for c, value := range row {
				cell, _ := CoordinatesToCellName(c+1, r+1)
				assert.NoError(t, f.SetCellValue("Sheet1", cell, value))
			}
		}
		assert.NoError(t, f.SetDefinedName(&DefinedName{Name: "defined_name1", RefersTo: "Sheet1!A1", Scope: "Workbook"}))
		assert.NoError(t, f.SetDefinedName(&DefinedName{Name: "defined_name1", RefersTo: "Sheet1!B1", Scope: "Sheet1"}))

		return f
	}
	f := prepareData()
	assert.NoError(t, f.SetCellFormula("Sheet1", "C1", "=defined_name1"))
	result, err := f.CalcCellValue("Sheet1", "C1")
	assert.NoError(t, err)
	// DefinedName with scope WorkSheet takes precedence over DefinedName with scope Workbook, so we should get B1 value
	assert.Equal(t, "B1 value", result, "=defined_name1")
907
}
908

909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
func TestCalcPow(t *testing.T) {
	err := `strconv.ParseFloat: parsing "text": invalid syntax`
	assert.EqualError(t, calcPow("1", "text", nil), err)
	assert.EqualError(t, calcPow("text", "1", nil), err)
	assert.EqualError(t, calcL("1", "text", nil), err)
	assert.EqualError(t, calcL("text", "1", nil), err)
	assert.EqualError(t, calcLe("1", "text", nil), err)
	assert.EqualError(t, calcLe("text", "1", nil), err)
	assert.EqualError(t, calcG("1", "text", nil), err)
	assert.EqualError(t, calcG("text", "1", nil), err)
	assert.EqualError(t, calcGe("1", "text", nil), err)
	assert.EqualError(t, calcGe("text", "1", nil), err)
	assert.EqualError(t, calcAdd("1", "text", nil), err)
	assert.EqualError(t, calcAdd("text", "1", nil), err)
	assert.EqualError(t, calcAdd("1", "text", nil), err)
	assert.EqualError(t, calcAdd("text", "1", nil), err)
	assert.EqualError(t, calcSubtract("1", "text", nil), err)
	assert.EqualError(t, calcSubtract("text", "1", nil), err)
	assert.EqualError(t, calcMultiply("1", "text", nil), err)
	assert.EqualError(t, calcMultiply("text", "1", nil), err)
	assert.EqualError(t, calcDiv("1", "text", nil), err)
	assert.EqualError(t, calcDiv("text", "1", nil), err)
}

func TestISBLANK(t *testing.T) {
	argsList := list.New()
	argsList.PushBack(formulaArg{
		Type: ArgUnknown,
	})
	fn := formulaFuncs{}
	result, err := fn.ISBLANK(argsList)
	assert.Equal(t, result, "TRUE")
	assert.NoError(t, err)
}

func TestAND(t *testing.T) {
	argsList := list.New()
	argsList.PushBack(formulaArg{
		Type: ArgUnknown,
	})
	fn := formulaFuncs{}
	result, err := fn.AND(argsList)
	assert.Equal(t, result, "TRUE")
	assert.NoError(t, err)
}

func TestOR(t *testing.T) {
	argsList := list.New()
	argsList.PushBack(formulaArg{
		Type: ArgUnknown,
	})
	fn := formulaFuncs{}
	result, err := fn.OR(argsList)
	assert.Equal(t, result, "FALSE")
	assert.NoError(t, err)
}

966 967 968 969 970 971 972 973
func TestDet(t *testing.T) {
	assert.Equal(t, det([][]float64{
		{1, 2, 3, 4},
		{2, 3, 4, 5},
		{3, 4, 5, 6},
		{4, 5, 6, 7},
	}), float64(0))
}