Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}