MCPcopy
hub / github.com/qax-os/excelize / AddTable

Method AddTable

table.go:80–125  ·  view source on GitHub ↗

AddTable provides the method to add table in a worksheet by given worksheet name, range reference and format set. For example, create a table of A1:D5 on Sheet1: err := f.AddTable("Sheet1", &excelize.Table{Range: "A1:D5"}) Create a table of F2:H6 on Sheet2 with format set: disable := false err

(sheet string, table *Table)

Source from the content-addressed store, hash-verified

78// TableStyleMedium1 - TableStyleMedium28
79// TableStyleDark1 - TableStyleDark11
80func (f *File) AddTable(sheet string, table *Table) error {
81 options, err := parseTableOptions(table)
82 if err != nil {
83 return err
84 }
85 var exist bool
86 f.Pkg.Range(func(k, v interface{}) bool {
87 if strings.Contains(k.(string), "xl/tables/table") {
88 var t xlsxTable
89 if err := f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(v.([]byte)))).
90 Decode(&t); err != nil && err != io.EOF {
91 return true
92 }
93 if exist = t.Name == options.Name; exist {
94 return false
95 }
96 }
97 return true
98 })
99 if exist {
100 return ErrExistsTableName
101 }
102 // Coordinate conversion, convert C1:B3 to 2,0,1,2.
103 coordinates, err := rangeRefToCoordinates(options.Range)
104 if err != nil {
105 return err
106 }
107 // Correct table reference range, such correct C1:B3 to B1:C3.
108 _ = sortCoordinates(coordinates)
109 tableID := f.countTables() + 1
110 sheetRelationshipsTableXML := "../tables/table" + strconv.Itoa(tableID) + ".xml"
111 tableXML := strings.ReplaceAll(sheetRelationshipsTableXML, "..", "xl")
112 // Add first table for given sheet.
113 sheetXMLPath, _ := f.getSheetXMLPath(sheet)
114 sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(sheetXMLPath, "xl/worksheets/") + ".rels"
115 rID := f.addRels(sheetRels, SourceRelationshipTable, sheetRelationshipsTableXML, "")
116 if err = f.addSheetTable(sheet, rID); err != nil {
117 return err
118 }
119 f.addSheetNameSpace(sheet, SourceRelationship)
120 f.clearCalcCache()
121 if err = f.addTable(sheet, tableXML, coordinates[0], coordinates[1], coordinates[2], coordinates[3], tableID, options); err != nil {
122 return err
123 }
124 return f.addContentTypePart(tableID, "table")
125}
126
127// GetTables provides the method to get all tables in a worksheet by given
128// worksheet name.

Callers 10

TestGetTablesFunction · 0.95
TestDeleteTableFunction · 0.95
TestPivotTableDataRangeFunction · 0.95
TestGetPivotFieldsOrderFunction · 0.95
TestSlicerFunction · 0.95
TestDeleteSlicerFunction · 0.95
TestAdjustTableFunction · 0.95
TestAddTableFunction · 0.45
TestSetCellFormulaFunction · 0.45

Calls 13

xmlNewDecoderMethod · 0.95
countTablesMethod · 0.95
getSheetXMLPathMethod · 0.95
addRelsMethod · 0.95
addSheetTableMethod · 0.95
addSheetNameSpaceMethod · 0.95
clearCalcCacheMethod · 0.95
addTableMethod · 0.95
addContentTypePartMethod · 0.95
parseTableOptionsFunction · 0.85
rangeRefToCoordinatesFunction · 0.85

Tested by 10

TestGetTablesFunction · 0.76
TestDeleteTableFunction · 0.76
TestPivotTableDataRangeFunction · 0.76
TestGetPivotFieldsOrderFunction · 0.76
TestSlicerFunction · 0.76
TestDeleteSlicerFunction · 0.76
TestAdjustTableFunction · 0.76
TestAddTableFunction · 0.36
TestSetCellFormulaFunction · 0.36