Skip to content

Commit

Permalink
This closed qax-os#1163, fix set cell value with column and row style…
Browse files Browse the repository at this point in the history
… inherit issue
  • Loading branch information
xuri committed May 15, 2022
1 parent c2311ce commit 19a0cf3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
10 changes: 5 additions & 5 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -1145,18 +1145,18 @@ func (f *File) prepareCellStyle(ws *xlsxWorksheet, col, row, style int) int {
if style != 0 {
return style
}
if row <= len(ws.SheetData.Row) {
if styleID := ws.SheetData.Row[row-1].S; styleID != 0 {
return styleID
}
}
if ws.Cols != nil {
for _, c := range ws.Cols.Col {
if c.Min <= col && col <= c.Max && c.Style != 0 {
return c.Style
}
}
}
if row <= len(ws.SheetData.Row) {
if styleID := ws.SheetData.Row[row-1].S; styleID != 0 {
return styleID
}
}
return style
}

Expand Down
15 changes: 15 additions & 0 deletions cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ func TestSetCellValue(t *testing.T) {
f := NewFile()
assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Now().UTC()), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Duration(1e13)), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
// Test set cell value with column and row style inherit
style1, err := f.NewStyle(&Style{NumFmt: 2})
assert.NoError(t, err)
style2, err := f.NewStyle(&Style{NumFmt: 9})
assert.NoError(t, err)
assert.NoError(t, f.SetColStyle("Sheet1", "B", style1))
assert.NoError(t, f.SetRowStyle("Sheet1", 1, 1, style2))
assert.NoError(t, f.SetCellValue("Sheet1", "B1", 0.5))
assert.NoError(t, f.SetCellValue("Sheet1", "B2", 0.5))
B1, err := f.GetCellValue("Sheet1", "B1")
assert.NoError(t, err)
assert.Equal(t, "50%", B1)
B2, err := f.GetCellValue("Sheet1", "B2")
assert.NoError(t, err)
assert.Equal(t, "0.50", B2)
}

func TestSetCellValues(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ func formatToC(v, format string, date1904 bool) string {
if err != nil {
return v
}
f = f * 100
f *= 100
return fmt.Sprintf("%.f%%", f)
}

Expand All @@ -912,7 +912,7 @@ func formatToD(v, format string, date1904 bool) string {
if err != nil {
return v
}
f = f * 100
f *= 100
return fmt.Sprintf("%.2f%%", f)
}

Expand Down

0 comments on commit 19a0cf3

Please sign in to comment.