table.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. vxAxis, hxAxis = hxAxis, vxAxis
  52. }
  53. if vyAxis < hyAxis {
  54. vyAxis, hyAxis = hyAxis, vyAxis
  55. }
  56. tableID := f.countTables() + 1
  57. sheetRelationshipsTableXML := "../tables/table" + strconv.Itoa(tableID) + ".xml"
  58. tableXML := strings.Replace(sheetRelationshipsTableXML, "..", "xl", -1)
  59. // Add first table for given sheet.
  60. rID := f.addSheetRelationships(sheet, SourceRelationshipTable, sheetRelationshipsTableXML, "")
  61. f.addSheetTable(sheet, rID)
  62. f.addTable(sheet, tableXML, hxAxis, hyAxis, vxAxis, vyAxis, tableID, formatSet)
  63. f.addContentTypePart(tableID, "table")
  64. }
  65. // countTables provides function to get table files count storage in the folder
  66. // xl/tables.
  67. func (f *File) countTables() int {
  68. count := 0
  69. for k := range f.XLSX {
  70. if strings.Contains(k, "xl/tables/table") {
  71. count++
  72. }
  73. }
  74. return count
  75. }
  76. // addSheetTable provides function to add tablePart element to
  77. // xl/worksheets/sheet%d.xml by given sheet name and relationship index.
  78. func (f *File) addSheetTable(sheet string, rID int) {
  79. xlsx := f.workSheetReader(sheet)
  80. table := &xlsxTablePart{
  81. RID: "rId" + strconv.Itoa(rID),
  82. }
  83. if xlsx.TableParts != nil {
  84. xlsx.TableParts.Count++
  85. xlsx.TableParts.TableParts = append(xlsx.TableParts.TableParts, table)
  86. } else {
  87. xlsx.TableParts = &xlsxTableParts{
  88. Count: 1,
  89. TableParts: []*xlsxTablePart{table},
  90. }
  91. }
  92. }
  93. // addTable provides function to add table by given sheet index, coordinate area
  94. // and format set.
  95. func (f *File) addTable(sheet, tableXML string, hxAxis, hyAxis, vxAxis, vyAxis, i int, formatSet *formatTable) {
  96. // Correct the minimum number of rows, the table at least two lines.
  97. if hyAxis == vyAxis {
  98. vyAxis++
  99. }
  100. // Correct table reference coordinate area, such correct C1:B3 to B1:C3.
  101. ref := toAlphaString(hxAxis+1) + strconv.Itoa(hyAxis+1) + ":" + toAlphaString(vxAxis+1) + strconv.Itoa(vyAxis+1)
  102. tableColumn := []*xlsxTableColumn{}
  103. idx := 0
  104. for i := hxAxis; i <= vxAxis; i++ {
  105. idx++
  106. cell := toAlphaString(i+1) + strconv.Itoa(hyAxis+1)
  107. name := f.GetCellValue(sheet, cell)
  108. if _, err := strconv.Atoi(name); err == nil {
  109. f.SetCellStr(sheet, cell, name)
  110. }
  111. if name == "" {
  112. name = "Column" + strconv.Itoa(idx)
  113. f.SetCellStr(sheet, cell, name)
  114. }
  115. tableColumn = append(tableColumn, &xlsxTableColumn{
  116. ID: idx,
  117. Name: name,
  118. })
  119. }
  120. name := "Table" + strconv.Itoa(i)
  121. t := xlsxTable{
  122. XMLNS: NameSpaceSpreadSheet,
  123. ID: i,
  124. Name: name,
  125. DisplayName: name,
  126. Ref: ref,
  127. AutoFilter: &xlsxAutoFilter{
  128. Ref: ref,
  129. },
  130. TableColumns: &xlsxTableColumns{
  131. Count: idx,
  132. TableColumn: tableColumn,
  133. },
  134. TableStyleInfo: &xlsxTableStyleInfo{
  135. Name: formatSet.TableStyle,
  136. ShowFirstColumn: formatSet.ShowFirstColumn,
  137. ShowLastColumn: formatSet.ShowLastColumn,
  138. ShowRowStripes: formatSet.ShowRowStripes,
  139. ShowColumnStripes: formatSet.ShowColumnStripes,
  140. },
  141. }
  142. table, _ := xml.Marshal(t)
  143. f.saveFileList(tableXML, string(table))
  144. }