From 19a0cf3cec71fc49851d29fb420674b027f50d93 Mon Sep 17 00:00:00 2001 From: xuri Date: Sun, 15 May 2022 15:38:40 +0800 Subject: [PATCH] This closed #1163, fix set cell value with column and row style inherit issue --- cell.go | 10 +++++----- cell_test.go | 15 +++++++++++++++ styles.go | 4 ++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/cell.go b/cell.go index 80c03efe..1e130dcb 100644 --- a/cell.go +++ b/cell.go @@ -1145,6 +1145,11 @@ 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 { @@ -1152,11 +1157,6 @@ func (f *File) prepareCellStyle(ws *xlsxWorksheet, col, row, style int) int { } } } - if row <= len(ws.SheetData.Row) { - if styleID := ws.SheetData.Row[row-1].S; styleID != 0 { - return styleID - } - } return style } diff --git a/cell_test.go b/cell_test.go index 8ed8e1f6..da251cdf 100644 --- a/cell_test.go +++ b/cell_test.go @@ -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) { diff --git a/styles.go b/styles.go index 6ef7dcbe..b7b1525d 100644 --- a/styles.go +++ b/styles.go @@ -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) } @@ -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) }