table.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. vxAxis, hxAxis = hxAxis, vxAxis
  227. }
  228. if vyAxis < hyAxis {
  229. vyAxis, hyAxis = hyAxis, vyAxis
  230. }
  231. ref := toAlphaString(hxAxis+1) + strconv.Itoa(hyAxis+1) + ":" + toAlphaString(vxAxis+1) + strconv.Itoa(vyAxis+1)
  232. refRange := vxAxis - hxAxis
  233. err := f.autoFilter(sheet, ref, refRange, hxAxis, formatSet)
  234. return err
  235. }
  236. // autoFilter provides function to extract the tokens from the filter
  237. // expression. The tokens are mainly non-whitespace groups.
  238. func (f *File) autoFilter(sheet, ref string, refRange, hxAxis int, formatSet *formatAutoFilter) error {
  239. xlsx := f.workSheetReader(sheet)
  240. if xlsx.SheetPr != nil {
  241. xlsx.SheetPr.FilterMode = true
  242. }
  243. xlsx.SheetPr = &xlsxSheetPr{FilterMode: true}
  244. filter := &xlsxAutoFilter{
  245. Ref: ref,
  246. }
  247. xlsx.AutoFilter = filter
  248. if formatSet.Column == "" || formatSet.Expression == "" {
  249. return nil
  250. }
  251. col := titleToNumber(formatSet.Column)
  252. offset := col - hxAxis
  253. if offset < 0 || offset > refRange {
  254. return fmt.Errorf("Incorrect index of column '%s'", formatSet.Column)
  255. }
  256. filter.FilterColumn = &xlsxFilterColumn{
  257. ColID: offset,
  258. }
  259. re := regexp.MustCompile(`"(?:[^"]|"")*"|\S+`)
  260. token := re.FindAllString(formatSet.Expression, -1)
  261. if len(token) != 3 && len(token) != 7 {
  262. return fmt.Errorf("Incorrect number of tokens in criteria '%s'", formatSet.Expression)
  263. }
  264. expressions, tokens, err := f.parseFilterExpression(formatSet.Expression, token)
  265. if err != nil {
  266. return err
  267. }
  268. f.writeAutoFilter(filter, expressions, tokens)
  269. xlsx.AutoFilter = filter
  270. return nil
  271. }
  272. // writeAutoFilter provides funtion to check for single or double custom filters
  273. // as default filters and handle them accordingly.
  274. func (f *File) writeAutoFilter(filter *xlsxAutoFilter, exp []int, tokens []string) {
  275. if len(exp) == 1 && exp[0] == 2 {
  276. // Single equality.
  277. filters := []*xlsxFilter{}
  278. filters = append(filters, &xlsxFilter{Val: tokens[0]})
  279. filter.FilterColumn.Filters = &xlsxFilters{Filter: filters}
  280. } else if len(exp) == 3 && exp[0] == 2 && exp[1] == 1 && exp[2] == 2 {
  281. // Double equality with "or" operator.
  282. filters := []*xlsxFilter{}
  283. for _, v := range tokens {
  284. filters = append(filters, &xlsxFilter{Val: v})
  285. }
  286. filter.FilterColumn.Filters = &xlsxFilters{Filter: filters}
  287. } else {
  288. // Non default custom filter.
  289. expRel := map[int]int{0: 0, 1: 2}
  290. andRel := map[int]bool{0: true, 1: false}
  291. for k, v := range tokens {
  292. f.writeCustomFilter(filter, exp[expRel[k]], v)
  293. if k == 1 {
  294. filter.FilterColumn.CustomFilters.And = andRel[exp[k]]
  295. }
  296. }
  297. }
  298. }
  299. // writeCustomFilter provides function to write the <customFilter> element.
  300. func (f *File) writeCustomFilter(filter *xlsxAutoFilter, operator int, val string) {
  301. operators := map[int]string{
  302. 1: "lessThan",
  303. 2: "equal",
  304. 3: "lessThanOrEqual",
  305. 4: "greaterThan",
  306. 5: "notEqual",
  307. 6: "greaterThanOrEqual",
  308. 22: "equal",
  309. }
  310. customFilter := xlsxCustomFilter{
  311. Operator: operators[operator],
  312. Val: val,
  313. }
  314. if filter.FilterColumn.CustomFilters != nil {
  315. filter.FilterColumn.CustomFilters.CustomFilter = append(filter.FilterColumn.CustomFilters.CustomFilter, &customFilter)
  316. } else {
  317. customFilters := []*xlsxCustomFilter{}
  318. customFilters = append(customFilters, &customFilter)
  319. filter.FilterColumn.CustomFilters = &xlsxCustomFilters{CustomFilter: customFilters}
  320. }
  321. }
  322. // parseFilterExpression provides function to converts the tokens of a possibly
  323. // conditional expression into 1 or 2 sub expressions for further parsing.
  324. //
  325. // Examples:
  326. //
  327. // ('x', '==', 2000) -> exp1
  328. // ('x', '>', 2000, 'and', 'x', '<', 5000) -> exp1 and exp2
  329. //
  330. func (f *File) parseFilterExpression(expression string, tokens []string) ([]int, []string, error) {
  331. expressions := []int{}
  332. t := []string{}
  333. if len(tokens) == 7 {
  334. // The number of tokens will be either 3 (for 1 expression) or 7 (for 2
  335. // expressions).
  336. conditional := 0
  337. c := tokens[3]
  338. re, _ := regexp.Match(`(or|\|\|)`, []byte(c))
  339. if re {
  340. conditional = 1
  341. }
  342. expression1, token1, err := f.parseFilterTokens(expression, tokens[0:3])
  343. if err != nil {
  344. return expressions, t, err
  345. }
  346. expression2, token2, err := f.parseFilterTokens(expression, tokens[4:7])
  347. if err != nil {
  348. return expressions, t, err
  349. }
  350. expressions = []int{expression1[0], conditional, expression2[0]}
  351. t = []string{token1, token2}
  352. } else {
  353. exp, token, err := f.parseFilterTokens(expression, tokens)
  354. if err != nil {
  355. return expressions, t, err
  356. }
  357. expressions = exp
  358. t = []string{token}
  359. }
  360. return expressions, t, nil
  361. }
  362. // parseFilterTokens provides function to parse the 3 tokens of a filter
  363. // expression and return the operator and token.
  364. func (f *File) parseFilterTokens(expression string, tokens []string) ([]int, string, error) {
  365. operators := map[string]int{
  366. "==": 2,
  367. "=": 2,
  368. "=~": 2,
  369. "eq": 2,
  370. "!=": 5,
  371. "!~": 5,
  372. "ne": 5,
  373. "<>": 5,
  374. "<": 1,
  375. "<=": 3,
  376. ">": 4,
  377. ">=": 6,
  378. }
  379. operator, ok := operators[strings.ToLower(tokens[1])]
  380. if !ok {
  381. // Convert the operator from a number to a descriptive string.
  382. return []int{}, "", fmt.Errorf("Unknown operator: %s", tokens[1])
  383. }
  384. token := tokens[2]
  385. // Special handling for Blanks/NonBlanks.
  386. re, _ := regexp.Match("blanks|nonblanks", []byte(strings.ToLower(token)))
  387. if re {
  388. // Only allow Equals or NotEqual in this context.
  389. if operator != 2 && operator != 5 {
  390. return []int{operator}, token, fmt.Errorf("The operator '%s' in expression '%s' is not valid in relation to Blanks/NonBlanks'", tokens[1], expression)
  391. }
  392. token = strings.ToLower(token)
  393. // The operator should always be 2 (=) to flag a "simple" equality in
  394. // the binary record. Therefore we convert <> to =.
  395. if token == "blanks" {
  396. if operator == 5 {
  397. token = " "
  398. }
  399. } else {
  400. if operator == 5 {
  401. operator = 2
  402. token = "blanks"
  403. } else {
  404. operator = 5
  405. token = " "
  406. }
  407. }
  408. }
  409. // if the string token contains an Excel match character then change the
  410. // operator type to indicate a non "simple" equality.
  411. re, _ = regexp.Match("[*?]", []byte(token))
  412. if operator == 2 && re {
  413. operator = 22
  414. }
  415. return []int{operator}, token, nil
  416. }