Skip to content

Commit 2c3d13f

Browse files
authored
Using binary search instead of sequential search for cell (qax-os#2129)
- Update unit tests
1 parent 094ff39 commit 2c3d13f

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

cell.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"math"
1818
"os"
1919
"reflect"
20+
"sort"
2021
"strconv"
2122
"strings"
2223
"time"
@@ -1498,23 +1499,30 @@ func (f *File) getCellStringFunc(sheet, cell string, fn func(x *xlsxWorksheet, c
14981499
return "", nil
14991500
}
15001501

1501-
for rowIdx := range ws.SheetData.Row {
1502-
rowData := &ws.SheetData.Row[rowIdx]
1503-
if rowData.R != row {
1504-
continue
1502+
idx, found := sort.Find(len(ws.SheetData.Row), func(i int) int {
1503+
if ws.SheetData.Row[i].R == row {
1504+
return 0
15051505
}
1506-
for colIdx := range rowData.C {
1507-
colData := &rowData.C[colIdx]
1508-
if cell != colData.R {
1509-
continue
1510-
}
1506+
if ws.SheetData.Row[i].R > row {
1507+
return -1
1508+
}
1509+
return 1
1510+
})
1511+
if !found {
1512+
return "", nil
1513+
}
1514+
rowData := ws.SheetData.Row[idx]
1515+
for colIdx := range rowData.C {
1516+
colData := &rowData.C[colIdx]
1517+
if cell == colData.R {
15111518
val, ok, err := fn(ws, colData)
15121519
if err != nil {
15131520
return "", err
15141521
}
15151522
if ok {
15161523
return val, nil
15171524
}
1525+
break
15181526
}
15191527
}
15201528
return "", nil

cell_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -1265,3 +1265,14 @@ func TestSetCellIntFunc(t *testing.T) {
12651265
func TestSIString(t *testing.T) {
12661266
assert.Empty(t, xlsxSI{}.String())
12671267
}
1268+
1269+
func TestGetCellStringFunc(t *testing.T) {
1270+
f := NewFile()
1271+
ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
1272+
assert.True(t, ok)
1273+
ws.(*xlsxWorksheet).SheetData.Row = []xlsxRow{{R: 2}}
1274+
val, err := f.GetCellValue("Sheet1", "A1")
1275+
assert.Empty(t, val)
1276+
assert.NoError(t, err)
1277+
assert.NoError(t, f.Close())
1278+
}

0 commit comments

Comments
 (0)