Skip to content

Commit 79b9d65

Browse files
shcabinshcabin
authored andcommitted
feat: use binary search
1 parent 7e8727d commit 79b9d65

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

cell.go

Lines changed: 20 additions & 21 deletions
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,34 +1499,32 @@ func (f *File) getCellStringFunc(sheet, cell string, fn func(x *xlsxWorksheet, c
14981499
return "", nil
14991500
}
15001501

1501-
var rowData *xlsxRow
1502-
for rowIdx := range ws.SheetData.Row {
1503-
if ws.SheetData.Row[rowIdx].R == row {
1504-
rowData = &ws.SheetData.Row[rowIdx]
1505-
break
1502+
idx, found := sort.Find(len(ws.SheetData.Row), func(i int) int {
1503+
if ws.SheetData.Row[i].R == row {
1504+
return 0
1505+
} else if ws.SheetData.Row[i].R > row {
1506+
return -1
1507+
} else {
1508+
return 1
15061509
}
1507-
}
1508-
if rowData == nil {
1510+
})
1511+
if !found {
15091512
return "", nil
15101513
}
1511-
1512-
var colData *xlsxC
1514+
rowData := ws.SheetData.Row[idx]
15131515
for colIdx := range rowData.C {
1514-
if rowData.C[colIdx].R == cell {
1515-
colData = &rowData.C[colIdx]
1516+
colData := &rowData.C[colIdx]
1517+
if cell == colData.R {
1518+
val, ok, err := fn(ws, colData)
1519+
if err != nil {
1520+
return "", err
1521+
}
1522+
if ok {
1523+
return val, nil
1524+
}
15161525
break
15171526
}
15181527
}
1519-
if colData == nil {
1520-
return "", nil
1521-
}
1522-
val, ok, err := fn(ws, colData)
1523-
if err != nil {
1524-
return "", err
1525-
}
1526-
if ok {
1527-
return val, nil
1528-
}
15291528
return "", nil
15301529
}
15311530

0 commit comments

Comments
 (0)