forked from p30928647/excelize
Support update column style when inserting or deleting columns
- Go Modules dependencies upgrade - Unify internal variable name - Unit test updated
This commit is contained in:
parent
8753950d62
commit
58b5dae5eb
46
adjust.go
46
adjust.go
|
@ -70,6 +70,50 @@ func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int)
|
|||
return nil
|
||||
}
|
||||
|
||||
// adjustCols provides a function to update column style when inserting or
|
||||
// deleting columns.
|
||||
func (f *File) adjustCols(ws *xlsxWorksheet, col, offset int) error {
|
||||
if ws.Cols == nil {
|
||||
return nil
|
||||
}
|
||||
for i := 0; i < len(ws.Cols.Col); i++ {
|
||||
if offset > 0 {
|
||||
if ws.Cols.Col[i].Max+1 == col {
|
||||
ws.Cols.Col[i].Max += offset
|
||||
continue
|
||||
}
|
||||
if ws.Cols.Col[i].Min >= col {
|
||||
ws.Cols.Col[i].Min += offset
|
||||
ws.Cols.Col[i].Max += offset
|
||||
continue
|
||||
}
|
||||
if ws.Cols.Col[i].Min < col && ws.Cols.Col[i].Max >= col {
|
||||
ws.Cols.Col[i].Max += offset
|
||||
}
|
||||
}
|
||||
if offset < 0 {
|
||||
if ws.Cols.Col[i].Min == col && ws.Cols.Col[i].Max == col {
|
||||
if len(ws.Cols.Col) > 1 {
|
||||
ws.Cols.Col = append(ws.Cols.Col[:i], ws.Cols.Col[i+1:]...)
|
||||
} else {
|
||||
ws.Cols.Col = nil
|
||||
}
|
||||
i--
|
||||
continue
|
||||
}
|
||||
if ws.Cols.Col[i].Min > col {
|
||||
ws.Cols.Col[i].Min += offset
|
||||
ws.Cols.Col[i].Max += offset
|
||||
continue
|
||||
}
|
||||
if ws.Cols.Col[i].Min <= col && ws.Cols.Col[i].Max >= col {
|
||||
ws.Cols.Col[i].Max += offset
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// adjustColDimensions provides a function to update column dimensions when
|
||||
// inserting or deleting rows or columns.
|
||||
func (f *File) adjustColDimensions(ws *xlsxWorksheet, col, offset int) error {
|
||||
|
@ -91,7 +135,7 @@ func (f *File) adjustColDimensions(ws *xlsxWorksheet, col, offset int) error {
|
|||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return f.adjustCols(ws, col, offset)
|
||||
}
|
||||
|
||||
// adjustRowDimensions provides a function to update row dimensions when
|
||||
|
|
|
@ -366,3 +366,79 @@ func TestAdjustCalcChain(t *testing.T) {
|
|||
f.CalcChain = nil
|
||||
assert.NoError(t, f.InsertCols("Sheet1", "A", 1))
|
||||
}
|
||||
|
||||
func TestAdjustCols(t *testing.T) {
|
||||
sheetName := "Sheet1"
|
||||
preset := func() (*File, error) {
|
||||
f := NewFile()
|
||||
if err := f.SetColWidth(sheetName, "J", "T", 5); err != nil {
|
||||
return f, err
|
||||
}
|
||||
if err := f.SetSheetRow(sheetName, "J1", &[]string{"J1", "K1", "L1", "M1", "N1", "O1", "P1", "Q1", "R1", "S1", "T1"}); err != nil {
|
||||
return f, err
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
baseTbl := []string{"B", "J", "O", "O", "O", "U", "V"}
|
||||
insertTbl := []int{2, 2, 2, 5, 6, 2, 2}
|
||||
expectedTbl := []map[string]float64{
|
||||
{"J": defaultColWidth, "K": defaultColWidth, "U": 5, "V": 5, "W": defaultColWidth},
|
||||
{"J": defaultColWidth, "K": defaultColWidth, "U": 5, "V": 5, "W": defaultColWidth},
|
||||
{"O": 5, "P": 5, "U": 5, "V": 5, "W": defaultColWidth},
|
||||
{"O": 5, "S": 5, "X": 5, "Y": 5, "Z": defaultColWidth},
|
||||
{"O": 5, "S": 5, "Y": 5, "X": 5, "AA": defaultColWidth},
|
||||
{"U": 5, "V": 5, "W": defaultColWidth},
|
||||
{"U": defaultColWidth, "V": defaultColWidth, "W": defaultColWidth},
|
||||
}
|
||||
for idx, columnName := range baseTbl {
|
||||
f, err := preset()
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, f.InsertCols(sheetName, columnName, insertTbl[idx]))
|
||||
for column, expected := range expectedTbl[idx] {
|
||||
width, err := f.GetColWidth(sheetName, column)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, width, column)
|
||||
}
|
||||
assert.NoError(t, f.Close())
|
||||
}
|
||||
|
||||
baseTbl = []string{"B", "J", "O", "T"}
|
||||
expectedTbl = []map[string]float64{
|
||||
{"H": defaultColWidth, "I": 5, "S": 5, "T": defaultColWidth},
|
||||
{"I": defaultColWidth, "J": 5, "S": 5, "T": defaultColWidth},
|
||||
{"I": defaultColWidth, "O": 5, "S": 5, "T": defaultColWidth},
|
||||
{"R": 5, "S": 5, "T": defaultColWidth, "U": defaultColWidth},
|
||||
}
|
||||
for idx, columnName := range baseTbl {
|
||||
f, err := preset()
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, f.RemoveCol(sheetName, columnName))
|
||||
for column, expected := range expectedTbl[idx] {
|
||||
width, err := f.GetColWidth(sheetName, column)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, width, column)
|
||||
}
|
||||
assert.NoError(t, f.Close())
|
||||
}
|
||||
|
||||
f, err := preset()
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, f.SetColWidth(sheetName, "I", "I", 8))
|
||||
for i := 0; i <= 12; i++ {
|
||||
assert.NoError(t, f.RemoveCol(sheetName, "I"))
|
||||
}
|
||||
for c := 9; c <= 21; c++ {
|
||||
columnName, err := ColumnNumberToName(c)
|
||||
assert.NoError(t, err)
|
||||
width, err := f.GetColWidth(sheetName, columnName)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, defaultColWidth, width, columnName)
|
||||
}
|
||||
|
||||
ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
|
||||
assert.True(t, ok)
|
||||
ws.(*xlsxWorksheet).Cols = nil
|
||||
assert.NoError(t, f.RemoveCol(sheetName, "A"))
|
||||
|
||||
assert.NoError(t, f.Close())
|
||||
}
|
||||
|
|
36
col.go
36
col.go
|
@ -279,7 +279,7 @@ func (f *File) GetColVisible(sheet, col string) (bool, error) {
|
|||
//
|
||||
// err := f.SetColVisible("Sheet1", "D:F", false)
|
||||
func (f *File) SetColVisible(sheet, columns string, visible bool) error {
|
||||
start, end, err := f.parseColRange(columns)
|
||||
min, max, err := f.parseColRange(columns)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -290,8 +290,8 @@ func (f *File) SetColVisible(sheet, columns string, visible bool) error {
|
|||
ws.Lock()
|
||||
defer ws.Unlock()
|
||||
colData := xlsxCol{
|
||||
Min: start,
|
||||
Max: end,
|
||||
Min: min,
|
||||
Max: max,
|
||||
Width: defaultColWidth, // default width
|
||||
Hidden: !visible,
|
||||
CustomWidth: true,
|
||||
|
@ -343,20 +343,20 @@ func (f *File) GetColOutlineLevel(sheet, col string) (uint8, error) {
|
|||
}
|
||||
|
||||
// parseColRange parse and convert column range with column name to the column number.
|
||||
func (f *File) parseColRange(columns string) (start, end int, err error) {
|
||||
func (f *File) parseColRange(columns string) (min, max int, err error) {
|
||||
colsTab := strings.Split(columns, ":")
|
||||
start, err = ColumnNameToNumber(colsTab[0])
|
||||
min, err = ColumnNameToNumber(colsTab[0])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
end = start
|
||||
max = min
|
||||
if len(colsTab) == 2 {
|
||||
if end, err = ColumnNameToNumber(colsTab[1]); err != nil {
|
||||
if max, err = ColumnNameToNumber(colsTab[1]); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
if end < start {
|
||||
start, end = end, start
|
||||
if max < min {
|
||||
min, max = max, min
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -416,7 +416,7 @@ func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error {
|
|||
//
|
||||
// err = f.SetColStyle("Sheet1", "C:F", style)
|
||||
func (f *File) SetColStyle(sheet, columns string, styleID int) error {
|
||||
start, end, err := f.parseColRange(columns)
|
||||
min, max, err := f.parseColRange(columns)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -436,8 +436,8 @@ func (f *File) SetColStyle(sheet, columns string, styleID int) error {
|
|||
ws.Cols = &xlsxCols{}
|
||||
}
|
||||
ws.Cols.Col = flatCols(xlsxCol{
|
||||
Min: start,
|
||||
Max: end,
|
||||
Min: min,
|
||||
Max: max,
|
||||
Width: defaultColWidth,
|
||||
Style: styleID,
|
||||
}, ws.Cols.Col, func(fc, c xlsxCol) xlsxCol {
|
||||
|
@ -452,7 +452,7 @@ func (f *File) SetColStyle(sheet, columns string, styleID int) error {
|
|||
})
|
||||
ws.Unlock()
|
||||
if rows := len(ws.SheetData.Row); rows > 0 {
|
||||
for col := start; col <= end; col++ {
|
||||
for col := min; col <= max; col++ {
|
||||
from, _ := CoordinatesToCellName(col, 1)
|
||||
to, _ := CoordinatesToCellName(col, rows)
|
||||
err = f.SetCellStyle(sheet, from, to, styleID)
|
||||
|
@ -467,21 +467,13 @@ func (f *File) SetColStyle(sheet, columns string, styleID int) error {
|
|||
// f := excelize.NewFile()
|
||||
// err := f.SetColWidth("Sheet1", "A", "H", 20)
|
||||
func (f *File) SetColWidth(sheet, startCol, endCol string, width float64) error {
|
||||
min, err := ColumnNameToNumber(startCol)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
max, err := ColumnNameToNumber(endCol)
|
||||
min, max, err := f.parseColRange(startCol + ":" + endCol)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if width > MaxColumnWidth {
|
||||
return ErrColumnWidth
|
||||
}
|
||||
if min > max {
|
||||
min, max = max, min
|
||||
}
|
||||
|
||||
ws, err := f.workSheetReader(sheet)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
6
go.mod
6
go.mod
|
@ -8,10 +8,10 @@ require (
|
|||
github.com/stretchr/testify v1.8.0
|
||||
github.com/xuri/efp v0.0.0-20220603152613-6918739fd470
|
||||
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22
|
||||
golang.org/x/crypto v0.0.0-20221012134737-56aed061732a
|
||||
golang.org/x/crypto v0.2.0
|
||||
golang.org/x/image v0.0.0-20220902085622-e7cb96979f69
|
||||
golang.org/x/net v0.0.0-20221012135044-0b7e1fb9d458
|
||||
golang.org/x/text v0.3.8
|
||||
golang.org/x/net v0.2.0
|
||||
golang.org/x/text v0.4.0
|
||||
)
|
||||
|
||||
require github.com/richardlehane/msoleps v1.0.3 // indirect
|
||||
|
|
18
go.sum
18
go.sum
|
@ -22,34 +22,32 @@ github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22/go.mod h1:WwHg+CVyzlv/TX9
|
|||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.0.0-20221012134737-56aed061732a h1:NmSIgad6KjE6VvHciPZuNRTKxGhlPfD6OA87W/PLkqg=
|
||||
golang.org/x/crypto v0.0.0-20221012134737-56aed061732a/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/crypto v0.2.0 h1:BRXPfhNivWL5Yq0BGQ39a2sW6t44aODpfxkWjYdzewE=
|
||||
golang.org/x/crypto v0.2.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
|
||||
golang.org/x/image v0.0.0-20220902085622-e7cb96979f69 h1:Lj6HJGCSn5AjxRAH2+r35Mir4icalbqku+CLUtjnvXY=
|
||||
golang.org/x/image v0.0.0-20220902085622-e7cb96979f69/go.mod h1:doUCurBvlfPMKfmIpRIywoHmhN3VyhnoFDbvIEWF4hY=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.0.0-20221012135044-0b7e1fb9d458 h1:MgJ6t2zo8v0tbmLCueaCbF1RM+TtB0rs3Lv8DGtOIpY=
|
||||
golang.org/x/net v0.0.0-20221012135044-0b7e1fb9d458/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
|
||||
golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU=
|
||||
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
|
||||
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
|
||||
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
|
||||
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
|
|
26
sheetpr.go
26
sheetpr.go
|
@ -163,26 +163,12 @@ func (f *File) SetSheetProps(sheet string, opts *SheetPropsOptions) error {
|
|||
if ws.SheetFormatPr == nil {
|
||||
ws.SheetFormatPr = &xlsxSheetFormatPr{DefaultRowHeight: defaultRowHeight}
|
||||
}
|
||||
if opts.BaseColWidth != nil {
|
||||
ws.SheetFormatPr.BaseColWidth = *opts.BaseColWidth
|
||||
}
|
||||
if opts.DefaultColWidth != nil {
|
||||
ws.SheetFormatPr.DefaultColWidth = *opts.DefaultColWidth
|
||||
}
|
||||
if opts.DefaultRowHeight != nil {
|
||||
ws.SheetFormatPr.DefaultRowHeight = *opts.DefaultRowHeight
|
||||
}
|
||||
if opts.CustomHeight != nil {
|
||||
ws.SheetFormatPr.CustomHeight = *opts.CustomHeight
|
||||
}
|
||||
if opts.ZeroHeight != nil {
|
||||
ws.SheetFormatPr.ZeroHeight = *opts.ZeroHeight
|
||||
}
|
||||
if opts.ThickTop != nil {
|
||||
ws.SheetFormatPr.ThickTop = *opts.ThickTop
|
||||
}
|
||||
if opts.ThickBottom != nil {
|
||||
ws.SheetFormatPr.ThickBottom = *opts.ThickBottom
|
||||
s := reflect.ValueOf(opts).Elem()
|
||||
for i := 11; i < 18; i++ {
|
||||
if !s.Field(i).IsNil() {
|
||||
name := s.Type().Field(i).Name
|
||||
reflect.ValueOf(ws.SheetFormatPr).Elem().FieldByName(name).Set(s.Field(i).Elem())
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue