diff --git a/adjust.go b/adjust.go index 583e2b979f..ab97c435b3 100644 --- a/adjust.go +++ b/adjust.go @@ -259,12 +259,12 @@ func (f *File) adjustCellRef(cellRef string, dir adjustDirection, num, offset in return "", err } if dir == columns { - if offset < 0 && coordinates[0] == coordinates[2] { + if offset < 0 && coordinates[0] == coordinates[2] && num == coordinates[0] { continue } coordinates = applyOffset(coordinates, 0, 2, MaxColumns) } else { - if offset < 0 && coordinates[1] == coordinates[3] { + if offset < 0 && coordinates[1] == coordinates[3] && num == coordinates[1] { continue } coordinates = applyOffset(coordinates, 1, 3, TotalRows) diff --git a/adjust_test.go b/adjust_test.go index 4f42e5145d..fccfdf22b1 100644 --- a/adjust_test.go +++ b/adjust_test.go @@ -1020,6 +1020,57 @@ func TestAdjustConditionalFormats(t *testing.T) { ws.(*xlsxWorksheet).ConditionalFormatting[0] = nil assert.NoError(t, f.RemoveCol("Sheet1", "B")) + + t.Run("for_remove_conditional_formats_column", func(t *testing.T) { + f := NewFile() + format := []ConditionalFormatOptions{{ + Type: "data_bar", + Criteria: "=", + MinType: "min", + MaxType: "max", + BarColor: "#638EC6", + }} + assert.NoError(t, f.SetConditionalFormat("Sheet1", "D2:D3", format)) + assert.NoError(t, f.SetConditionalFormat("Sheet1", "D5", format)) + assert.NoError(t, f.RemoveCol("Sheet1", "D")) + opts, err := f.GetConditionalFormats("Sheet1") + assert.NoError(t, err) + assert.Len(t, opts, 0) + }) + t.Run("for_remove_conditional_formats_row", func(t *testing.T) { + f := NewFile() + format := []ConditionalFormatOptions{{ + Type: "data_bar", + Criteria: "=", + MinType: "min", + MaxType: "max", + BarColor: "#638EC6", + }} + assert.NoError(t, f.SetConditionalFormat("Sheet1", "D2:E2", format)) + assert.NoError(t, f.SetConditionalFormat("Sheet1", "F2", format)) + assert.NoError(t, f.RemoveRow("Sheet1", 2)) + opts, err := f.GetConditionalFormats("Sheet1") + assert.NoError(t, err) + assert.Len(t, opts, 0) + }) + t.Run("for_adjust_conditional_formats_row", func(t *testing.T) { + f := NewFile() + format := []ConditionalFormatOptions{{ + Type: "data_bar", + Criteria: "=", + MinType: "min", + MaxType: "max", + BarColor: "#638EC6", + }} + assert.NoError(t, f.SetConditionalFormat("Sheet1", "D2:D3", format)) + assert.NoError(t, f.SetConditionalFormat("Sheet1", "D5", format)) + assert.NoError(t, f.RemoveRow("Sheet1", 1)) + opts, err := f.GetConditionalFormats("Sheet1") + assert.NoError(t, err) + assert.Len(t, opts, 2) + assert.Equal(t, format, opts["D1:D2"]) + assert.Equal(t, format, opts["D4:D4"]) + }) } func TestAdjustDataValidations(t *testing.T) { diff --git a/sheet.go b/sheet.go index 82c8f76cf0..ee66b90afe 100644 --- a/sheet.go +++ b/sheet.go @@ -773,6 +773,11 @@ func (f *File) SetSheetVisible(sheet string, visible bool, veryHidden ...bool) e return err } tabSelected := false + if ws.SheetViews == nil { + ws.SheetViews = &xlsxSheetViews{ + SheetView: []xlsxSheetView{{WorkbookViewID: 0}}, + } + } if len(ws.SheetViews.SheetView) > 0 { tabSelected = ws.SheetViews.SheetView[0].TabSelected } diff --git a/sheet_test.go b/sheet_test.go index 5c96efc1d2..1c4f55acf2 100644 --- a/sheet_test.go +++ b/sheet_test.go @@ -570,6 +570,18 @@ func TestSetSheetVisible(t *testing.T) { f.WorkBook = nil f.Pkg.Store(defaultXMLPathWorkbook, MacintoshCyrillicCharset) assert.EqualError(t, f.SetSheetVisible("Sheet1", false), "XML syntax error on line 1: invalid UTF-8") + + // Test set sheet visible with empty sheet views + f = NewFile() + _, err := f.NewSheet("Sheet2") + assert.NoError(t, err) + ws, ok := f.Sheet.Load("xl/worksheets/sheet2.xml") + assert.True(t, ok) + ws.(*xlsxWorksheet).SheetViews = nil + assert.NoError(t, f.SetSheetVisible("Sheet2", false)) + visible, err := f.GetSheetVisible("Sheet2") + assert.NoError(t, err) + assert.False(t, visible) } func TestGetSheetVisible(t *testing.T) {