col.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. 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. // SetCellMetadata sets the CellMetadata related attributes
  42. // of a Col
  43. func (c *Col) SetCellMetadata(cellMetadata CellMetadata) {
  44. c.defaultCellType = &cellMetadata.cellType
  45. c.SetStreamStyle(cellMetadata.streamStyle)
  46. }
  47. // GetStyle returns the Style associated with a Col
  48. func (c *Col) GetStyle() *Style {
  49. return c.style
  50. }
  51. // SetStyle sets the style of a Col
  52. func (c *Col) SetStyle(style *Style) {
  53. c.style = style
  54. }
  55. // SetDataValidation set data validation with zero based start and end.
  56. // Set end to -1 for all rows.
  57. func (c *Col) SetDataValidation(dd *xlsxCellDataValidation, start, end int) {
  58. if end < 0 {
  59. end = Excel2006MaxRowIndex
  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 a zero basd start row.
  92. // This will apply to the rest of the rest of the column.
  93. func (c *Col) SetDataValidationWithStart(dd *xlsxCellDataValidation, start int) {
  94. c.SetDataValidation(dd, start, -1)
  95. }
  96. // SetStreamStyle sets the style and number format id to the ones specified in the given StreamStyle
  97. func (c *Col) SetStreamStyle(style StreamStyle) {
  98. c.style = style.style
  99. // TODO: `style.xNumFmtId` could be out of the range of the builtin map
  100. // returning "" which may not be a valid formatCode
  101. c.numFmt = builtInNumFmt[style.xNumFmtId]
  102. }
  103. func (c *Col) GetStreamStyle() StreamStyle {
  104. // TODO: Like `SetStreamStyle`, `numFmt` could be out of the range of the builtin inv map
  105. // returning 0 which maps to formatCode "general"
  106. return StreamStyle{builtInNumFmtInv[c.numFmt], c.style}
  107. }