col.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. defaultCellType *CellType
  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. c.defaultCellType = &cellType
  23. switch cellType {
  24. case CellTypeString:
  25. c.numFmt = builtInNumFmt[builtInNumFmtIndex_STRING]
  26. case CellTypeNumeric:
  27. c.numFmt = builtInNumFmt[builtInNumFmtIndex_INT]
  28. case CellTypeBool:
  29. c.numFmt = builtInNumFmt[builtInNumFmtIndex_GENERAL] //TEMP
  30. case CellTypeInline:
  31. c.numFmt = builtInNumFmt[builtInNumFmtIndex_STRING]
  32. case CellTypeError:
  33. c.numFmt = builtInNumFmt[builtInNumFmtIndex_GENERAL] //TEMP
  34. case CellTypeDate:
  35. // Cells that are stored as dates are not properly supported in this library.
  36. // They should instead be stored as a Numeric with a date format.
  37. c.numFmt = builtInNumFmt[builtInNumFmtIndex_GENERAL]
  38. case CellTypeStringFormula:
  39. c.numFmt = builtInNumFmt[builtInNumFmtIndex_STRING]
  40. }
  41. }
  42. // GetStyle returns the Style associated with a Col
  43. func (c *Col) GetStyle() *Style {
  44. return c.style
  45. }
  46. // SetStyle sets the style of a Col
  47. func (c *Col) SetStyle(style *Style) {
  48. c.style = style
  49. }
  50. // SetDataValidation set data validation with zero based start and end.
  51. // Set end to -1 for all rows.
  52. func (c *Col) SetDataValidation(dd *xlsxCellDataValidation, start, end int) {
  53. if end < 0 {
  54. end = Excel2006MaxRowIndex
  55. }
  56. dd.minRow = start
  57. dd.maxRow = end
  58. tmpDD := make([]*xlsxCellDataValidation, 0)
  59. for _, item := range c.DataValidation {
  60. if item.maxRow < dd.minRow {
  61. tmpDD = append(tmpDD, item) //No intersection
  62. } else if item.minRow > dd.maxRow {
  63. tmpDD = append(tmpDD, item) //No intersection
  64. } else if dd.minRow <= item.minRow && dd.maxRow >= item.maxRow {
  65. continue //union , item can be ignored
  66. } else if dd.minRow >= item.minRow {
  67. //Split into three or two, Newly added object, intersect with the current object in the lower half
  68. tmpSplit := new(xlsxCellDataValidation)
  69. *tmpSplit = *item
  70. if dd.minRow > item.minRow { //header whetherneed to split
  71. item.maxRow = dd.minRow - 1
  72. tmpDD = append(tmpDD, item)
  73. }
  74. if dd.maxRow < tmpSplit.maxRow { //footer whetherneed to split
  75. tmpSplit.minRow = dd.maxRow + 1
  76. tmpDD = append(tmpDD, tmpSplit)
  77. }
  78. } else {
  79. item.minRow = dd.maxRow + 1
  80. tmpDD = append(tmpDD, item)
  81. }
  82. }
  83. tmpDD = append(tmpDD, dd)
  84. c.DataValidation = tmpDD
  85. }
  86. // SetDataValidationWithStart set data validation with a zero basd start row.
  87. // This will apply to the rest of the rest of the column.
  88. func (c *Col) SetDataValidationWithStart(dd *xlsxCellDataValidation, start int) {
  89. c.SetDataValidation(dd, start, -1)
  90. }
  91. // SetStreamStyle sets the style and number format id to the ones specified in the given StreamStyle
  92. func (c *Col) SetStreamStyle(style StreamStyle) {
  93. c.style = style.style
  94. c.numFmt = builtInNumFmt[style.xNumFmtId]
  95. }