From 07f2c6831a85214b2e914f274e746213e3f6d081 Mon Sep 17 00:00:00 2001 From: Abdelaziz-Ouhammou Date: Fri, 6 Oct 2023 19:05:50 +0300 Subject: [PATCH] Keep all cells value in the table range when deleting table (#1684) --- table.go | 12 ------------ table_test.go | 16 ++++++++++++---- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/table.go b/table.go index 1efcd41..b88db4c 100644 --- a/table.go +++ b/table.go @@ -196,18 +196,6 @@ func (f *File) DeleteTable(name string) error { if ws.TableParts.Count = len(ws.TableParts.TableParts); ws.TableParts.Count == 0 { ws.TableParts = nil } - // Delete cell value in the table header - coordinates, err := rangeRefToCoordinates(table.Range) - if err != nil { - return err - } - _ = sortCoordinates(coordinates) - for col := coordinates[0]; col <= coordinates[2]; col++ { - for row := coordinates[1]; row < coordinates[1]+1; row++ { - cell, _ := CoordinatesToCellName(col, row) - err = f.SetCellValue(sheet, cell, nil) - } - } return err } } diff --git a/table_test.go b/table_test.go index aa1980c..0b82b2a 100644 --- a/table_test.go +++ b/table_test.go @@ -122,11 +122,19 @@ func TestDeleteTable(t *testing.T) { f.Sheet.Delete("xl/worksheets/sheet1.xml") f.Pkg.Store("xl/worksheets/sheet1.xml", MacintoshCyrillicCharset) assert.EqualError(t, f.DeleteTable("Table1"), "XML syntax error on line 1: invalid UTF-8") - // Test delete table with invalid table range + // Test delete table without deleting table header f = NewFile() - assert.NoError(t, f.AddTable("Sheet1", &Table{Range: "A1:B4", Name: "Table1"})) - f.Pkg.Store("xl/tables/table1.xml", []byte("")) - assert.EqualError(t, f.DeleteTable("Table1"), ErrParameterInvalid.Error()) + assert.NoError(t, f.SetCellValue("Sheet1", "A1", "Date")) + assert.NoError(t, f.SetCellValue("Sheet1", "B1", "Values")) + assert.NoError(t, f.UpdateLinkedValue()) + assert.NoError(t, f.AddTable("Sheet1", &Table{Range: "A1:B2", Name: "Table1"})) + assert.NoError(t, f.DeleteTable("Table1")) + val, err := f.GetCellValue("Sheet1", "A1") + assert.NoError(t, err) + assert.Equal(t, "Date", val) + val, err = f.GetCellValue("Sheet1", "B1") + assert.NoError(t, err) + assert.Equal(t, "Values", val) } func TestSetTableHeader(t *testing.T) {