From ce4f7a25c98b9e8fc13b32f0f88b4056fd8da7d7 Mon Sep 17 00:00:00 2001 From: Bayzet Tlyupov Date: Mon, 19 Dec 2022 04:28:43 +0300 Subject: [PATCH] This closes #1416, support set row outline level to stream (#1422) Co-authored-by: TlyupovBM --- stream.go | 16 +++++++++++++--- stream_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/stream.go b/stream.go index 09baa420..a575e761 100644 --- a/stream.go +++ b/stream.go @@ -310,9 +310,10 @@ type Cell struct { // RowOpts define the options for the set row, it can be used directly in // StreamWriter.SetRow to specify the style and properties of the row. type RowOpts struct { - Height float64 - Hidden bool - StyleID int + Height float64 + Hidden bool + StyleID int + OutlineLevel int } // marshalAttrs prepare attributes of the row. @@ -328,6 +329,10 @@ func (r *RowOpts) marshalAttrs() (strings.Builder, error) { err = ErrMaxRowHeight return attrs, err } + if r.OutlineLevel > 7 { + err = ErrOutlineLevel + return attrs, err + } if r.StyleID > 0 { attrs.WriteString(` s="`) attrs.WriteString(strconv.Itoa(r.StyleID)) @@ -338,6 +343,11 @@ func (r *RowOpts) marshalAttrs() (strings.Builder, error) { attrs.WriteString(strconv.FormatFloat(r.Height, 'f', -1, 64)) attrs.WriteString(`" customHeight="1"`) } + if r.OutlineLevel > 0 { + attrs.WriteString(` outlineLevel="`) + attrs.WriteString(strconv.Itoa(r.OutlineLevel)) + attrs.WriteString(`"`) + } if r.Hidden { attrs.WriteString(` hidden="1"`) } diff --git a/stream_test.go b/stream_test.go index bdf634ea..41f54151 100644 --- a/stream_test.go +++ b/stream_test.go @@ -358,3 +358,31 @@ func TestStreamSetCellValFunc(t *testing.T) { assert.NoError(t, sw.setCellValFunc(c, nil)) assert.NoError(t, sw.setCellValFunc(c, complex64(5+10i))) } + +func TestStreamWriterOutlineLevel(t *testing.T) { + file := NewFile() + streamWriter, err := file.NewStreamWriter("Sheet1") + assert.NoError(t, err) + + // Test set outlineLevel in row. + assert.NoError(t, streamWriter.SetRow("A1", nil, RowOpts{OutlineLevel: 1})) + assert.NoError(t, streamWriter.SetRow("A2", nil, RowOpts{OutlineLevel: 7})) + assert.ErrorIs(t, ErrOutlineLevel, streamWriter.SetRow("A3", nil, RowOpts{OutlineLevel: 8})) + + assert.NoError(t, streamWriter.Flush()) + // Save spreadsheet by the given path. + assert.NoError(t, file.SaveAs(filepath.Join("test", "TestStreamWriterSetRowOutlineLevel.xlsx"))) + + file, err = OpenFile(filepath.Join("test", "TestStreamWriterSetRowOutlineLevel.xlsx")) + assert.NoError(t, err) + level, err := file.GetRowOutlineLevel("Sheet1", 1) + assert.NoError(t, err) + assert.Equal(t, uint8(1), level) + level, err = file.GetRowOutlineLevel("Sheet1", 2) + assert.NoError(t, err) + assert.Equal(t, uint8(7), level) + level, err = file.GetRowOutlineLevel("Sheet1", 3) + assert.NoError(t, err) + assert.Equal(t, uint8(0), level) + assert.NoError(t, file.Close()) +}