Support unset custom row height if height value is -1 (#1736)

- Return error if get an invalid row height value
- Update unit tests and update documentation of the SetRowHeigth function
This commit is contained in:
天爱有情 2023-12-02 12:03:09 +08:00 committed by GitHub
parent a16182e004
commit 18a160c5be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 3 deletions

16
rows.go
View File

@ -350,8 +350,10 @@ func (f *File) xmlDecoder(name string) (bool, *xml.Decoder, *os.File, error) {
return true, f.xmlNewDecoder(tempFile), tempFile, err
}
// SetRowHeight provides a function to set the height of a single row. For
// example, set the height of the first row in Sheet1:
// SetRowHeight provides a function to set the height of a single row. If the
// value of height is 0, will hide the specified row, if the value of height is
// -1, will unset the custom row height. For example, set the height of the
// first row in Sheet1:
//
// err := f.SetRowHeight("Sheet1", 1, 50)
func (f *File) SetRowHeight(sheet string, row int, height float64) error {
@ -361,6 +363,9 @@ func (f *File) SetRowHeight(sheet string, row int, height float64) error {
if height > MaxRowHeight {
return ErrMaxRowHeight
}
if height < -1 {
return ErrParameterInvalid
}
ws, err := f.workSheetReader(sheet)
if err != nil {
return err
@ -369,9 +374,14 @@ func (f *File) SetRowHeight(sheet string, row int, height float64) error {
ws.prepareSheetXML(0, row)
rowIdx := row - 1
if height == -1 {
ws.SheetData.Row[rowIdx].Ht = nil
ws.SheetData.Row[rowIdx].CustomHeight = false
return err
}
ws.SheetData.Row[rowIdx].Ht = float64Ptr(height)
ws.SheetData.Row[rowIdx].CustomHeight = true
return nil
return err
}
// getRowHeight provides a function to get row height in pixels by given sheet

View File

@ -1034,6 +1034,22 @@ func TestSetRowStyle(t *testing.T) {
assert.EqualError(t, f.SetRowStyle("Sheet1", 1, 1, cellStyleID), "XML syntax error on line 1: invalid UTF-8")
}
func TestSetRowHeight(t *testing.T) {
f := NewFile()
// Test hidden row by set row height to 0
assert.NoError(t, f.SetRowHeight("Sheet1", 2, 0))
ht, err := f.GetRowHeight("Sheet1", 2)
assert.NoError(t, err)
assert.Empty(t, ht)
// Test unset custom row height
assert.NoError(t, f.SetRowHeight("Sheet1", 2, -1))
ht, err = f.GetRowHeight("Sheet1", 2)
assert.NoError(t, err)
assert.Equal(t, defaultRowHeight, ht)
// Test set row height with invalid height value
assert.Equal(t, ErrParameterInvalid, f.SetRowHeight("Sheet1", 2, -2))
}
func TestNumberFormats(t *testing.T) {
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
if !assert.NoError(t, err) {