This improves applying cell value with currency and accounting number format
- Update the unit test and dependencies modules
This commit is contained in:
parent
c63ae6d262
commit
1b63d098a7
22
cell.go
22
cell.go
|
@ -1365,27 +1365,29 @@ func (f *File) formattedValue(c *xlsxC, raw bool, cellType CellType) (string, er
|
||||||
if wb != nil && wb.WorkbookPr != nil {
|
if wb != nil && wb.WorkbookPr != nil {
|
||||||
date1904 = wb.WorkbookPr.Date1904
|
date1904 = wb.WorkbookPr.Date1904
|
||||||
}
|
}
|
||||||
|
if fmtCode, ok := styleSheet.getCustomNumFmtCode(numFmtID); ok {
|
||||||
|
return format(c.V, fmtCode, date1904, cellType, f.options), err
|
||||||
|
}
|
||||||
if fmtCode, ok := f.getBuiltInNumFmtCode(numFmtID); ok {
|
if fmtCode, ok := f.getBuiltInNumFmtCode(numFmtID); ok {
|
||||||
return f.applyBuiltInNumFmt(c, fmtCode, numFmtID, date1904, cellType), err
|
return f.applyBuiltInNumFmt(c, fmtCode, numFmtID, date1904, cellType), err
|
||||||
}
|
}
|
||||||
return f.applyNumFmt(c, styleSheet, numFmtID, date1904, cellType), err
|
return c.V, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// applyNumFmt provides a function to returns formatted cell value with custom
|
// getCustomNumFmtCode provides a function to returns custom number format code.
|
||||||
// number format code.
|
func (ss *xlsxStyleSheet) getCustomNumFmtCode(numFmtID int) (string, bool) {
|
||||||
func (f *File) applyNumFmt(c *xlsxC, styleSheet *xlsxStyleSheet, numFmtID int, date1904 bool, cellType CellType) string {
|
if ss.NumFmts == nil {
|
||||||
if styleSheet.NumFmts == nil {
|
return "", false
|
||||||
return c.V
|
|
||||||
}
|
}
|
||||||
for _, xlsxFmt := range styleSheet.NumFmts.NumFmt {
|
for _, xlsxFmt := range ss.NumFmts.NumFmt {
|
||||||
if xlsxFmt.NumFmtID == numFmtID {
|
if xlsxFmt.NumFmtID == numFmtID {
|
||||||
if xlsxFmt.FormatCode16 != "" {
|
if xlsxFmt.FormatCode16 != "" {
|
||||||
return format(c.V, xlsxFmt.FormatCode16, date1904, cellType, f.options)
|
return xlsxFmt.FormatCode16, true
|
||||||
}
|
}
|
||||||
return format(c.V, xlsxFmt.FormatCode, date1904, cellType, f.options)
|
return xlsxFmt.FormatCode, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return c.V
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepareCellStyle provides a function to prepare style index of cell in
|
// prepareCellStyle provides a function to prepare style index of cell in
|
||||||
|
|
14
cell_test.go
14
cell_test.go
|
@ -927,12 +927,14 @@ func TestFormattedValueNilWorkbookPr(t *testing.T) {
|
||||||
assert.Equal(t, "43528", result)
|
assert.Equal(t, "43528", result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestApplyNumFmt(t *testing.T) {
|
func TestGetCustomNumFmtCode(t *testing.T) {
|
||||||
f := NewFile()
|
expected := "[$-ja-JP-x-gannen,80]ggge\"年\"m\"月\"d\"日\";@"
|
||||||
assert.Equal(t, "\u4EE4\u548C\u5143年9月1日", f.applyNumFmt(&xlsxC{V: "43709"},
|
styleSheet := &xlsxStyleSheet{NumFmts: &xlsxNumFmts{NumFmt: []*xlsxNumFmt{
|
||||||
&xlsxStyleSheet{NumFmts: &xlsxNumFmts{NumFmt: []*xlsxNumFmt{
|
{NumFmtID: 164, FormatCode16: expected},
|
||||||
{NumFmtID: 164, FormatCode16: "[$-ja-JP-x-gannen,80]ggge\"年\"m\"月\"d\"日\";@"},
|
}}}
|
||||||
}}}, 164, false, CellTypeNumber))
|
numFmtCode, ok := styleSheet.getCustomNumFmtCode(164)
|
||||||
|
assert.Equal(t, expected, numFmtCode)
|
||||||
|
assert.True(t, ok)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSharedStringsError(t *testing.T) {
|
func TestSharedStringsError(t *testing.T) {
|
||||||
|
|
|
@ -741,10 +741,10 @@ func TestSetCellStyleNumberFormat(t *testing.T) {
|
||||||
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}
|
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}
|
||||||
value := []string{"37947.7500001", "-37947.7500001", "0.007", "2.1", "String"}
|
value := []string{"37947.7500001", "-37947.7500001", "0.007", "2.1", "String"}
|
||||||
expected := [][]string{
|
expected := [][]string{
|
||||||
{"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"},
|
{"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"},
|
||||||
{"-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"},
|
{"-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"},
|
||||||
{"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"},
|
{"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"},
|
||||||
{"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"},
|
{"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"},
|
||||||
{"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"},
|
{"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"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
7
go.mod
7
go.mod
|
@ -5,13 +5,12 @@ go 1.16
|
||||||
require (
|
require (
|
||||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
|
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
|
||||||
github.com/richardlehane/mscfb v1.0.4
|
github.com/richardlehane/mscfb v1.0.4
|
||||||
|
github.com/richardlehane/msoleps v1.0.3 // indirect
|
||||||
github.com/stretchr/testify v1.8.0
|
github.com/stretchr/testify v1.8.0
|
||||||
github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca
|
github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca
|
||||||
github.com/xuri/nfp v0.0.0-20230802015359-2d5eeba905e9
|
github.com/xuri/nfp v0.0.0-20230819163627-dc951e3ffe1a
|
||||||
golang.org/x/crypto v0.12.0
|
golang.org/x/crypto v0.12.0
|
||||||
golang.org/x/image v0.5.0
|
golang.org/x/image v0.11.0
|
||||||
golang.org/x/net v0.14.0
|
golang.org/x/net v0.14.0
|
||||||
golang.org/x/text v0.12.0
|
golang.org/x/text v0.12.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require github.com/richardlehane/msoleps v1.0.3 // indirect
|
|
||||||
|
|
8
go.sum
8
go.sum
|
@ -17,15 +17,15 @@ github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PK
|
||||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca h1:uvPMDVyP7PXMMioYdyPH+0O+Ta/UO1WFfNYMO3Wz0eg=
|
github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca h1:uvPMDVyP7PXMMioYdyPH+0O+Ta/UO1WFfNYMO3Wz0eg=
|
||||||
github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
|
github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
|
||||||
github.com/xuri/nfp v0.0.0-20230802015359-2d5eeba905e9 h1:jmhvNv5by7bXDzzjzBXaIWmEI4lMYfv5iJtI5Pw5/aM=
|
github.com/xuri/nfp v0.0.0-20230819163627-dc951e3ffe1a h1:Mw2VNrNNNjDtw68VsEj2+st+oCSn4Uz7vZw6TbhcV1o=
|
||||||
github.com/xuri/nfp v0.0.0-20230802015359-2d5eeba905e9/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
|
github.com/xuri/nfp v0.0.0-20230819163627-dc951e3ffe1a/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
|
||||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
|
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
|
||||||
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
|
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
|
||||||
golang.org/x/image v0.5.0 h1:5JMiNunQeQw++mMOz48/ISeNu3Iweh/JaZU8ZLqHRrI=
|
golang.org/x/image v0.11.0 h1:ds2RoQvBvYTiJkwpSFDwCcDFNX7DqjL2WsUgTNk0Ooo=
|
||||||
golang.org/x/image v0.5.0/go.mod h1:FVC7BI/5Ym8R25iw5OLsgshdUBbT1h5jZTpA+mvAdZ4=
|
golang.org/x/image v0.11.0/go.mod h1:bglhjqbqVuEb9e9+eNR45Jfu7D+T4Qan+NhQk8Ck2P8=
|
||||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
|
|
@ -694,6 +694,7 @@ var (
|
||||||
nfp.TokenTypeHashPlaceHolder,
|
nfp.TokenTypeHashPlaceHolder,
|
||||||
nfp.TokenTypeLiteral,
|
nfp.TokenTypeLiteral,
|
||||||
nfp.TokenTypePercent,
|
nfp.TokenTypePercent,
|
||||||
|
nfp.TokenTypeRepeatsChar,
|
||||||
nfp.TokenTypeSwitchArgument,
|
nfp.TokenTypeSwitchArgument,
|
||||||
nfp.TokenTypeTextPlaceHolder,
|
nfp.TokenTypeTextPlaceHolder,
|
||||||
nfp.TokenTypeThousandsSeparator,
|
nfp.TokenTypeThousandsSeparator,
|
||||||
|
@ -923,8 +924,8 @@ var (
|
||||||
"11": {tags: []string{"ja"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese, weekdayNames: weekdayNamesJapanese, weekdayNamesAbbr: weekdayNamesJapaneseAbbr},
|
"11": {tags: []string{"ja"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese, weekdayNames: weekdayNamesJapanese, weekdayNamesAbbr: weekdayNamesJapaneseAbbr},
|
||||||
"411": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese, weekdayNames: weekdayNamesJapanese, weekdayNamesAbbr: weekdayNamesJapaneseAbbr},
|
"411": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese, weekdayNames: weekdayNamesJapanese, weekdayNamesAbbr: weekdayNamesJapaneseAbbr},
|
||||||
"800411": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese, weekdayNames: weekdayNamesJapanese, weekdayNamesAbbr: weekdayNamesJapaneseAbbr},
|
"800411": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese, weekdayNames: weekdayNamesJapanese, weekdayNamesAbbr: weekdayNamesJapaneseAbbr},
|
||||||
"JP-X-GANNEN": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese, weekdayNames: weekdayNamesJapanese, weekdayNamesAbbr: weekdayNamesJapaneseAbbr},
|
"JA-JP-X-GANNEN": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese, weekdayNames: weekdayNamesJapanese, weekdayNamesAbbr: weekdayNamesJapaneseAbbr},
|
||||||
"JP-X-GANNEN,80": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese, weekdayNames: weekdayNamesJapanese, weekdayNamesAbbr: weekdayNamesJapaneseAbbr, useGannen: true},
|
"JA-JP-X-GANNEN,80": {tags: []string{"ja-JP"}, localMonth: localMonthsNameChinese3, apFmt: apFmtJapanese, weekdayNames: weekdayNamesJapanese, weekdayNamesAbbr: weekdayNamesJapaneseAbbr, useGannen: true},
|
||||||
"4B": {tags: []string{"kn"}, localMonth: localMonthsNameKannada, apFmt: apFmtKannada, weekdayNames: weekdayNamesKannada, weekdayNamesAbbr: weekdayNamesKannadaAbbr},
|
"4B": {tags: []string{"kn"}, localMonth: localMonthsNameKannada, apFmt: apFmtKannada, weekdayNames: weekdayNamesKannada, weekdayNamesAbbr: weekdayNamesKannadaAbbr},
|
||||||
"44B": {tags: []string{"kn-IN"}, localMonth: localMonthsNameKannada, apFmt: apFmtKannada, weekdayNames: weekdayNamesKannada, weekdayNamesAbbr: weekdayNamesKannadaAbbr},
|
"44B": {tags: []string{"kn-IN"}, localMonth: localMonthsNameKannada, apFmt: apFmtKannada, weekdayNames: weekdayNamesKannada, weekdayNamesAbbr: weekdayNamesKannadaAbbr},
|
||||||
"471": {tags: []string{"kr-Latn-NG"}, localMonth: localMonthsNameEnglish, apFmt: nfp.AmPm[0], weekdayNames: weekdayNamesEnglish, weekdayNamesAbbr: weekdayNamesEnglishAbbr},
|
"471": {tags: []string{"kr-Latn-NG"}, localMonth: localMonthsNameEnglish, apFmt: nfp.AmPm[0], weekdayNames: weekdayNamesEnglish, weekdayNamesAbbr: weekdayNamesEnglishAbbr},
|
||||||
|
@ -4939,10 +4940,8 @@ func (nf *numberFormat) dateTimeHandler() string {
|
||||||
if changeNumFmtCode, err := nf.currencyLanguageHandler(token); err != nil || changeNumFmtCode {
|
if changeNumFmtCode, err := nf.currencyLanguageHandler(token); err != nil || changeNumFmtCode {
|
||||||
return nf.value
|
return nf.value
|
||||||
}
|
}
|
||||||
if !strings.EqualFold(nf.localCode, "JP-X-GANNEN") && !strings.EqualFold(nf.localCode, "JP-X-GANNEN,80") {
|
|
||||||
nf.result += nf.currencyString
|
nf.result += nf.currencyString
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if token.TType == nfp.TokenTypeDateTimes {
|
if token.TType == nfp.TokenTypeDateTimes {
|
||||||
nf.dateTimesHandler(i, token)
|
nf.dateTimesHandler(i, token)
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
func TestNumFmt(t *testing.T) {
|
func TestNumFmt(t *testing.T) {
|
||||||
for _, item := range [][]string{
|
for _, item := range [][]string{
|
||||||
{"123", "general", "123"},
|
{"123", "general", "123"},
|
||||||
|
{"-123", ";general", "-123"},
|
||||||
{"43528", "y", "19"},
|
{"43528", "y", "19"},
|
||||||
{"43528", "Y", "19"},
|
{"43528", "Y", "19"},
|
||||||
{"43528", "yy", "19"},
|
{"43528", "yy", "19"},
|
||||||
|
|
Loading…
Reference in New Issue