This closes #989, closes #990

New API: `SetRowStyle` support for set style for the rows
Update documentation for the `GetRows`, `SetCellStyle` and `SetColStyle`
This commit is contained in:
xuri 2021-08-17 00:01:44 +08:00
parent b02f864eab
commit a55f354eb3
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
6 changed files with 65 additions and 5 deletions

4
col.go
View File

@ -398,7 +398,9 @@ func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error {
}
// SetColStyle provides a function to set style of columns by given worksheet
// name, columns range and style ID.
// name, columns range and style ID. Note that this will overwrite the
// existing styles for the cell, it won't append or merge style with existing
// styles.
//
// For example set style of column H on Sheet1:
//

View File

@ -46,6 +46,11 @@ func newUnzipSizeLimitError(unzipSizeLimit int64) error {
return fmt.Errorf("unzip size exceeds the %d bytes limit", unzipSizeLimit)
}
// newInvalidStyleID defined the error message on receiving the invalid style ID.
func newInvalidStyleID(styleID int) error {
return fmt.Errorf("invalid style ID %d, negative values are not supported", styleID)
}
var (
// ErrStreamSetColWidth defined the error message on set column width in
// stream writing mode.
@ -76,6 +81,8 @@ var (
ErrAddVBAProject = errors.New("unsupported VBA project extension")
// ErrToExcelTime defined the error message on receive a not UTC time.
ErrToExcelTime = errors.New("only UTC time expected")
// ErrMaxRows defined the error message on receive a row number exceeds maximum limit.
ErrMaxRows = errors.New("row number exceeds maximum limit")
// ErrMaxRowHeight defined the error message on receive an invalid row
// height.
ErrMaxRowHeight = errors.New("the height of the row must be smaller than or equal to 409 points")

2
lib.go
View File

@ -196,7 +196,7 @@ func CellNameToCoordinates(cell string) (int, int, error) {
return -1, -1, fmt.Errorf(msg, cell, err)
}
if row > TotalRows {
return -1, -1, fmt.Errorf("row number exceeds maximum limit")
return -1, -1, ErrMaxRows
}
col, err := ColumnNameToNumber(colname)
return col, row, err

42
rows.go
View File

@ -23,8 +23,9 @@ import (
"github.com/mohae/deepcopy"
)
// GetRows return all the rows in a sheet by given worksheet name (case
// sensitive). For example:
// GetRows return all the rows in a sheet by given worksheet name
// (case sensitive). GetRows fetched the rows with value or formula cells,
// the tail continuously empty cell will be skipped. For example:
//
// rows, err := f.GetRows("Sheet1")
// if err != nil {
@ -719,6 +720,43 @@ func checkRow(ws *xlsxWorksheet) error {
return nil
}
// SetRowStyle provides a function to set style of rows by given worksheet
// name, row range and style ID. Note that this will overwrite the existing
// styles for the cell, it won't append or merge style with existing styles.
//
// For example set style of row 1 on Sheet1:
//
// err = f.SetRowStyle("Sheet1", 1, style)
//
// Set style of rows 1 to 10 on Sheet1:
//
// err = f.SetRowStyle("Sheet1", 1, 10, style)
//
func (f *File) SetRowStyle(sheet string, start, end, styleID int) error {
if end < start {
start, end = end, start
}
if start < 1 {
return newInvalidRowNumberError(start)
}
if end > TotalRows {
return ErrMaxRows
}
if styleID < 0 {
return newInvalidStyleID(styleID)
}
ws, err := f.workSheetReader(sheet)
if err != nil {
return err
}
prepareSheetXML(ws, 0, end)
for row := start - 1; row < end; row++ {
ws.SheetData.Row[row].S = styleID
ws.SheetData.Row[row].CustomFormat = true
}
return nil
}
// convertRowHeightToPixels provides a function to convert the height of a
// cell from user's units to pixels. If the height hasn't been set by the user
// we use the default value. If the row is hidden it has a value of zero.

View File

@ -889,6 +889,18 @@ func TestCheckRow(t *testing.T) {
assert.EqualError(t, f.SetCellValue("Sheet1", "A1", false), `cannot convert cell "-" to coordinates: invalid cell name "-"`)
}
func TestSetRowStyle(t *testing.T) {
f := NewFile()
styleID, err := f.NewStyle(`{"fill":{"type":"pattern","color":["#E0EBF5"],"pattern":1}}`)
assert.NoError(t, err)
assert.EqualError(t, f.SetRowStyle("Sheet1", 10, -1, styleID), newInvalidRowNumberError(-1).Error())
assert.EqualError(t, f.SetRowStyle("Sheet1", 1, TotalRows+1, styleID), ErrMaxRows.Error())
assert.EqualError(t, f.SetRowStyle("Sheet1", 1, 1, -1), newInvalidStyleID(-1).Error())
assert.EqualError(t, f.SetRowStyle("SheetN", 1, 1, styleID), "sheet SheetN is not exist")
assert.NoError(t, f.SetRowStyle("Sheet1", 10, 1, styleID))
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetRowStyle.xlsx")))
}
func TestNumberFormats(t *testing.T) {
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
if !assert.NoError(t, err) {

View File

@ -2603,7 +2603,8 @@ func (f *File) GetCellStyle(sheet, axis string) (int, error) {
// SetCellStyle provides a function to add style attribute for cells by given
// worksheet name, coordinate area and style ID. Note that diagonalDown and
// diagonalUp type border should be use same color in the same coordinate
// area.
// area, this will overwrite the existing styles for the cell, it won't
// append or merge style with existing styles.
//
// For example create a borders of cell H9 on Sheet1:
//