diff options
Diffstat (limited to 'table.go')
-rw-r--r-- | table.go | 66 |
1 files changed, 44 insertions, 22 deletions
@@ -1,11 +1,11 @@ -// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of +// Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of // this source code is governed by a BSD-style license that can be found in // the LICENSE file. // // Package excelize providing a set of functions that allow you to write to // and read from XLSX files. Support reads and writes XLSX file generated by // Microsoft Excelâ„¢ 2007 and later. Support save file without losing original -// charts of XLSX. This library needs Go version 1.8 or later. +// charts of XLSX. This library needs Go version 1.10 or later. package excelize @@ -39,8 +39,10 @@ func parseFormatTableSet(formatSet string) (*formatTable, error) { // // err := f.AddTable("Sheet2", "F2", "H6", `{"table_name":"table","table_style":"TableStyleMedium2", "show_first_column":true,"show_last_column":true,"show_row_stripes":false,"show_column_stripes":true}`) // -// Note that the table at least two lines include string type header. Multiple -// tables coordinate areas can't have an intersection. +// Note that the table must be at least two lines including the header. The +// header cells must contain strings and must be unique, and must set the +// header row data of the table before calling the AddTable function. Multiple +// tables coordinate areas that can't have an intersection. // // table_name: The name of the table, in the same worksheet name of the table should be unique // @@ -77,7 +79,8 @@ func (f *File) AddTable(sheet, hcell, vcell, format string) error { sheetRelationshipsTableXML := "../tables/table" + strconv.Itoa(tableID) + ".xml" tableXML := strings.Replace(sheetRelationshipsTableXML, "..", "xl", -1) // Add first table for given sheet. - rID := f.addSheetRelationships(sheet, SourceRelationshipTable, sheetRelationshipsTableXML, "") + sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/worksheets/") + ".rels" + rID := f.addRels(sheetRels, SourceRelationshipTable, sheetRelationshipsTableXML, "") f.addSheetTable(sheet, rID) err = f.addTable(sheet, tableXML, hcol, hrow, vcol, vrow, tableID, formatSet) if err != nil { @@ -115,35 +118,30 @@ func (f *File) addSheetTable(sheet string, rID int) { // addTable provides a function to add table by given worksheet name, // coordinate area and format set. -func (f *File) addTable(sheet, tableXML string, hcol, hrow, vcol, vrow, i int, formatSet *formatTable) error { +func (f *File) addTable(sheet, tableXML string, x1, y1, x2, y2, i int, formatSet *formatTable) error { // Correct the minimum number of rows, the table at least two lines. - if hrow == vrow { - vrow++ + if y1 == y2 { + y2++ } // Correct table reference coordinate area, such correct C1:B3 to B1:C3. - hcell, err := CoordinatesToCellName(hcol, hrow) + ref, err := f.coordinatesToAreaRef([]int{x1, y1, x2, y2}) if err != nil { return err } - vcell, err := CoordinatesToCellName(vcol, vrow) - if err != nil { - return err - } - ref := hcell + ":" + vcell var tableColumn []*xlsxTableColumn idx := 0 - for i := hcol; i <= vcol; i++ { + for i := x1; i <= x2; i++ { idx++ - cell, err := CoordinatesToCellName(i, hrow) + cell, err := CoordinatesToCellName(i, y1) if err != nil { return err } name, _ := f.GetCellValue(sheet, cell) if _, err := strconv.Atoi(name); err == nil { - f.SetCellStr(sheet, cell, name) + _ = f.SetCellStr(sheet, cell, name) } if name == "" { name = "Column" + strconv.Itoa(idx) @@ -283,15 +281,39 @@ func (f *File) AutoFilter(sheet, hcell, vcell, format string) error { formatSet, _ := parseAutoFilterSet(format) var cellStart, cellEnd string - cellStart, err = CoordinatesToCellName(hcol, hrow) - if err != nil { + if cellStart, err = CoordinatesToCellName(hcol, hrow); err != nil { return err } - cellEnd, err = CoordinatesToCellName(vcol, vrow) - if err != nil { + if cellEnd, err = CoordinatesToCellName(vcol, vrow); err != nil { return err } - ref := cellStart + ":" + cellEnd + ref, filterDB := cellStart+":"+cellEnd, "_xlnm._FilterDatabase" + wb := f.workbookReader() + sheetID := f.GetSheetIndex(sheet) + filterRange := fmt.Sprintf("%s!%s", sheet, ref) + d := xlsxDefinedName{ + Name: filterDB, + Hidden: true, + LocalSheetID: intPtr(sheetID), + Data: filterRange, + } + if wb.DefinedNames == nil { + wb.DefinedNames = &xlsxDefinedNames{ + DefinedName: []xlsxDefinedName{d}, + } + } else { + var definedNameExists bool + for idx := range wb.DefinedNames.DefinedName { + definedName := wb.DefinedNames.DefinedName[idx] + if definedName.Name == filterDB && *definedName.LocalSheetID == sheetID && definedName.Hidden { + wb.DefinedNames.DefinedName[idx].Data = filterRange + definedNameExists = true + } + } + if !definedNameExists { + wb.DefinedNames.DefinedName = append(wb.DefinedNames.DefinedName, d) + } + } refRange := vcol - hcol return f.autoFilter(sheet, ref, refRange, hcol, formatSet) } |