col.go 3.1 KB

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