未验证 提交 18a160c5 编写于 作者: 天爱有情 提交者: GitHub

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
上级 a16182e0
......@@ -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
......
......@@ -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) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册