Skip to content

Commit

Permalink
feat: support locale option for formattedValue & set locale numFmtMap
Browse files Browse the repository at this point in the history
  • Loading branch information
fudali113 committed Apr 21, 2023
1 parent fb6ce60 commit 3737ba4
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 8 deletions.
40 changes: 32 additions & 8 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -1350,20 +1350,44 @@ func (f *File) formattedValue(c *xlsxC, raw bool, cellType CellType) (string, er
if wb != nil && wb.WorkbookPr != nil {
date1904 = wb.WorkbookPr.Date1904
}
if ok := builtInNumFmtFunc[numFmtID]; ok != nil {
return ok(c.V, builtInNumFmt[numFmtID], date1904, cellType), err
if styleSheet.NumFmts != nil {
for _, xlsxFmt := range styleSheet.NumFmts.NumFmt {
if xlsxFmt.NumFmtID == numFmtID {
return format(c.V, xlsxFmt.FormatCode, date1904, cellType), err
}
}
}
if styleSheet.NumFmts == nil {
return c.V, err
numFmtFunc := builtInNumFmtFunc[numFmtID]
numFmtCode := f.getBuiltInNumFmtCode(numFmtID)
if numFmtFunc != nil {
return numFmtFunc(c.V, numFmtCode, date1904, cellType), err
}
for _, xlsxFmt := range styleSheet.NumFmts.NumFmt {
if xlsxFmt.NumFmtID == numFmtID {
return format(c.V, xlsxFmt.FormatCode, date1904, cellType), err
}
if numFmtCode != "" {
// default use format parse value
return format(c.V, numFmtCode, date1904, cellType), err
}
return c.V, err
}

func (f *File) getBuiltInNumFmtCode(numFmtId int) string {
locale := ""
if f.options != nil {
locale = f.options.Locale
}
numFmtCode := builtInNumFmt[numFmtId]
if locale == "" {
return numFmtCode
}
localeNumFmtMap, localeOk := langNumFmt[locale]
if localeOk {
localeNumFmtCode, ok := localeNumFmtMap[numFmtId]
if ok {
numFmtCode = localeNumFmtCode
}
}
return numFmtCode
}

// prepareCellStyle provides a function to prepare style index of cell in
// worksheet by given column index and style index.
func (f *File) prepareCellStyle(ws *xlsxWorksheet, col, row, style int) int {
Expand Down
40 changes: 40 additions & 0 deletions cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,46 @@ func TestFormattedValue(t *testing.T) {
assert.Equal(t, "0_0", fn("0_0", "", false, CellTypeNumber))
}

// Test format value with built-in number format ID = 14
styleID, err = f.NewStyle(&Style{
NumFmt: 14,
})
assert.NoError(t, err)
result, err = f.formattedValue(&xlsxC{S: styleID, V: "43528"}, false, CellTypeNumber)
assert.NoError(t, err)
assert.Equal(t, "03-04-19", result)
originalFileOptions := f.options
f.options = &Options{
Locale: "zh-cn",
}
result, err = f.formattedValue(&xlsxC{S: styleID, V: "43528"}, false, CellTypeNumber)
assert.NoError(t, err)
assert.Equal(t, "2019/3/4", result)
f.options = originalFileOptions

// Test format value with built-in number format zh-cn
styleID31, err := f.NewStyle(&Style{
NumFmt: 31,
Lang: "zh-cn",
})
assert.NoError(t, err)
styleID57, err := f.NewStyle(&Style{
NumFmt: 57,
Lang: "zh-cn",
})
assert.NoError(t, err)
originalFileOptions = f.options
f.options = &Options{
Locale: "zh-cn",
}
result, err = f.formattedValue(&xlsxC{S: styleID31, V: "43528"}, false, CellTypeNumber)
assert.NoError(t, err)
assert.Equal(t, "2019年3月4日", result)
result, err = f.formattedValue(&xlsxC{S: styleID57, V: "43528"}, false, CellTypeNumber)
assert.NoError(t, err)
assert.Equal(t, "2019年3月", result)
f.options = originalFileOptions

// Test format value with unsupported charset workbook
f.WorkBook = nil
f.Pkg.Store(defaultXMLPathWorkbook, MacintoshCyrillicCharset)
Expand Down
4 changes: 4 additions & 0 deletions excelize.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,16 @@ type charsetTranscoderFn func(charset string, input io.Reader) (rdr io.Reader, e
// temporary directory when the file size is over this value, this value
// should be less than or equal to UnzipSizeLimit, the default value is
// 16MB.
//
// Locale specifies the file parse locale, The resolution of the locale
// in the file will be affected;
type Options struct {
MaxCalcIterations uint
Password string
RawCellValue bool
UnzipSizeLimit int64
UnzipXMLSizeLimit int64
Locale string
}

// OpenFile take the name of an spreadsheet file and returns a populated
Expand Down
1 change: 1 addition & 0 deletions styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ var langNumFmt = map[string]map[int]string{
58: `[$-404]e"年"m"月"d"日"`,
},
"zh-cn": {
14: `yyyy/m/d`,
27: `yyyy"年"m"月"`,
28: `m"月"d"日"`,
29: `m"月"d"日"`,
Expand Down

0 comments on commit 3737ba4

Please sign in to comment.