col.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package xlsx
  2. // Default column width in excel
  3. const ColWidth = 9.5
  4. const Excel2006MaxRowIndex = 1048576
  5. const Excel2006MinRowIndex = 1
  6. type Col struct {
  7. Min int
  8. Max int
  9. Hidden bool
  10. Width float64
  11. Collapsed bool
  12. OutlineLevel uint8
  13. numFmt string
  14. parsedNumFmt *parsedNumberFormat
  15. style *Style
  16. DataValidation []*xlsxCellDataValidation
  17. }
  18. // SetType will set the format string of a column based on the type that you want to set it to.
  19. // This function does not really make a lot of sense.
  20. func (c *Col) SetType(cellType CellType) {
  21. switch cellType {
  22. case CellTypeString:
  23. c.numFmt = builtInNumFmt[builtInNumFmtIndex_STRING]
  24. case CellTypeNumeric:
  25. c.numFmt = builtInNumFmt[builtInNumFmtIndex_INT]
  26. case CellTypeBool:
  27. c.numFmt = builtInNumFmt[builtInNumFmtIndex_GENERAL] //TEMP
  28. case CellTypeInline:
  29. c.numFmt = builtInNumFmt[builtInNumFmtIndex_STRING]
  30. case CellTypeError:
  31. c.numFmt = builtInNumFmt[builtInNumFmtIndex_GENERAL] //TEMP
  32. case CellTypeDate:
  33. // Cells that are stored as dates are not properly supported in this library.
  34. // They should instead be stored as a Numeric with a date format.
  35. c.numFmt = builtInNumFmt[builtInNumFmtIndex_GENERAL]
  36. case CellTypeStringFormula:
  37. c.numFmt = builtInNumFmt[builtInNumFmtIndex_STRING]
  38. }
  39. }
  40. // GetStyle returns the Style associated with a Col
  41. func (c *Col) GetStyle() *Style {
  42. return c.style
  43. }
  44. // SetStyle sets the style of a Col
  45. func (c *Col) SetStyle(style *Style) {
  46. c.style = style
  47. }
  48. // SetDataValidation set data validation with start,end ; start or end = 0 equal all column
  49. func (c *Col) SetDataValidation(dd *xlsxCellDataValidation, start, end int) {
  50. if 0 == start {
  51. start = Excel2006MinRowIndex
  52. } else {
  53. start = start + 1
  54. }
  55. if 0 == end {
  56. end = Excel2006MinRowIndex
  57. } else if end < Excel2006MaxRowIndex {
  58. end = end + 1
  59. }
  60. dd.minRow = start
  61. dd.maxRow = end
  62. tmpDD := make([]*xlsxCellDataValidation, 0)
  63. for _, item := range c.DataValidation {
  64. if item.maxRow < dd.minRow {
  65. tmpDD = append(tmpDD, item) //No intersection
  66. } else if item.minRow > dd.maxRow {
  67. tmpDD = append(tmpDD, item) //No intersection
  68. } else if dd.minRow <= item.minRow && dd.maxRow >= item.maxRow {
  69. continue //union , item can be ignored
  70. } else if dd.minRow >= item.minRow {
  71. //Split into three or two, Newly added object, intersect with the current object in the lower half
  72. tmpSplit := new(xlsxCellDataValidation)
  73. *tmpSplit = *item
  74. if dd.minRow > item.minRow { //header whetherneed to split
  75. item.maxRow = dd.minRow - 1
  76. tmpDD = append(tmpDD, item)
  77. }
  78. if dd.maxRow < tmpSplit.maxRow { //footer whetherneed to split
  79. tmpSplit.minRow = dd.maxRow + 1
  80. tmpDD = append(tmpDD, tmpSplit)
  81. }
  82. } else {
  83. item.minRow = dd.maxRow + 1
  84. tmpDD = append(tmpDD, item)
  85. }
  86. }
  87. tmpDD = append(tmpDD, dd)
  88. c.DataValidation = tmpDD
  89. }
  90. // SetDataValidationWithStart set data validation with start
  91. func (c *Col) SetDataValidationWithStart(dd *xlsxCellDataValidation, start int) {
  92. //2006 excel all row 1048576
  93. c.SetDataValidation(dd, start, Excel2006MaxRowIndex)
  94. }