diff --git a/table.go b/table.go index aec7b26038..4fa4b63459 100644 --- a/table.go +++ b/table.go @@ -215,6 +215,62 @@ func (f *File) getTables() (map[string][]Table, error) { return tables, nil } +// ResizeTable provides a function to resize table by given table name and +// range reference. +func (f *File) ResizeTable(name, rangeRef string) error { + if err := checkDefinedName(name); err != nil { + return err + } + for _, sheet := range f.GetSheetList() { + tables, err := f.GetTables(sheet) + if err != nil { + return err + } + for _, table := range tables { + if table.Name != name { + continue + } + // Get XML content of the table + content, ok := f.Pkg.Load(table.tableXML) + if !ok { + continue + } + // Convert xml content to xlsxTable + var tbl xlsxTable + if err := f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(content.([]byte)))).Decode(&tbl); err != nil && err != io.EOF { + return err + } + // Convert reference range to coordinates + coordinates, err := rangeRefToCoordinates(rangeRef) + if err != nil { + return err + } + // Correct reference range, such correct C1:B3 to B1:C3. + _ = sortCoordinates(coordinates) + // Convert back coordinates to reference range + ref, _ := coordinatesToRangeRef(coordinates, true) + // Update the table with the new range + tbl.Ref = ref + tbl.AutoFilter.Ref = ref + table.Range = ref + // Update the header + hideHeaderRow := table.ShowHeaderRow != nil && !*table.ShowHeaderRow + if err = f.setTableColumns(sheet, !hideHeaderRow, coordinates[0], coordinates[1], coordinates[2], &tbl); err != nil { + return err + } + // Convert back xlsxTable to xml content + tableXML, err := xml.Marshal(tbl) + if err != nil { + return err + } + // Save the xml content of the table + f.saveFileList(table.tableXML, tableXML) + return err + } + } + return newNoExistTableError(name) +} + // countTables provides a function to get table files count storage in the // folder xl/tables. func (f *File) countTables() int { diff --git a/table_test.go b/table_test.go index 8abfe1fb40..7161979f55 100644 --- a/table_test.go +++ b/table_test.go @@ -224,3 +224,13 @@ func TestParseFilterTokens(t *testing.T) { _, _, err = f.parseFilterTokens("", []string{"", "<", "x != blanks"}) assert.Equal(t, newInvalidAutoFilterOperatorError("<", ""), err) } + +func TestResizeTable(t *testing.T) { + f := NewFile() + assert.NoError(t, f.AddTable("Sheet1", &Table{Range: "A1:B4", Name: "Table1"})) + assert.NoError(t, f.ResizeTable("Table1", "A1:C5")) + tables, err := f.GetTables("Sheet1") + assert.NoError(t, err) + assert.Len(t, tables, 1) + assert.Equal(t, tables[0].Range, "$A$1:$C$5") +}