Skip to content

Commit 7c221cf

Browse files
committed
Ref qax-os#660, support placeholder, padding and rounds numbers by specified number format code
- Remove built-in number formats functions - Update unit tests - Upgrade dependencies package
1 parent 65fc25e commit 7c221cf

9 files changed

+321
-290
lines changed

cell.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1365,8 +1365,8 @@ 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 ok := builtInNumFmtFunc[numFmtID]; ok != nil {
1369-
return ok(c.V, builtInNumFmt[numFmtID], date1904, cellType), err
1368+
if fmtCode, ok := builtInNumFmt[numFmtID]; ok {
1369+
return format(c.V, fmtCode, date1904, cellType), err
13701370
}
13711371
if styleSheet.NumFmts == nil {
13721372
return c.V, err

cell_test.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -873,9 +873,7 @@ func TestFormattedValue(t *testing.T) {
873873
assert.NoError(t, err)
874874
assert.Equal(t, "311", result)
875875

876-
for _, fn := range builtInNumFmtFunc {
877-
assert.Equal(t, "0_0", fn("0_0", "", false, CellTypeNumber))
878-
}
876+
assert.Equal(t, "0_0", format("0_0", "", false, CellTypeNumber))
879877

880878
// Test format value with unsupported charset workbook
881879
f.WorkBook = nil
@@ -889,9 +887,7 @@ func TestFormattedValue(t *testing.T) {
889887
_, err = f.formattedValue(&xlsxC{S: 1, V: "43528"}, false, CellTypeNumber)
890888
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
891889

892-
for _, fn := range builtInNumFmtFunc {
893-
assert.Equal(t, fn("text", "0", false, CellTypeNumber), "text")
894-
}
890+
assert.Equal(t, "text", format("text", "0", false, CellTypeNumber))
895891
}
896892

897893
func TestFormattedValueNilXfs(t *testing.T) {

excelize_test.go

+18-17
Original file line numberDiff line numberDiff line change
@@ -747,33 +747,33 @@ func TestSetCellStyleNumberFormat(t *testing.T) {
747747

748748
// Test only set fill and number format for a cell
749749
col := []string{"L", "M", "N", "O", "P"}
750-
data := []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}
750+
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}
751751
value := []string{"37947.7500001", "-37947.7500001", "0.007", "2.1", "String"}
752752
expected := [][]string{
753-
{"37947.7500001", "37948", "37947.75", "37,948", "37947.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", "37947.7500001", "3.79E+04", "37947.7500001"},
754-
{"-37947.7500001", "-37948", "-37947.75", "-37,948", "-37947.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", "-3.79E+04", "-37947.7500001"},
755-
{"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", "0:10 am", "0:10:04 am", "00:10", "00:10:04", "12/30/99 00:10", "0 ", "0 ", "0.01 ", "0.01 ", "0.007", "0.007", "0.007", "0.007", "10:04", "0:10:04", "0.007", "7.00E-03", "0.007"},
756-
{"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", "2.1", "2.10E+00", "2.1"},
753+
{"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", "0000.0", "37947.7500001", "37947.7500001"},
754+
{"-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"},
755+
{"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", "0:10 am", "0:10:04 am", "00:10", "00:10:04", "12/30/99 00:10", "0 ", "0 ", "0.01 ", "0.01 ", "0.007", "0.007", "0.007", "0.007", "10:04", "0:10:04", "1004.0", "0.007", "0.007"},
756+
{"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", "2400.0", "2.1", "2.1"},
757757
{"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"},
758758
}
759759

760-
for i, v := range value {
761-
for k, d := range data {
762-
c := col[i] + strconv.Itoa(k+1)
760+
for c, v := range value {
761+
for r, idx := range idxTbl {
762+
cell := col[c] + strconv.Itoa(r+1)
763763
var val float64
764764
val, err = strconv.ParseFloat(v, 64)
765765
if err != nil {
766-
assert.NoError(t, f.SetCellValue("Sheet2", c, v))
766+
assert.NoError(t, f.SetCellValue("Sheet2", cell, v))
767767
} else {
768-
assert.NoError(t, f.SetCellValue("Sheet2", c, val))
768+
assert.NoError(t, f.SetCellValue("Sheet2", cell, val))
769769
}
770-
style, err := f.NewStyle(&Style{Fill: Fill{Type: "gradient", Color: []string{"FFFFFF", "E0EBF5"}, Shading: 5}, NumFmt: d})
770+
style, err := f.NewStyle(&Style{Fill: Fill{Type: "gradient", Color: []string{"FFFFFF", "E0EBF5"}, Shading: 5}, NumFmt: idx})
771771
if !assert.NoError(t, err) {
772772
t.FailNow()
773773
}
774-
assert.NoError(t, f.SetCellStyle("Sheet2", c, c, style))
775-
cellValue, err := f.GetCellValue("Sheet2", c)
776-
assert.Equal(t, expected[i][k], cellValue, fmt.Sprintf("Sheet2!%s value: %s, number format: %d", c, value[i], k))
774+
assert.NoError(t, f.SetCellStyle("Sheet2", cell, cell, style))
775+
cellValue, err := f.GetCellValue("Sheet2", cell)
776+
assert.Equal(t, expected[c][r], cellValue, fmt.Sprintf("Sheet2!%s value: %s, number format: %s c: %d r: %d", cell, value[c], builtInNumFmt[idx], c, r))
777777
assert.NoError(t, err)
778778
}
779779
}
@@ -997,7 +997,7 @@ func TestConditionalFormat(t *testing.T) {
997997
f := NewFile()
998998
sheet1 := f.GetSheetName(0)
999999

1000-
fillCells(f, sheet1, 10, 15)
1000+
assert.NoError(t, fillCells(f, sheet1, 10, 15))
10011001

10021002
var format1, format2, format3, format4 int
10031003
var err error
@@ -1612,15 +1612,16 @@ func prepareTestBook4() (*File, error) {
16121612
return f, nil
16131613
}
16141614

1615-
func fillCells(f *File, sheet string, colCount, rowCount int) {
1615+
func fillCells(f *File, sheet string, colCount, rowCount int) error {
16161616
for col := 1; col <= colCount; col++ {
16171617
for row := 1; row <= rowCount; row++ {
16181618
cell, _ := CoordinatesToCellName(col, row)
16191619
if err := f.SetCellStr(sheet, cell, cell); err != nil {
1620-
fmt.Println(err)
1620+
return err
16211621
}
16221622
}
16231623
}
1624+
return nil
16241625
}
16251626

16261627
func BenchmarkOpenFile(b *testing.B) {

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ require (
66
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
77
github.com/richardlehane/mscfb v1.0.4
88
github.com/stretchr/testify v1.8.0
9-
github.com/xuri/efp v0.0.0-20220603152613-6918739fd470
10-
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22
9+
github.com/xuri/efp v0.0.0-20230422071738-01f4e37c47e9
10+
github.com/xuri/nfp v0.0.0-20230428090735-b50b0f0358f4
1111
golang.org/x/crypto v0.8.0
1212
golang.org/x/image v0.5.0
1313
golang.org/x/net v0.9.0

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
1515
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1616
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
1717
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
18-
github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 h1:6932x8ltq1w4utjmfMPVj09jdMlkY0aiA6+Skbtl3/c=
19-
github.com/xuri/efp v0.0.0-20220603152613-6918739fd470/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
20-
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 h1:OAmKAfT06//esDdpi/DZ8Qsdt4+M5+ltca05dA5bG2M=
21-
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
18+
github.com/xuri/efp v0.0.0-20230422071738-01f4e37c47e9 h1:ge5g8vsTQclA5lXDi+PuiAFw5GMIlMHOB/5e1hsf96E=
19+
github.com/xuri/efp v0.0.0-20230422071738-01f4e37c47e9/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
20+
github.com/xuri/nfp v0.0.0-20230428090735-b50b0f0358f4 h1:YoU/1S7L25dvNepEir3Fg2aU9iGmDyE4gWKoEswWXts=
21+
github.com/xuri/nfp v0.0.0-20230428090735-b50b0f0358f4/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
2222
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
2323
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2424
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=

0 commit comments

Comments
 (0)