Skip to content

Commit f4e3951

Browse files
committed
This closes #1770, fix incorrect multiple conditional formats rules priorities
- Rename variable name hCell to topLeftCell, and rename vCell to bottomRightCell - Update the unit tests
1 parent bb8e5da commit f4e3951

File tree

5 files changed

+71
-28
lines changed

5 files changed

+71
-28
lines changed

merge.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,24 @@ func (mc *xlsxMergeCell) Rect() ([]int, error) {
4949
// | |
5050
// |A8(x3,y4) C8(x4,y4)|
5151
// +------------------------+
52-
func (f *File) MergeCell(sheet, hCell, vCell string) error {
53-
rect, err := rangeRefToCoordinates(hCell + ":" + vCell)
52+
func (f *File) MergeCell(sheet, topLeftCell, bottomRightCell string) error {
53+
rect, err := rangeRefToCoordinates(topLeftCell + ":" + bottomRightCell)
5454
if err != nil {
5555
return err
5656
}
5757
// Correct the range reference, such correct C1:B3 to B1:C3.
5858
_ = sortCoordinates(rect)
5959

60-
hCell, _ = CoordinatesToCellName(rect[0], rect[1])
61-
vCell, _ = CoordinatesToCellName(rect[2], rect[3])
60+
topLeftCell, _ = CoordinatesToCellName(rect[0], rect[1])
61+
bottomRightCell, _ = CoordinatesToCellName(rect[2], rect[3])
6262

6363
ws, err := f.workSheetReader(sheet)
6464
if err != nil {
6565
return err
6666
}
6767
ws.mu.Lock()
6868
defer ws.mu.Unlock()
69-
ref := hCell + ":" + vCell
69+
ref := topLeftCell + ":" + bottomRightCell
7070
if ws.MergeCells != nil {
7171
ws.MergeCells.Cells = append(ws.MergeCells.Cells, &xlsxMergeCell{Ref: ref, rect: rect})
7272
} else {
@@ -82,14 +82,14 @@ func (f *File) MergeCell(sheet, hCell, vCell string) error {
8282
// err := f.UnmergeCell("Sheet1", "D3", "E9")
8383
//
8484
// Attention: overlapped range will also be unmerged.
85-
func (f *File) UnmergeCell(sheet, hCell, vCell string) error {
85+
func (f *File) UnmergeCell(sheet, topLeftCell, bottomRightCell string) error {
8686
ws, err := f.workSheetReader(sheet)
8787
if err != nil {
8888
return err
8989
}
9090
ws.mu.Lock()
9191
defer ws.mu.Unlock()
92-
rect1, err := rangeRefToCoordinates(hCell + ":" + vCell)
92+
rect1, err := rangeRefToCoordinates(topLeftCell + ":" + bottomRightCell)
9393
if err != nil {
9494
return err
9595
}
@@ -265,9 +265,9 @@ func mergeCell(cell1, cell2 *xlsxMergeCell) *xlsxMergeCell {
265265
if rect1[3] < rect2[3] {
266266
rect1[3], rect2[3] = rect2[3], rect1[3]
267267
}
268-
hCell, _ := CoordinatesToCellName(rect1[0], rect1[1])
269-
vCell, _ := CoordinatesToCellName(rect1[2], rect1[3])
270-
return &xlsxMergeCell{rect: rect1, Ref: hCell + ":" + vCell}
268+
topLeftCell, _ := CoordinatesToCellName(rect1[0], rect1[1])
269+
bottomRightCell, _ := CoordinatesToCellName(rect1[2], rect1[3])
270+
return &xlsxMergeCell{rect: rect1, Ref: topLeftCell + ":" + bottomRightCell}
271271
}
272272

273273
// MergeCell define a merged cell data.

pivotTable.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ func (f *File) addPivotCache(opts *PivotTableOptions) error {
274274
}
275275
// data range has been checked
276276
order, _ := f.getTableFieldsOrder(opts)
277-
hCell, _ := CoordinatesToCellName(coordinates[0], coordinates[1])
278-
vCell, _ := CoordinatesToCellName(coordinates[2], coordinates[3])
277+
topLeftCell, _ := CoordinatesToCellName(coordinates[0], coordinates[1])
278+
bottomRightCell, _ := CoordinatesToCellName(coordinates[2], coordinates[3])
279279
pc := xlsxPivotCacheDefinition{
280280
SaveData: false,
281281
RefreshOnLoad: true,
@@ -285,7 +285,7 @@ func (f *File) addPivotCache(opts *PivotTableOptions) error {
285285
CacheSource: &xlsxCacheSource{
286286
Type: "worksheet",
287287
WorksheetSource: &xlsxWorksheetSource{
288-
Ref: hCell + ":" + vCell,
288+
Ref: topLeftCell + ":" + bottomRightCell,
289289
Sheet: dataSheet,
290290
},
291291
},
@@ -315,8 +315,8 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, opts *PivotTableOptions)
315315
return newPivotTableRangeError(err.Error())
316316
}
317317

318-
hCell, _ := CoordinatesToCellName(coordinates[0], coordinates[1])
319-
vCell, _ := CoordinatesToCellName(coordinates[2], coordinates[3])
318+
topLeftCell, _ := CoordinatesToCellName(coordinates[0], coordinates[1])
319+
bottomRightCell, _ := CoordinatesToCellName(coordinates[2], coordinates[3])
320320

321321
pivotTableStyle := func() string {
322322
if opts.PivotTableStyleName == "" {
@@ -340,7 +340,7 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, opts *PivotTableOptions)
340340
ShowError: &opts.ShowError,
341341
DataCaption: "Values",
342342
Location: &xlsxLocation{
343-
Ref: hCell + ":" + vCell,
343+
Ref: topLeftCell + ":" + bottomRightCell,
344344
FirstDataCol: 1,
345345
FirstDataRow: 1,
346346
FirstHeaderRow: 1,

stream.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,16 +484,16 @@ func (sw *StreamWriter) SetPanes(panes *Panes) error {
484484
// MergeCell provides a function to merge cells by a given range reference for
485485
// the StreamWriter. Don't create a merged cell that overlaps with another
486486
// existing merged cell.
487-
func (sw *StreamWriter) MergeCell(hCell, vCell string) error {
488-
_, err := cellRefsToCoordinates(hCell, vCell)
487+
func (sw *StreamWriter) MergeCell(topLeftCell, bottomRightCell string) error {
488+
_, err := cellRefsToCoordinates(topLeftCell, bottomRightCell)
489489
if err != nil {
490490
return err
491491
}
492492
sw.mergeCellsCount++
493493
_, _ = sw.mergeCells.WriteString(`<mergeCell ref="`)
494-
_, _ = sw.mergeCells.WriteString(hCell)
494+
_, _ = sw.mergeCells.WriteString(topLeftCell)
495495
_, _ = sw.mergeCells.WriteString(`:`)
496-
_, _ = sw.mergeCells.WriteString(vCell)
496+
_, _ = sw.mergeCells.WriteString(bottomRightCell)
497497
_, _ = sw.mergeCells.WriteString(`"/>`)
498498
return nil
499499
}

styles.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,13 +2262,13 @@ func (f *File) GetCellStyle(sheet, cell string) (int, error) {
22622262
// fmt.Println(err)
22632263
// }
22642264
// err = f.SetCellStyle("Sheet1", "H9", "H9", style)
2265-
func (f *File) SetCellStyle(sheet, hCell, vCell string, styleID int) error {
2266-
hCol, hRow, err := CellNameToCoordinates(hCell)
2265+
func (f *File) SetCellStyle(sheet, topLeftCell, bottomRightCell string, styleID int) error {
2266+
hCol, hRow, err := CellNameToCoordinates(topLeftCell)
22672267
if err != nil {
22682268
return err
22692269
}
22702270

2271-
vCol, vRow, err := CellNameToCoordinates(vCell)
2271+
vCol, vRow, err := CellNameToCoordinates(bottomRightCell)
22722272
if err != nil {
22732273
return err
22742274
}
@@ -2759,19 +2759,20 @@ func (f *File) SetConditionalFormat(sheet, rangeRef string, opts []ConditionalFo
27592759
"iconSet",
27602760
}
27612761
)
2762-
for p, v := range opts {
2762+
for i, opt := range opts {
27632763
var vt, ct string
27642764
var ok bool
27652765
// "type" is a required parameter, check for valid validation types.
2766-
vt, ok = validType[v.Type]
2766+
vt, ok = validType[opt.Type]
27672767
if ok {
27682768
// Check for valid criteria types.
2769-
ct, ok = criteriaType[v.Criteria]
2769+
ct, ok = criteriaType[opt.Criteria]
27702770
if ok || inStrSlice(noCriteriaTypes, vt, true) != -1 {
27712771
drawFunc, ok := drawContFmtFunc[vt]
27722772
if ok {
2773-
rule, x14rule := drawFunc(p, ct, strings.Split(rangeRef, ":")[0],
2774-
fmt.Sprintf("{00000000-0000-0000-%04X-%012X}", f.getSheetID(sheet), rules+p), &v)
2773+
priority := rules + i
2774+
rule, x14rule := drawFunc(priority, ct, strings.Split(rangeRef, ":")[0],
2775+
fmt.Sprintf("{00000000-0000-0000-%04X-%012X}", f.getSheetID(sheet), priority), &opt)
27752776
if rule == nil {
27762777
return ErrParameterInvalid
27772778
}

styles_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,48 @@ func TestSetConditionalFormat(t *testing.T) {
193193
assert.Equal(t, ErrParameterInvalid, f.SetConditionalFormat("Sheet1", "A1:A2", []ConditionalFormatOptions{{Type: "icon_set", IconStyle: "unknown"}}))
194194
// Test unsupported conditional formatting rule types
195195
assert.Equal(t, ErrParameterInvalid, f.SetConditionalFormat("Sheet1", "A1", []ConditionalFormatOptions{{Type: "unsupported"}}))
196+
197+
t.Run("multi_conditional_formatting_rules_priority", func(t *testing.T) {
198+
f := NewFile()
199+
var condFmts []ConditionalFormatOptions
200+
for _, color := range []string{
201+
"#264B96", // Blue
202+
"#F9A73E", // Yellow
203+
"#006F3C", // Green
204+
} {
205+
condFmts = append(condFmts, ConditionalFormatOptions{
206+
Type: "data_bar",
207+
Criteria: "=",
208+
MinType: "num",
209+
MaxType: "num",
210+
MinValue: "0",
211+
MaxValue: "5",
212+
BarColor: color,
213+
BarSolid: true,
214+
})
215+
}
216+
assert.NoError(t, f.SetConditionalFormat("Sheet1", "A1:A5", condFmts))
217+
assert.NoError(t, f.SetConditionalFormat("Sheet1", "B1:B5", condFmts))
218+
for r := 1; r <= 20; r++ {
219+
cell, err := CoordinatesToCellName(1, r)
220+
assert.NoError(t, err)
221+
assert.NoError(t, f.SetCellValue("Sheet1", cell, r))
222+
cell, err = CoordinatesToCellName(2, r)
223+
assert.NoError(t, err)
224+
assert.NoError(t, f.SetCellValue("Sheet1", cell, r))
225+
}
226+
ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
227+
assert.True(t, ok)
228+
var priorities []int
229+
expected := []int{1, 2, 3, 4, 5, 6}
230+
for _, condFmt := range ws.(*xlsxWorksheet).ConditionalFormatting {
231+
for _, rule := range condFmt.CfRule {
232+
priorities = append(priorities, rule.Priority)
233+
}
234+
}
235+
assert.Equal(t, expected, priorities)
236+
assert.NoError(t, f.Close())
237+
})
196238
}
197239

198240
func TestGetConditionalFormats(t *testing.T) {

0 commit comments

Comments
 (0)