Skip to content

Commit 8b325dc

Browse files
authored
Fix DeleteDataValidation with unordered sqref ranges (qax-os#2248)
1 parent a880146 commit 8b325dc

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

datavalidation_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,32 @@ func TestDeleteDataValidation(t *testing.T) {
270270
ExtLst: &xlsxExtLst{Ext: "<extLst><x14:dataValidations></x14:dataValidation></x14:dataValidations></ext></extLst>"},
271271
}, nil), "XML syntax error on line 1: element <dataValidations> closed by </dataValidation>")
272272
})
273+
274+
t.Run("delete_data_validation_with_unordered_sqref", func(t *testing.T) {
275+
// Test deleting data validation when sqref has unordered ranges
276+
f := NewFile()
277+
ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
278+
assert.True(t, ok)
279+
280+
ws.(*xlsxWorksheet).DataValidations = &xlsxDataValidations{
281+
Count: 1,
282+
DataValidation: []*xlsxDataValidation{{
283+
AllowBlank: true,
284+
ShowInputMessage: true,
285+
ShowErrorMessage: true,
286+
Type: "whole",
287+
Operator: "between",
288+
Sqref: "A5:A10 A15:A20 A3:A4",
289+
Formula1: &xlsxInnerXML{Content: "1"},
290+
Formula2: &xlsxInnerXML{Content: "100"},
291+
}},
292+
}
293+
294+
assert.NoError(t, f.DeleteDataValidation("Sheet1", "A7"))
295+
296+
dvs, err := f.GetDataValidations("Sheet1")
297+
assert.NoError(t, err)
298+
assert.Len(t, dvs, 1)
299+
assert.Equal(t, "A3:A6 A8:A10 A15:A20", dvs[0].Sqref)
300+
})
273301
}

lib.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424
"reflect"
2525
"regexp"
26+
"sort"
2627
"strconv"
2728
"strings"
2829
"unicode/utf16"
@@ -393,6 +394,12 @@ func flatSqref(sqref string) (cells map[int][][]int, err error) {
393394
}
394395
}
395396
}
397+
// Sort each column's cells by row number
398+
for col := range cells {
399+
sort.Slice(cells[col], func(i, j int) bool {
400+
return cells[col][i][1] < cells[col][j][1]
401+
})
402+
}
396403
return
397404
}
398405

0 commit comments

Comments
 (0)