table.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. package excelize
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "fmt"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. )
  10. // parseFormatTableSet provides function to parse the format settings of the
  11. // table with default value.
  12. func parseFormatTableSet(formatSet string) *formatTable {
  13. format := formatTable{
  14. TableStyle: "",
  15. ShowRowStripes: true,
  16. }
  17. json.Unmarshal([]byte(formatSet), &format)
  18. return &format
  19. }
  20. // AddTable provides the method to add table in a worksheet by given sheet
  21. // index, coordinate area and format set. For example, create a table of A1:D5
  22. // on Sheet1:
  23. //
  24. // xlsx.AddTable("Sheet1", "A1", "D5", ``)
  25. //
  26. // Create a table of F2:H6 on Sheet2 with format set:
  27. //
  28. // xlsx.AddTable("Sheet2", "F2", "H6", `{"table_style":"TableStyleMedium2", "show_first_column":true,"show_last_column":true,"show_row_stripes":false,"show_column_stripes":true}`)
  29. //
  30. // Note that the table at least two lines include string type header. The two
  31. // chart coordinate areas can not have an intersection.
  32. //
  33. // table_style: The built-in table style names
  34. //
  35. // TableStyleLight1 - TableStyleLight21
  36. // TableStyleMedium1 - TableStyleMedium28
  37. // TableStyleDark1 - TableStyleDark11
  38. //
  39. func (f *File) AddTable(sheet, hcell, vcell, format string) {
  40. formatSet := parseFormatTableSet(format)
  41. hcell = strings.ToUpper(hcell)
  42. vcell = strings.ToUpper(vcell)
  43. // Coordinate conversion, convert C1:B3 to 2,0,1,2.
  44. hcol := string(strings.Map(letterOnlyMapF, hcell))
  45. hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
  46. hyAxis := hrow - 1
  47. hxAxis := titleToNumber(hcol)
  48. vcol := string(strings.Map(letterOnlyMapF, vcell))
  49. vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
  50. vyAxis := vrow - 1
  51. vxAxis := titleToNumber(vcol)
  52. if vxAxis < hxAxis {
  53. vxAxis, hxAxis = hxAxis, vxAxis
  54. }
  55. if vyAxis < hyAxis {
  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.addContentTypePart(tableID, "table")
  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. // parseAutoFilterSet provides function to parse the settings of the auto
  148. // filter.
  149. func parseAutoFilterSet(formatSet string) *formatAutoFilter {
  150. format := formatAutoFilter{}
  151. json.Unmarshal([]byte(formatSet), &format)
  152. return &format
  153. }
  154. // AutoFilter provides the method to add auto filter in a worksheet by given
  155. // sheet index, coordinate area and settings. An autofilter in Excel is a way of
  156. // filtering a 2D range of data based on some simple criteria. For example
  157. // applying an autofilter to a cell range A1:D4 in the worksheet 1:
  158. //
  159. // err = xlsx.AutoFilter("Sheet1", "A1", "D4", "")
  160. //
  161. // Filter data in an autofilter:
  162. //
  163. // err = xlsx.AutoFilter("Sheet1", "A1", "D4", `{"column":"B","expression":"x != blanks"}`)
  164. //
  165. // column defines the filter columns in a autofilter range based on simple criteria
  166. //
  167. // It isn't sufficient to just specify the filter condition. You must also hide any rows that don't match the filter condition. Rows are hidden using the HideRow() method. Excelize can't filter rows automatically since this isn't part of the file format.
  168. //
  169. // Setting a filter criteria for a column:
  170. //
  171. // expression defines the conditions, the following operators are available for setting the filter criteria:
  172. //
  173. // ==
  174. // !=
  175. // >
  176. // <
  177. // >=
  178. // <=
  179. // and
  180. // or
  181. //
  182. // An expression can comprise a single statement or two statements separated by the and and or operators. For example:
  183. //
  184. // x < 2000
  185. // x > 2000
  186. // x == 2000
  187. // x > 2000 and x < 5000
  188. // x == 2000 or x == 5000
  189. //
  190. // Filtering of blank or non-blank data can be achieved by using a value of Blanks or NonBlanks in the expression:
  191. //
  192. // x == Blanks
  193. // x == NonBlanks
  194. //
  195. // Excel also allows some simple string matching operations:
  196. //
  197. // x == b* // begins with b
  198. // x != b* // doesnt begin with b
  199. // x == *b // ends with b
  200. // x != *b // doesnt end with b
  201. // x == *b* // contains b
  202. // x != *b* // doesn't contains b
  203. //
  204. // You can also use '*' to match any character or number and '?' to match any single character or number. No other regular expression quantifier is supported by Excel's filters. Excel's regular expression characters can be escaped using '~'.
  205. //
  206. // The placeholder variable x in the above examples can be replaced by any simple string. The actual placeholder name is ignored internally so the following are all equivalent:
  207. //
  208. // x < 2000
  209. // col < 2000
  210. // Price < 2000
  211. //
  212. func (f *File) AutoFilter(sheet, hcell, vcell, format string) error {
  213. formatSet := parseAutoFilterSet(format)
  214. hcell = strings.ToUpper(hcell)
  215. vcell = strings.ToUpper(vcell)
  216. // Coordinate conversion, convert C1:B3 to 2,0,1,2.
  217. hcol := string(strings.Map(letterOnlyMapF, hcell))
  218. hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
  219. hyAxis := hrow - 1
  220. hxAxis := titleToNumber(hcol)
  221. vcol := string(strings.Map(letterOnlyMapF, vcell))
  222. vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
  223. vyAxis := vrow - 1
  224. vxAxis := titleToNumber(vcol)
  225. if vxAxis < hxAxis {
  226. hcell, vcell = vcell, hcell
  227. vxAxis, hxAxis = hxAxis, vxAxis
  228. }
  229. if vyAxis < hyAxis {
  230. hcell, vcell = vcell, hcell
  231. vyAxis, hyAxis = hyAxis, vyAxis
  232. }
  233. ref := toAlphaString(hxAxis+1) + strconv.Itoa(hyAxis+1) + ":" + toAlphaString(vxAxis+1) + strconv.Itoa(vyAxis+1)
  234. refRange := vxAxis - hxAxis
  235. err := f.autoFilter(sheet, ref, refRange, hxAxis, formatSet)
  236. return err
  237. }
  238. // autoFilter provides function to extract the tokens from the filter
  239. // expression. The tokens are mainly non-whitespace groups.
  240. func (f *File) autoFilter(sheet, ref string, refRange, hxAxis int, formatSet *formatAutoFilter) error {
  241. xlsx := f.workSheetReader(sheet)
  242. if xlsx.SheetPr != nil {
  243. xlsx.SheetPr.FilterMode = true
  244. }
  245. xlsx.SheetPr = &xlsxSheetPr{FilterMode: true}
  246. filter := &xlsxAutoFilter{
  247. Ref: ref,
  248. }
  249. xlsx.AutoFilter = filter
  250. if formatSet.Column == "" || formatSet.Expression == "" {
  251. return nil
  252. }
  253. col := titleToNumber(formatSet.Column)
  254. offset := col - hxAxis
  255. if offset < 0 || offset > refRange {
  256. return fmt.Errorf("Incorrect index of column '%s'", formatSet.Column)
  257. }
  258. filter.FilterColumn = &xlsxFilterColumn{
  259. ColID: offset,
  260. }
  261. re := regexp.MustCompile(`"(?:[^"]|"")*"|\S+`)
  262. token := re.FindAllString(formatSet.Expression, -1)
  263. if len(token) != 3 && len(token) != 7 {
  264. return fmt.Errorf("Incorrect number of tokens in criteria '%s'", formatSet.Expression)
  265. }
  266. expressions, tokens, err := f.parseFilterExpression(formatSet.Expression, token)
  267. if err != nil {
  268. return err
  269. }
  270. f.writeAutoFilter(filter, expressions, tokens)
  271. xlsx.AutoFilter = filter
  272. return nil
  273. }
  274. // writeAutoFilter provides funtion to check for single or double custom filters
  275. // as default filters and handle them accordingly.
  276. func (f *File) writeAutoFilter(filter *xlsxAutoFilter, exp []int, tokens []string) {
  277. if len(exp) == 1 && exp[0] == 2 {
  278. // Single equality.
  279. filters := []*xlsxFilter{}
  280. filters = append(filters, &xlsxFilter{Val: tokens[0]})
  281. filter.FilterColumn.Filters = &xlsxFilters{Filter: filters}
  282. } else if len(exp) == 3 && exp[0] == 2 && exp[1] == 1 && exp[2] == 2 {
  283. // Double equality with "or" operator.
  284. filters := []*xlsxFilter{}
  285. for _, v := range tokens {
  286. filters = append(filters, &xlsxFilter{Val: v})
  287. }
  288. filter.FilterColumn.Filters = &xlsxFilters{Filter: filters}
  289. } else {
  290. // Non default custom filter.
  291. expRel := map[int]int{0: 0, 1: 2}
  292. andRel := map[int]bool{0: true, 1: false}
  293. for k, v := range tokens {
  294. f.writeCustomFilter(filter, exp[expRel[k]], v)
  295. if k == 1 {
  296. filter.FilterColumn.CustomFilters.And = andRel[exp[k]]
  297. }
  298. }
  299. }
  300. }
  301. // writeCustomFilter provides function to write the <customFilter> element.
  302. func (f *File) writeCustomFilter(filter *xlsxAutoFilter, operator int, val string) {
  303. operators := map[int]string{
  304. 1: "lessThan",
  305. 2: "equal",
  306. 3: "lessThanOrEqual",
  307. 4: "greaterThan",
  308. 5: "notEqual",
  309. 6: "greaterThanOrEqual",
  310. 22: "equal",
  311. }
  312. customFilter := xlsxCustomFilter{
  313. Operator: operators[operator],
  314. Val: val,
  315. }
  316. if filter.FilterColumn.CustomFilters != nil {
  317. filter.FilterColumn.CustomFilters.CustomFilter = append(filter.FilterColumn.CustomFilters.CustomFilter, &customFilter)
  318. } else {
  319. customFilters := []*xlsxCustomFilter{}
  320. customFilters = append(customFilters, &customFilter)
  321. filter.FilterColumn.CustomFilters = &xlsxCustomFilters{CustomFilter: customFilters}
  322. }
  323. }
  324. // parseFilterExpression provides function to converts the tokens of a possibly
  325. // conditional expression into 1 or 2 sub expressions for further parsing.
  326. //
  327. // Examples:
  328. //
  329. // ('x', '==', 2000) -> exp1
  330. // ('x', '>', 2000, 'and', 'x', '<', 5000) -> exp1 and exp2
  331. //
  332. func (f *File) parseFilterExpression(expression string, tokens []string) ([]int, []string, error) {
  333. expressions := []int{}
  334. t := []string{}
  335. if len(tokens) == 7 {
  336. // The number of tokens will be either 3 (for 1 expression) or 7 (for 2
  337. // expressions).
  338. conditional := 0
  339. c := tokens[3]
  340. re, _ := regexp.Match(`(or|\|\|)`, []byte(c))
  341. if re {
  342. conditional = 1
  343. }
  344. expression1, token1, err := f.parseFilterTokens(expression, tokens[0:3])
  345. if err != nil {
  346. return expressions, t, err
  347. }
  348. expression2, token2, err := f.parseFilterTokens(expression, tokens[4:7])
  349. if err != nil {
  350. return expressions, t, err
  351. }
  352. expressions = []int{expression1[0], conditional, expression2[0]}
  353. t = []string{token1, token2}
  354. } else {
  355. exp, token, err := f.parseFilterTokens(expression, tokens)
  356. if err != nil {
  357. return expressions, t, err
  358. }
  359. expressions = exp
  360. t = []string{token}
  361. }
  362. return expressions, t, nil
  363. }
  364. // parseFilterTokens provides function to parse the 3 tokens of a filter
  365. // expression and return the operator and token.
  366. func (f *File) parseFilterTokens(expression string, tokens []string) ([]int, string, error) {
  367. operators := map[string]int{
  368. "==": 2,
  369. "=": 2,
  370. "=~": 2,
  371. "eq": 2,
  372. "!=": 5,
  373. "!~": 5,
  374. "ne": 5,
  375. "<>": 5,
  376. "<": 1,
  377. "<=": 3,
  378. ">": 4,
  379. ">=": 6,
  380. }
  381. operator, ok := operators[strings.ToLower(tokens[1])]
  382. if !ok {
  383. // Convert the operator from a number to a descriptive string.
  384. return []int{}, "", fmt.Errorf("Unknown operator: %s", tokens[1])
  385. }
  386. token := tokens[2]
  387. // Special handling for Blanks/NonBlanks.
  388. re, _ := regexp.Match("blanks|nonblanks", []byte(strings.ToLower(token)))
  389. if re {
  390. // Only allow Equals or NotEqual in this context.
  391. if operator != 2 && operator != 5 {
  392. return []int{operator}, token, fmt.Errorf("The operator '%s' in expression '%s' is not valid in relation to Blanks/NonBlanks'", tokens[1], expression)
  393. }
  394. token = strings.ToLower(token)
  395. // The operator should always be 2 (=) to flag a "simple" equality in
  396. // the binary record. Therefore we convert <> to =.
  397. if token == "blanks" {
  398. if operator == 5 {
  399. token = " "
  400. }
  401. } else {
  402. if operator == 5 {
  403. operator = 2
  404. token = "blanks"
  405. } else {
  406. operator = 5
  407. token = " "
  408. }
  409. }
  410. }
  411. // if the string token contains an Excel match character then change the
  412. // operator type to indicate a non "simple" equality.
  413. re, _ = regexp.Match("[*?]", []byte(token))
  414. if operator == 2 && re {
  415. operator = 22
  416. }
  417. return []int{operator}, token, nil
  418. }