|
|
@ -279,7 +279,7 @@ func (f *File) GetColVisible(sheet, col string) (bool, error) {
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// err := f.SetColVisible("Sheet1", "D:F", false)
|
|
|
|
// err := f.SetColVisible("Sheet1", "D:F", false)
|
|
|
|
func (f *File) SetColVisible(sheet, columns string, visible bool) error {
|
|
|
|
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 {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -290,8 +290,8 @@ func (f *File) SetColVisible(sheet, columns string, visible bool) error {
|
|
|
|
ws.Lock()
|
|
|
|
ws.Lock()
|
|
|
|
defer ws.Unlock()
|
|
|
|
defer ws.Unlock()
|
|
|
|
colData := xlsxCol{
|
|
|
|
colData := xlsxCol{
|
|
|
|
Min: start,
|
|
|
|
Min: min,
|
|
|
|
Max: end,
|
|
|
|
Max: max,
|
|
|
|
Width: defaultColWidth, // default width
|
|
|
|
Width: defaultColWidth, // default width
|
|
|
|
Hidden: !visible,
|
|
|
|
Hidden: !visible,
|
|
|
|
CustomWidth: true,
|
|
|
|
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.
|
|
|
|
// 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, ":")
|
|
|
|
colsTab := strings.Split(columns, ":")
|
|
|
|
start, err = ColumnNameToNumber(colsTab[0])
|
|
|
|
min, err = ColumnNameToNumber(colsTab[0])
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end = start
|
|
|
|
max = min
|
|
|
|
if len(colsTab) == 2 {
|
|
|
|
if len(colsTab) == 2 {
|
|
|
|
if end, err = ColumnNameToNumber(colsTab[1]); err != nil {
|
|
|
|
if max, err = ColumnNameToNumber(colsTab[1]); err != nil {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if end < start {
|
|
|
|
if max < min {
|
|
|
|
start, end = end, start
|
|
|
|
min, max = max, min
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -416,7 +416,7 @@ func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error {
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// err = f.SetColStyle("Sheet1", "C:F", style)
|
|
|
|
// err = f.SetColStyle("Sheet1", "C:F", style)
|
|
|
|
func (f *File) SetColStyle(sheet, columns string, styleID int) error {
|
|
|
|
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 {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -436,8 +436,8 @@ func (f *File) SetColStyle(sheet, columns string, styleID int) error {
|
|
|
|
ws.Cols = &xlsxCols{}
|
|
|
|
ws.Cols = &xlsxCols{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ws.Cols.Col = flatCols(xlsxCol{
|
|
|
|
ws.Cols.Col = flatCols(xlsxCol{
|
|
|
|
Min: start,
|
|
|
|
Min: min,
|
|
|
|
Max: end,
|
|
|
|
Max: max,
|
|
|
|
Width: defaultColWidth,
|
|
|
|
Width: defaultColWidth,
|
|
|
|
Style: styleID,
|
|
|
|
Style: styleID,
|
|
|
|
}, ws.Cols.Col, func(fc, c xlsxCol) xlsxCol {
|
|
|
|
}, ws.Cols.Col, func(fc, c xlsxCol) xlsxCol {
|
|
|
@ -452,7 +452,7 @@ func (f *File) SetColStyle(sheet, columns string, styleID int) error {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
ws.Unlock()
|
|
|
|
ws.Unlock()
|
|
|
|
if rows := len(ws.SheetData.Row); rows > 0 {
|
|
|
|
if rows := len(ws.SheetData.Row); rows > 0 {
|
|
|
|
for col := start; col <= end; col++ {
|
|
|
|
for col := min; col <= max; col++ {
|
|
|
|
from, _ := CoordinatesToCellName(col, 1)
|
|
|
|
from, _ := CoordinatesToCellName(col, 1)
|
|
|
|
to, _ := CoordinatesToCellName(col, rows)
|
|
|
|
to, _ := CoordinatesToCellName(col, rows)
|
|
|
|
err = f.SetCellStyle(sheet, from, to, styleID)
|
|
|
|
err = f.SetCellStyle(sheet, from, to, styleID)
|
|
|
@ -467,21 +467,13 @@ func (f *File) SetColStyle(sheet, columns string, styleID int) error {
|
|
|
|
// f := excelize.NewFile()
|
|
|
|
// f := excelize.NewFile()
|
|
|
|
// err := f.SetColWidth("Sheet1", "A", "H", 20)
|
|
|
|
// err := f.SetColWidth("Sheet1", "A", "H", 20)
|
|
|
|
func (f *File) SetColWidth(sheet, startCol, endCol string, width float64) error {
|
|
|
|
func (f *File) SetColWidth(sheet, startCol, endCol string, width float64) error {
|
|
|
|
min, err := ColumnNameToNumber(startCol)
|
|
|
|
min, max, err := f.parseColRange(startCol + ":" + endCol)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
max, err := ColumnNameToNumber(endCol)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if width > MaxColumnWidth {
|
|
|
|
if width > MaxColumnWidth {
|
|
|
|
return ErrColumnWidth
|
|
|
|
return ErrColumnWidth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if min > max {
|
|
|
|
|
|
|
|
min, max = max, min
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ws, err := f.workSheetReader(sheet)
|
|
|
|
ws, err := f.workSheetReader(sheet)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|