Skip to content

Commit 2514bb1

Browse files
committed
Fix qax-os#724, standardize variable naming and update unit tests
1 parent 5dd0b4a commit 2514bb1

27 files changed

+439
-437
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ go:
1313
os:
1414
- linux
1515
- osx
16+
- windows
1617

1718
env:
1819
jobs:

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func main() {
5151
f.SetCellValue("Sheet1", "B2", 100)
5252
// Set active sheet of the workbook.
5353
f.SetActiveSheet(index)
54-
// Save xlsx file by the given path.
54+
// Save spreadsheet by the given path.
5555
if err := f.SaveAs("Book1.xlsx"); err != nil {
5656
fmt.Println(err)
5757
}
@@ -124,7 +124,7 @@ func main() {
124124
fmt.Println(err)
125125
return
126126
}
127-
// Save xlsx file by the given path.
127+
// Save spreadsheet by the given path.
128128
if err := f.SaveAs("Book1.xlsx"); err != nil {
129129
fmt.Println(err)
130130
}
@@ -163,7 +163,7 @@ func main() {
163163
if err := f.AddPicture("Sheet1", "H2", "image.gif", `{"x_offset": 15, "y_offset": 10, "print_obj": true, "lock_aspect_ratio": false, "locked": false}`); err != nil {
164164
fmt.Println(err)
165165
}
166-
// Save the xlsx file with the origin path.
166+
// Save the spreadsheet with the origin path.
167167
if err = f.Save(); err != nil {
168168
fmt.Println(err)
169169
}

adjust.go

+46-49
Original file line numberDiff line numberDiff line change
@@ -35,44 +35,44 @@ const (
3535
// TODO: adjustPageBreaks, adjustComments, adjustDataValidations, adjustProtectedCells
3636
//
3737
func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) error {
38-
xlsx, err := f.workSheetReader(sheet)
38+
ws, err := f.workSheetReader(sheet)
3939
if err != nil {
4040
return err
4141
}
4242
if dir == rows {
43-
f.adjustRowDimensions(xlsx, num, offset)
43+
f.adjustRowDimensions(ws, num, offset)
4444
} else {
45-
f.adjustColDimensions(xlsx, num, offset)
45+
f.adjustColDimensions(ws, num, offset)
4646
}
47-
f.adjustHyperlinks(xlsx, sheet, dir, num, offset)
48-
if err = f.adjustMergeCells(xlsx, dir, num, offset); err != nil {
47+
f.adjustHyperlinks(ws, sheet, dir, num, offset)
48+
if err = f.adjustMergeCells(ws, dir, num, offset); err != nil {
4949
return err
5050
}
51-
if err = f.adjustAutoFilter(xlsx, dir, num, offset); err != nil {
51+
if err = f.adjustAutoFilter(ws, dir, num, offset); err != nil {
5252
return err
5353
}
5454
if err = f.adjustCalcChain(dir, num, offset); err != nil {
5555
return err
5656
}
57-
checkSheet(xlsx)
58-
_ = checkRow(xlsx)
57+
checkSheet(ws)
58+
_ = checkRow(ws)
5959

60-
if xlsx.MergeCells != nil && len(xlsx.MergeCells.Cells) == 0 {
61-
xlsx.MergeCells = nil
60+
if ws.MergeCells != nil && len(ws.MergeCells.Cells) == 0 {
61+
ws.MergeCells = nil
6262
}
6363

6464
return nil
6565
}
6666

6767
// adjustColDimensions provides a function to update column dimensions when
6868
// inserting or deleting rows or columns.
69-
func (f *File) adjustColDimensions(xlsx *xlsxWorksheet, col, offset int) {
70-
for rowIdx := range xlsx.SheetData.Row {
71-
for colIdx, v := range xlsx.SheetData.Row[rowIdx].C {
69+
func (f *File) adjustColDimensions(ws *xlsxWorksheet, col, offset int) {
70+
for rowIdx := range ws.SheetData.Row {
71+
for colIdx, v := range ws.SheetData.Row[rowIdx].C {
7272
cellCol, cellRow, _ := CellNameToCoordinates(v.R)
7373
if col <= cellCol {
7474
if newCol := cellCol + offset; newCol > 0 {
75-
xlsx.SheetData.Row[rowIdx].C[colIdx].R, _ = CoordinatesToCellName(newCol, cellRow)
75+
ws.SheetData.Row[rowIdx].C[colIdx].R, _ = CoordinatesToCellName(newCol, cellRow)
7676
}
7777
}
7878
}
@@ -81,9 +81,9 @@ func (f *File) adjustColDimensions(xlsx *xlsxWorksheet, col, offset int) {
8181

8282
// adjustRowDimensions provides a function to update row dimensions when
8383
// inserting or deleting rows or columns.
84-
func (f *File) adjustRowDimensions(xlsx *xlsxWorksheet, row, offset int) {
85-
for i := range xlsx.SheetData.Row {
86-
r := &xlsx.SheetData.Row[i]
84+
func (f *File) adjustRowDimensions(ws *xlsxWorksheet, row, offset int) {
85+
for i := range ws.SheetData.Row {
86+
r := &ws.SheetData.Row[i]
8787
if newRow := r.R + offset; r.R >= row && newRow > 0 {
8888
f.ajustSingleRowDimensions(r, newRow)
8989
}
@@ -101,38 +101,35 @@ func (f *File) ajustSingleRowDimensions(r *xlsxRow, num int) {
101101

102102
// adjustHyperlinks provides a function to update hyperlinks when inserting or
103103
// deleting rows or columns.
104-
func (f *File) adjustHyperlinks(xlsx *xlsxWorksheet, sheet string, dir adjustDirection, num, offset int) {
104+
func (f *File) adjustHyperlinks(ws *xlsxWorksheet, sheet string, dir adjustDirection, num, offset int) {
105105
// short path
106-
if xlsx.Hyperlinks == nil || len(xlsx.Hyperlinks.Hyperlink) == 0 {
106+
if ws.Hyperlinks == nil || len(ws.Hyperlinks.Hyperlink) == 0 {
107107
return
108108
}
109109

110110
// order is important
111111
if offset < 0 {
112-
for i := len(xlsx.Hyperlinks.Hyperlink) - 1; i >= 0; i-- {
113-
linkData := xlsx.Hyperlinks.Hyperlink[i]
112+
for i := len(ws.Hyperlinks.Hyperlink) - 1; i >= 0; i-- {
113+
linkData := ws.Hyperlinks.Hyperlink[i]
114114
colNum, rowNum, _ := CellNameToCoordinates(linkData.Ref)
115115

116116
if (dir == rows && num == rowNum) || (dir == columns && num == colNum) {
117117
f.deleteSheetRelationships(sheet, linkData.RID)
118-
if len(xlsx.Hyperlinks.Hyperlink) > 1 {
119-
xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink[:i],
120-
xlsx.Hyperlinks.Hyperlink[i+1:]...)
118+
if len(ws.Hyperlinks.Hyperlink) > 1 {
119+
ws.Hyperlinks.Hyperlink = append(ws.Hyperlinks.Hyperlink[:i],
120+
ws.Hyperlinks.Hyperlink[i+1:]...)
121121
} else {
122-
xlsx.Hyperlinks = nil
122+
ws.Hyperlinks = nil
123123
}
124124
}
125125
}
126126
}
127-
128-
if xlsx.Hyperlinks == nil {
127+
if ws.Hyperlinks == nil {
129128
return
130129
}
131-
132-
for i := range xlsx.Hyperlinks.Hyperlink {
133-
link := &xlsx.Hyperlinks.Hyperlink[i] // get reference
130+
for i := range ws.Hyperlinks.Hyperlink {
131+
link := &ws.Hyperlinks.Hyperlink[i] // get reference
134132
colNum, rowNum, _ := CellNameToCoordinates(link.Ref)
135-
136133
if dir == rows {
137134
if rowNum >= num {
138135
link.Ref, _ = CoordinatesToCellName(colNum, rowNum+offset)
@@ -147,21 +144,21 @@ func (f *File) adjustHyperlinks(xlsx *xlsxWorksheet, sheet string, dir adjustDir
147144

148145
// adjustAutoFilter provides a function to update the auto filter when
149146
// inserting or deleting rows or columns.
150-
func (f *File) adjustAutoFilter(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) error {
151-
if xlsx.AutoFilter == nil {
147+
func (f *File) adjustAutoFilter(ws *xlsxWorksheet, dir adjustDirection, num, offset int) error {
148+
if ws.AutoFilter == nil {
152149
return nil
153150
}
154151

155-
coordinates, err := f.areaRefToCoordinates(xlsx.AutoFilter.Ref)
152+
coordinates, err := f.areaRefToCoordinates(ws.AutoFilter.Ref)
156153
if err != nil {
157154
return err
158155
}
159156
x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
160157

161158
if (dir == rows && y1 == num && offset < 0) || (dir == columns && x1 == num && x2 == num) {
162-
xlsx.AutoFilter = nil
163-
for rowIdx := range xlsx.SheetData.Row {
164-
rowData := &xlsx.SheetData.Row[rowIdx]
159+
ws.AutoFilter = nil
160+
for rowIdx := range ws.SheetData.Row {
161+
rowData := &ws.SheetData.Row[rowIdx]
165162
if rowData.R > y1 && rowData.R <= y2 {
166163
rowData.Hidden = false
167164
}
@@ -172,7 +169,7 @@ func (f *File) adjustAutoFilter(xlsx *xlsxWorksheet, dir adjustDirection, num, o
172169
coordinates = f.adjustAutoFilterHelper(dir, coordinates, num, offset)
173170
x1, y1, x2, y2 = coordinates[0], coordinates[1], coordinates[2], coordinates[3]
174171

175-
if xlsx.AutoFilter.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
172+
if ws.AutoFilter.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
176173
return err
177174
}
178175
return nil
@@ -251,35 +248,35 @@ func (f *File) coordinatesToAreaRef(coordinates []int) (string, error) {
251248

252249
// adjustMergeCells provides a function to update merged cells when inserting
253250
// or deleting rows or columns.
254-
func (f *File) adjustMergeCells(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) error {
255-
if xlsx.MergeCells == nil {
251+
func (f *File) adjustMergeCells(ws *xlsxWorksheet, dir adjustDirection, num, offset int) error {
252+
if ws.MergeCells == nil {
256253
return nil
257254
}
258255

259-
for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
260-
areaData := xlsx.MergeCells.Cells[i]
256+
for i := 0; i < len(ws.MergeCells.Cells); i++ {
257+
areaData := ws.MergeCells.Cells[i]
261258
coordinates, err := f.areaRefToCoordinates(areaData.Ref)
262259
if err != nil {
263260
return err
264261
}
265262
x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
266263
if dir == rows {
267264
if y1 == num && y2 == num && offset < 0 {
268-
f.deleteMergeCell(xlsx, i)
265+
f.deleteMergeCell(ws, i)
269266
i--
270267
}
271268
y1 = f.adjustMergeCellsHelper(y1, num, offset)
272269
y2 = f.adjustMergeCellsHelper(y2, num, offset)
273270
} else {
274271
if x1 == num && x2 == num && offset < 0 {
275-
f.deleteMergeCell(xlsx, i)
272+
f.deleteMergeCell(ws, i)
276273
i--
277274
}
278275
x1 = f.adjustMergeCellsHelper(x1, num, offset)
279276
x2 = f.adjustMergeCellsHelper(x2, num, offset)
280277
}
281278
if x1 == x2 && y1 == y2 {
282-
f.deleteMergeCell(xlsx, i)
279+
f.deleteMergeCell(ws, i)
283280
i--
284281
}
285282
if areaData.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
@@ -304,10 +301,10 @@ func (f *File) adjustMergeCellsHelper(pivot, num, offset int) int {
304301
}
305302

306303
// deleteMergeCell provides a function to delete merged cell by given index.
307-
func (f *File) deleteMergeCell(sheet *xlsxWorksheet, idx int) {
308-
if len(sheet.MergeCells.Cells) > idx {
309-
sheet.MergeCells.Cells = append(sheet.MergeCells.Cells[:idx], sheet.MergeCells.Cells[idx+1:]...)
310-
sheet.MergeCells.Count = len(sheet.MergeCells.Cells)
304+
func (f *File) deleteMergeCell(ws *xlsxWorksheet, idx int) {
305+
if len(ws.MergeCells.Cells) > idx {
306+
ws.MergeCells.Cells = append(ws.MergeCells.Cells[:idx], ws.MergeCells.Cells[idx+1:]...)
307+
ws.MergeCells.Count = len(ws.MergeCells.Cells)
311308
}
312309
}
313310

calc_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/stretchr/testify/assert"
10+
"github.com/xuri/efp"
1011
)
1112

1213
func TestCalcCellValue(t *testing.T) {
@@ -854,6 +855,18 @@ func TestCalcCellValue(t *testing.T) {
854855

855856
}
856857

858+
func TestCalculate(t *testing.T) {
859+
err := `strconv.ParseFloat: parsing "string": invalid syntax`
860+
opd := NewStack()
861+
opd.Push(efp.Token{TValue: "string"})
862+
opt := efp.Token{TValue: "-", TType: efp.TokenTypeOperatorPrefix}
863+
assert.EqualError(t, calculate(opd, opt), err)
864+
opd.Push(efp.Token{TValue: "string"})
865+
opd.Push(efp.Token{TValue: "string"})
866+
opt = efp.Token{TValue: "-", TType: efp.TokenTypeOperatorInfix}
867+
assert.EqualError(t, calculate(opd, opt), err)
868+
}
869+
857870
func TestCalcCellValueWithDefinedName(t *testing.T) {
858871
cellData := [][]interface{}{
859872
{"A1 value", "B1 value", nil},

0 commit comments

Comments
 (0)