table.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package excelize
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "strconv"
  6. "strings"
  7. )
  8. // parseFormatTableSet provides function to parse the format settings of the
  9. // table with default value.
  10. func parseFormatTableSet(formatSet string) *formatTable {
  11. format := formatTable{
  12. TableStyle: "",
  13. ShowRowStripes: true,
  14. }
  15. json.Unmarshal([]byte(formatSet), &format)
  16. return &format
  17. }
  18. // AddTable provides the method to add table in a worksheet by given sheet
  19. // index, coordinate area and format set. For example, create a table of A1:D5
  20. // on Sheet1:
  21. //
  22. // xlsx.AddTable("Sheet1", "A1", "D5", ``)
  23. //
  24. // Create a table of F2:H6 on Sheet2 with format set:
  25. //
  26. // xlsx.AddTable("Sheet2", "F2", "H6", `{"table_style":"TableStyleMedium2", "show_first_column":true,"show_last_column":true,"show_row_stripes":false,"show_column_stripes":true}`)
  27. //
  28. // Note that the table at least two lines include string type header. The two
  29. // chart coordinate areas can not have an intersection.
  30. //
  31. // table_style: The built-in table style names
  32. //
  33. // TableStyleLight1 - TableStyleLight21
  34. // TableStyleMedium1 - TableStyleMedium28
  35. // TableStyleDark1 - TableStyleDark11
  36. //
  37. func (f *File) AddTable(sheet, hcell, vcell, format string) {
  38. formatSet := parseFormatTableSet(format)
  39. hcell = strings.ToUpper(hcell)
  40. vcell = strings.ToUpper(vcell)
  41. // Coordinate conversion, convert C1:B3 to 2,0,1,2.
  42. hcol := string(strings.Map(letterOnlyMapF, hcell))
  43. hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
  44. hyAxis := hrow - 1
  45. hxAxis := titleToNumber(hcol)
  46. vcol := string(strings.Map(letterOnlyMapF, vcell))
  47. vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
  48. vyAxis := vrow - 1
  49. vxAxis := titleToNumber(vcol)
  50. if vxAxis < hxAxis {
  51. hcell, vcell = vcell, hcell
  52. vxAxis, hxAxis = hxAxis, vxAxis
  53. }
  54. if vyAxis < hyAxis {
  55. hcell, vcell = vcell, hcell
  56. vyAxis, hyAxis = hyAxis, vyAxis
  57. }
  58. tableID := f.countTables() + 1
  59. sheetRelationshipsTableXML := "../tables/table" + strconv.Itoa(tableID) + ".xml"
  60. tableXML := strings.Replace(sheetRelationshipsTableXML, "..", "xl", -1)
  61. // Add first table for given sheet.
  62. rID := f.addSheetRelationships(sheet, SourceRelationshipTable, sheetRelationshipsTableXML, "")
  63. f.addSheetTable(sheet, rID)
  64. f.addTable(sheet, tableXML, hxAxis, hyAxis, vxAxis, vyAxis, tableID, formatSet)
  65. f.addTableContentTypePart(tableID)
  66. }
  67. // countTables provides function to get table files count storage in the folder
  68. // xl/tables.
  69. func (f *File) countTables() int {
  70. count := 0
  71. for k := range f.XLSX {
  72. if strings.Contains(k, "xl/tables/table") {
  73. count++
  74. }
  75. }
  76. return count
  77. }
  78. // addSheetTable provides function to add tablePart element to
  79. // xl/worksheets/sheet%d.xml by given sheet name and relationship index.
  80. func (f *File) addSheetTable(sheet string, rID int) {
  81. xlsx := f.workSheetReader(sheet)
  82. table := &xlsxTablePart{
  83. RID: "rId" + strconv.Itoa(rID),
  84. }
  85. if xlsx.TableParts != nil {
  86. xlsx.TableParts.Count++
  87. xlsx.TableParts.TableParts = append(xlsx.TableParts.TableParts, table)
  88. } else {
  89. xlsx.TableParts = &xlsxTableParts{
  90. Count: 1,
  91. TableParts: []*xlsxTablePart{table},
  92. }
  93. }
  94. }
  95. // addTable provides function to add table by given sheet index, coordinate area
  96. // and format set.
  97. func (f *File) addTable(sheet, tableXML string, hxAxis, hyAxis, vxAxis, vyAxis, i int, formatSet *formatTable) {
  98. // Correct the minimum number of rows, the table at least two lines.
  99. if hyAxis == vyAxis {
  100. vyAxis++
  101. }
  102. // Correct table reference coordinate area, such correct C1:B3 to B1:C3.
  103. ref := toAlphaString(hxAxis+1) + strconv.Itoa(hyAxis+1) + ":" + toAlphaString(vxAxis+1) + strconv.Itoa(vyAxis+1)
  104. tableColumn := []*xlsxTableColumn{}
  105. idx := 0
  106. for i := hxAxis; i <= vxAxis; i++ {
  107. idx++
  108. cell := toAlphaString(i+1) + strconv.Itoa(hyAxis+1)
  109. name := f.GetCellValue(sheet, cell)
  110. if _, err := strconv.Atoi(name); err == nil {
  111. f.SetCellStr(sheet, cell, name)
  112. }
  113. if name == "" {
  114. name = "Column" + strconv.Itoa(idx)
  115. f.SetCellStr(sheet, cell, name)
  116. }
  117. tableColumn = append(tableColumn, &xlsxTableColumn{
  118. ID: idx,
  119. Name: name,
  120. })
  121. }
  122. name := "Table" + strconv.Itoa(i)
  123. t := xlsxTable{
  124. XMLNS: NameSpaceSpreadSheet,
  125. ID: i,
  126. Name: name,
  127. DisplayName: name,
  128. Ref: ref,
  129. AutoFilter: &xlsxAutoFilter{
  130. Ref: ref,
  131. },
  132. TableColumns: &xlsxTableColumns{
  133. Count: idx,
  134. TableColumn: tableColumn,
  135. },
  136. TableStyleInfo: &xlsxTableStyleInfo{
  137. Name: formatSet.TableStyle,
  138. ShowFirstColumn: formatSet.ShowFirstColumn,
  139. ShowLastColumn: formatSet.ShowLastColumn,
  140. ShowRowStripes: formatSet.ShowRowStripes,
  141. ShowColumnStripes: formatSet.ShowColumnStripes,
  142. },
  143. }
  144. table, _ := xml.Marshal(t)
  145. f.saveFileList(tableXML, string(table))
  146. }
  147. // addTableContentTypePart provides function to add image part relationships
  148. // in the file [Content_Types].xml by given drawing index.
  149. func (f *File) addTableContentTypePart(index int) {
  150. f.setContentTypePartImageExtensions()
  151. content := f.contentTypesReader()
  152. for _, v := range content.Overrides {
  153. if v.PartName == "/xl/tables/table"+strconv.Itoa(index)+".xml" {
  154. return
  155. }
  156. }
  157. content.Overrides = append(content.Overrides, xlsxOverride{
  158. PartName: "/xl/tables/table" + strconv.Itoa(index) + ".xml",
  159. ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml",
  160. })
  161. }