Skip to content

Commit 1989dc2

Browse files
committed
This closes qax-os#1293, add new function GetColStyle
- Fix generate workbook corruption after insert cols/rows in some case - Update unit tests - Update dependencies module
1 parent 7a852ed commit 1989dc2

File tree

5 files changed

+70
-16
lines changed

5 files changed

+70
-16
lines changed

adjust.go

+19-9
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,19 @@ func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int)
7373
// adjustColDimensions provides a function to update column dimensions when
7474
// inserting or deleting rows or columns.
7575
func (f *File) adjustColDimensions(ws *xlsxWorksheet, col, offset int) error {
76+
for rowIdx := range ws.SheetData.Row {
77+
for _, v := range ws.SheetData.Row[rowIdx].C {
78+
if cellCol, _, _ := CellNameToCoordinates(v.R); col <= cellCol {
79+
if newCol := cellCol + offset; newCol > 0 && newCol > MaxColumns {
80+
return ErrColumnNumber
81+
}
82+
}
83+
}
84+
}
7685
for rowIdx := range ws.SheetData.Row {
7786
for colIdx, v := range ws.SheetData.Row[rowIdx].C {
78-
cellCol, cellRow, _ := CellNameToCoordinates(v.R)
79-
if col <= cellCol {
87+
if cellCol, cellRow, _ := CellNameToCoordinates(v.R); col <= cellCol {
8088
if newCol := cellCol + offset; newCol > 0 {
81-
if newCol > MaxColumns {
82-
return ErrColumnNumber
83-
}
8489
ws.SheetData.Row[rowIdx].C[colIdx].R, _ = CoordinatesToCellName(newCol, cellRow)
8590
}
8691
}
@@ -92,12 +97,17 @@ func (f *File) adjustColDimensions(ws *xlsxWorksheet, col, offset int) error {
9297
// adjustRowDimensions provides a function to update row dimensions when
9398
// inserting or deleting rows or columns.
9499
func (f *File) adjustRowDimensions(ws *xlsxWorksheet, row, offset int) error {
95-
for i := range ws.SheetData.Row {
100+
totalRows := len(ws.SheetData.Row)
101+
if totalRows == 0 {
102+
return nil
103+
}
104+
lastRow := &ws.SheetData.Row[totalRows-1]
105+
if newRow := lastRow.R + offset; lastRow.R >= row && newRow > 0 && newRow >= TotalRows {
106+
return ErrMaxRows
107+
}
108+
for i := 0; i < len(ws.SheetData.Row); i++ {
96109
r := &ws.SheetData.Row[i]
97110
if newRow := r.R + offset; r.R >= row && newRow > 0 {
98-
if newRow >= TotalRows {
99-
return ErrMaxRows
100-
}
101111
f.adjustSingleRowDimensions(r, newRow)
102112
}
103113
}

col.go

+26
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,30 @@ func (f *File) getColWidth(sheet string, col int) int {
638638
return int(defaultColWidthPixels)
639639
}
640640

641+
// GetColStyle provides a function to get column style ID by given worksheet
642+
// name and column name.
643+
func (f *File) GetColStyle(sheet, col string) (int, error) {
644+
var styleID int
645+
colNum, err := ColumnNameToNumber(col)
646+
if err != nil {
647+
return styleID, err
648+
}
649+
ws, err := f.workSheetReader(sheet)
650+
if err != nil {
651+
return styleID, err
652+
}
653+
ws.Lock()
654+
defer ws.Unlock()
655+
if ws.Cols != nil {
656+
for _, v := range ws.Cols.Col {
657+
if v.Min <= colNum && colNum <= v.Max {
658+
styleID = v.Style
659+
}
660+
}
661+
}
662+
return styleID, err
663+
}
664+
641665
// GetColWidth provides a function to get column width by given worksheet name
642666
// and column name.
643667
func (f *File) GetColWidth(sheet, col string) (float64, error) {
@@ -649,6 +673,8 @@ func (f *File) GetColWidth(sheet, col string) (float64, error) {
649673
if err != nil {
650674
return defaultColWidth, err
651675
}
676+
ws.Lock()
677+
defer ws.Unlock()
652678
if ws.Cols != nil {
653679
var width float64
654680
for _, v := range ws.Cols.Col {

col_test.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func TestSetColStyle(t *testing.T) {
293293
assert.NoError(t, err)
294294
// Test set column style on not exists worksheet.
295295
assert.EqualError(t, f.SetColStyle("SheetN", "E", styleID), "sheet SheetN does not exist")
296-
// Test set column style with illegal cell coordinates.
296+
// Test set column style with illegal column name.
297297
assert.EqualError(t, f.SetColStyle("Sheet1", "*", styleID), newInvalidColumnNameError("*").Error())
298298
assert.EqualError(t, f.SetColStyle("Sheet1", "A:*", styleID), newInvalidColumnNameError("*").Error())
299299
// Test set column style with invalid style ID.
@@ -302,6 +302,10 @@ func TestSetColStyle(t *testing.T) {
302302
assert.EqualError(t, f.SetColStyle("Sheet1", "B", 10), newInvalidStyleID(10).Error())
303303

304304
assert.NoError(t, f.SetColStyle("Sheet1", "B", styleID))
305+
style, err := f.GetColStyle("Sheet1", "B")
306+
assert.NoError(t, err)
307+
assert.Equal(t, styleID, style)
308+
305309
// Test set column style with already exists column with style.
306310
assert.NoError(t, f.SetColStyle("Sheet1", "B", styleID))
307311
assert.NoError(t, f.SetColStyle("Sheet1", "D:C", styleID))
@@ -343,6 +347,20 @@ func TestColWidth(t *testing.T) {
343347
convertRowHeightToPixels(0)
344348
}
345349

350+
func TestGetColStyle(t *testing.T) {
351+
f := NewFile()
352+
styleID, err := f.GetColStyle("Sheet1", "A")
353+
assert.NoError(t, err)
354+
assert.Equal(t, styleID, 0)
355+
356+
// Test set column style on not exists worksheet.
357+
_, err = f.GetColStyle("SheetN", "A")
358+
assert.EqualError(t, err, "sheet SheetN does not exist")
359+
// Test set column style with illegal column name.
360+
_, err = f.GetColStyle("Sheet1", "*")
361+
assert.EqualError(t, err, newInvalidColumnNameError("*").Error())
362+
}
363+
346364
func TestInsertCols(t *testing.T) {
347365
f := NewFile()
348366
sheet1 := f.GetSheetName(0)

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ require (
1010
github.com/stretchr/testify v1.7.1
1111
github.com/xuri/efp v0.0.0-20220603152613-6918739fd470
1212
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22
13-
golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8
13+
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90
1414
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9
15-
golang.org/x/net v0.0.0-20220812174116-3211cb980234
15+
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b
1616
golang.org/x/text v0.3.7
1717
gopkg.in/yaml.v3 v3.0.0 // indirect
1818
)

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 h1:6932x8ltq1w4utjmfMPVj0
1717
github.com/xuri/efp v0.0.0-20220603152613-6918739fd470/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
1818
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 h1:OAmKAfT06//esDdpi/DZ8Qsdt4+M5+ltca05dA5bG2M=
1919
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
20-
golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8 h1:GIAS/yBem/gq2MUqgNIzUHW7cJMmx3TGZOrnyYaNQ6c=
21-
golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
20+
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM=
21+
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
2222
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 h1:LRtI4W37N+KFebI/qV0OFiLUv4GLOWeEW5hn/KEJvxE=
2323
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
2424
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
25-
golang.org/x/net v0.0.0-20220812174116-3211cb980234 h1:RDqmgfe7SvlMWoqC3xwQ2blLO3fcWcxMa3eBLRdRW7E=
26-
golang.org/x/net v0.0.0-20220812174116-3211cb980234/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
25+
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b h1:ZmngSVLe/wycRns9MKikG9OWIEjGcGAkacif7oYQaUY=
26+
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
2727
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2828
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2929
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

0 commit comments

Comments
 (0)