col.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package xlsx
  2. // Default column width in excel
  3. const ColWidth = 9.5
  4. const Excel2006MaxRowCount = 1048576
  5. const Excel2006MaxRowIndex = Excel2006MaxRowCount - 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 zero based start and end.
  49. // Set end to -1 for all rows.
  50. func (c *Col) SetDataValidation(dd *xlsxCellDataValidation, start, end int) {
  51. if end < 0 {
  52. end = Excel2006MaxRowIndex
  53. }
  54. dd.minRow = start
  55. dd.maxRow = end
  56. tmpDD := make([]*xlsxCellDataValidation, 0)
  57. for _, item := range c.DataValidation {
  58. if item.maxRow < dd.minRow {
  59. tmpDD = append(tmpDD, item) //No intersection
  60. } else if item.minRow > dd.maxRow {
  61. tmpDD = append(tmpDD, item) //No intersection
  62. } else if dd.minRow <= item.minRow && dd.maxRow >= item.maxRow {
  63. continue //union , item can be ignored
  64. } else if dd.minRow >= item.minRow {
  65. //Split into three or two, Newly added object, intersect with the current object in the lower half
  66. tmpSplit := new(xlsxCellDataValidation)
  67. *tmpSplit = *item
  68. if dd.minRow > item.minRow { //header whetherneed to split
  69. item.maxRow = dd.minRow - 1
  70. tmpDD = append(tmpDD, item)
  71. }
  72. if dd.maxRow < tmpSplit.maxRow { //footer whetherneed to split
  73. tmpSplit.minRow = dd.maxRow + 1
  74. tmpDD = append(tmpDD, tmpSplit)
  75. }
  76. } else {
  77. item.minRow = dd.maxRow + 1
  78. tmpDD = append(tmpDD, item)
  79. }
  80. }
  81. tmpDD = append(tmpDD, dd)
  82. c.DataValidation = tmpDD
  83. }
  84. // SetDataValidationWithStart set data validation with a zero basd start row.
  85. // This will apply to the rest of the rest of the column.
  86. func (c *Col) SetDataValidationWithStart(dd *xlsxCellDataValidation, start int) {
  87. c.SetDataValidation(dd, start, -1)
  88. }
  89. // SetStreamStyle sets the style and number format id to the ones specified in the given StreamStyle
  90. func (c *Col) SetStreamStyle(style StreamStyle) {
  91. c.style = style.style
  92. c.numFmt = builtInNumFmt[style.xNumFmtId]
  93. }