Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b7b6980

Browse files
committedAug 20, 2023
This improves applying cell value with currency and accounting number format
- Update the unit test and dependencies modules
1 parent c63ae6d commit b7b6980

File tree

7 files changed

+361
-359
lines changed

7 files changed

+361
-359
lines changed
 

‎cell.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -1365,27 +1365,29 @@ func (f *File) formattedValue(c *xlsxC, raw bool, cellType CellType) (string, er
13651365
if wb != nil && wb.WorkbookPr != nil {
13661366
date1904 = wb.WorkbookPr.Date1904
13671367
}
1368+
if fmtCode, ok := styleSheet.getCustomNumFmtCode(numFmtID); ok {
1369+
return format(c.V, fmtCode, date1904, cellType, f.options), err
1370+
}
13681371
if fmtCode, ok := f.getBuiltInNumFmtCode(numFmtID); ok {
13691372
return f.applyBuiltInNumFmt(c, fmtCode, numFmtID, date1904, cellType), err
13701373
}
1371-
return f.applyNumFmt(c, styleSheet, numFmtID, date1904, cellType), err
1374+
return c.V, err
13721375
}
13731376

1374-
// applyNumFmt provides a function to returns formatted cell value with custom
1375-
// number format code.
1376-
func (f *File) applyNumFmt(c *xlsxC, styleSheet *xlsxStyleSheet, numFmtID int, date1904 bool, cellType CellType) string {
1377-
if styleSheet.NumFmts == nil {
1378-
return c.V
1377+
// getCustomNumFmtCode provides a function to returns custom number format code.
1378+
func (ss *xlsxStyleSheet) getCustomNumFmtCode(numFmtID int) (string, bool) {
1379+
if ss.NumFmts == nil {
1380+
return "", false
13791381
}
1380-
for _, xlsxFmt := range styleSheet.NumFmts.NumFmt {
1382+
for _, xlsxFmt := range ss.NumFmts.NumFmt {
13811383
if xlsxFmt.NumFmtID == numFmtID {
13821384
if xlsxFmt.FormatCode16 != "" {
1383-
return format(c.V, xlsxFmt.FormatCode16, date1904, cellType, f.options)
1385+
return xlsxFmt.FormatCode16, true
13841386
}
1385-
return format(c.V, xlsxFmt.FormatCode, date1904, cellType, f.options)
1387+
return xlsxFmt.FormatCode, true
13861388
}
13871389
}
1388-
return c.V
1390+
return "", false
13891391
}
13901392

13911393
// prepareCellStyle provides a function to prepare style index of cell in

‎cell_test.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -927,12 +927,14 @@ func TestFormattedValueNilWorkbookPr(t *testing.T) {
927927
assert.Equal(t, "43528", result)
928928
}
929929

930-
func TestApplyNumFmt(t *testing.T) {
931-
f := NewFile()
932-
assert.Equal(t, "\u4EE4\u548C\u5143年9月1日", f.applyNumFmt(&xlsxC{V: "43709"},
933-
&xlsxStyleSheet{NumFmts: &xlsxNumFmts{NumFmt: []*xlsxNumFmt{
934-
{NumFmtID: 164, FormatCode16: "[$-ja-JP-x-gannen,80]ggge\"\"m\"\"d\"\";@"},
935-
}}}, 164, false, CellTypeNumber))
930+
func TestGetCustomNumFmtCode(t *testing.T) {
931+
expected := "[$-ja-JP-x-gannen,80]ggge\"\"m\"\"d\"\";@"
932+
styleSheet := &xlsxStyleSheet{NumFmts: &xlsxNumFmts{NumFmt: []*xlsxNumFmt{
933+
{NumFmtID: 164, FormatCode16: expected},
934+
}}}
935+
numFmtCode, ok := styleSheet.getCustomNumFmtCode(164)
936+
assert.Equal(t, expected, numFmtCode)
937+
assert.True(t, ok)
936938
}
937939

938940
func TestSharedStringsError(t *testing.T) {

‎excelize_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -741,10 +741,10 @@ func TestSetCellStyleNumberFormat(t *testing.T) {
741741
idxTbl := []int{0, 1, 2, 3, 4, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49}
742742
value := []string{"37947.7500001", "-37947.7500001", "0.007", "2.1", "String"}
743743
expected := [][]string{
744-
{"37947.7500001", "37948", "37947.75", "37,948", "37,947.75", "3794775%", "3794775.00%", "3.79E+04", "37947.7500001", "37947.7500001", "11-22-03", "22-Nov-03", "22-Nov", "Nov-03", "6:00 PM", "6:00:00 PM", "18:00", "18:00:00", "11/22/03 18:00", "37,948 ", "37,948 ", "37,947.75 ", "37,947.75 ", "37947.7500001", "37947.7500001", "37947.7500001", "37947.7500001", "00:00", "910746:00:00", "00:00.0", "37947.7500001", "37947.7500001"},
745-
{"-37947.7500001", "-37948", "-37947.75", "-37,948", "-37,947.75", "-3794775%", "-3794775.00%", "-3.79E+04", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "(37,948)", "(37,948)", "(37,947.75)", "(37,947.75)", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001"},
746-
{"0.007", "0", "0.01", "0", "0.01", "1%", "0.70%", "7.00E-03", "0.007", "0.007", "12-30-99", "30-Dec-99", "30-Dec", "Dec-99", "12:10 AM", "12:10:05 AM", "00:10", "00:10:05", "12/30/99 00:10", "0 ", "0 ", "0.01 ", "0.01 ", "0.007", "0.007", "0.007", "0.007", "10:05", "0:10:05", "10:04.8", "0.007", "0.007"},
747-
{"2.1", "2", "2.10", "2", "2.10", "210%", "210.00%", "2.10E+00", "2.1", "2.1", "01-01-00", "1-Jan-00", "1-Jan", "Jan-00", "2:24 AM", "2:24:00 AM", "02:24", "02:24:00", "1/1/00 02:24", "2 ", "2 ", "2.10 ", "2.10 ", "2.1", "2.1", "2.1", "2.1", "24:00", "50:24:00", "24:00.0", "2.1", "2.1"},
744+
{"37947.7500001", "37948", "37947.75", "37,948", "37,947.75", "3794775%", "3794775.00%", "3.79E+04", "37947.7500001", "37947.7500001", "11-22-03", "22-Nov-03", "22-Nov", "Nov-03", "6:00 PM", "6:00:00 PM", "18:00", "18:00:00", "11/22/03 18:00", "37,948 ", "37,948 ", "37,947.75 ", "37,947.75 ", "37,948", "$37,948", "37,947.75", "$37,947.75", "00:00", "910746:00:00", "00:00.0", "37947.7500001", "37947.7500001"},
745+
{"-37947.7500001", "-37948", "-37947.75", "-37,948", "-37,947.75", "-3794775%", "-3794775.00%", "-3.79E+04", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "(37,948)", "(37,948)", "(37,947.75)", "(37,947.75)", "(37,948)", "$(37,948)", "(37,947.75)", "$(37,947.75)", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001", "-37947.7500001"},
746+
{"0.007", "0", "0.01", "0", "0.01", "1%", "0.70%", "7.00E-03", "0.007", "0.007", "12-30-99", "30-Dec-99", "30-Dec", "Dec-99", "12:10 AM", "12:10:05 AM", "00:10", "00:10:05", "12/30/99 00:10", "0 ", "0 ", "0.01 ", "0.01 ", "0", "$0", "0.01", "$0.01", "10:05", "0:10:05", "10:04.8", "0.007", "0.007"},
747+
{"2.1", "2", "2.10", "2", "2.10", "210%", "210.00%", "2.10E+00", "2.1", "2.1", "01-01-00", "1-Jan-00", "1-Jan", "Jan-00", "2:24 AM", "2:24:00 AM", "02:24", "02:24:00", "1/1/00 02:24", "2 ", "2 ", "2.10 ", "2.10 ", "2", "$2", "2.10", "$2.10", "24:00", "50:24:00", "24:00.0", "2.1", "2.1"},
748748
{"String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String", "String"},
749749
}
750750

‎go.mod

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ go 1.16
55
require (
66
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
77
github.com/richardlehane/mscfb v1.0.4
8+
github.com/richardlehane/msoleps v1.0.3 // indirect
89
github.com/stretchr/testify v1.8.0
910
github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca
10-
github.com/xuri/nfp v0.0.0-20230802015359-2d5eeba905e9
11+
github.com/xuri/nfp v0.0.0-20230819163627-dc951e3ffe1a
1112
golang.org/x/crypto v0.12.0
12-
golang.org/x/image v0.5.0
13+
golang.org/x/image v0.11.0
1314
golang.org/x/net v0.14.0
1415
golang.org/x/text v0.12.0
1516
)
16-
17-
require github.com/richardlehane/msoleps v1.0.3 // indirect

‎go.sum

+4-5
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@ github.com/richardlehane/msoleps v1.0.3/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTK
1313
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1414
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
1515
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
16-
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
1716
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
1817
github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca h1:uvPMDVyP7PXMMioYdyPH+0O+Ta/UO1WFfNYMO3Wz0eg=
1918
github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
20-
github.com/xuri/nfp v0.0.0-20230802015359-2d5eeba905e9 h1:jmhvNv5by7bXDzzjzBXaIWmEI4lMYfv5iJtI5Pw5/aM=
21-
github.com/xuri/nfp v0.0.0-20230802015359-2d5eeba905e9/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
19+
github.com/xuri/nfp v0.0.0-20230819163627-dc951e3ffe1a h1:Mw2VNrNNNjDtw68VsEj2+st+oCSn4Uz7vZw6TbhcV1o=
20+
github.com/xuri/nfp v0.0.0-20230819163627-dc951e3ffe1a/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
2221
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
2322
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2423
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
2524
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
2625
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
27-
golang.org/x/image v0.5.0 h1:5JMiNunQeQw++mMOz48/ISeNu3Iweh/JaZU8ZLqHRrI=
28-
golang.org/x/image v0.5.0/go.mod h1:FVC7BI/5Ym8R25iw5OLsgshdUBbT1h5jZTpA+mvAdZ4=
26+
golang.org/x/image v0.11.0 h1:ds2RoQvBvYTiJkwpSFDwCcDFNX7DqjL2WsUgTNk0Ooo=
27+
golang.org/x/image v0.11.0/go.mod h1:bglhjqbqVuEb9e9+eNR45Jfu7D+T4Qan+NhQk8Ck2P8=
2928
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
3029
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
3130
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=

0 commit comments

Comments
 (0)
Please sign in to comment.