datavalidation.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package xlsx
  2. import (
  3. "strings"
  4. )
  5. type DataValidationType int
  6. // Data validation types
  7. /*const (
  8. _ DataValidationType = iota
  9. TypeNone
  10. TypeCustom
  11. TypeDate
  12. TypeDecimal
  13. TypeList
  14. TypeTextLeng
  15. TypeTime
  16. TypeWhole
  17. )*/
  18. // Data validation types
  19. const (
  20. typeNone = "none"
  21. typeCustom = "custom"
  22. typeDate = "date"
  23. typeDecimal = "decimal"
  24. typeList = "list"
  25. typeTextLeng = "textLength"
  26. typeTime = "time"
  27. typeWhole = "whole"
  28. )
  29. type DataValidationErrorStyle int
  30. // Data validation error styles
  31. const (
  32. _ DataValidationErrorStyle = iota
  33. StyleStop
  34. StyleWarning
  35. StyleInformation
  36. )
  37. // Data validation error styles
  38. const (
  39. styleStop = "stop"
  40. styleWarning = "warning"
  41. styleInformation = "information"
  42. )
  43. /*
  44. type DataValidationOperator int
  45. // Data validation operators
  46. const (
  47. _DataValidationOperator = iota
  48. OperatorBetween = "between"
  49. OperatorEqual = "equal"
  50. OperatorGreaterThan = "greaterThan"
  51. OperatorGgreaterThanOrEqual = "greaterThanOrEqual"
  52. OperatorLessThan = "lessThan"
  53. OperatorLessThanOrEqual = "lessThanOrEqual"
  54. OperatorNotBetween = "notBetween"
  55. OperatorNotEqual = "notEqual"
  56. )
  57. // Data validation operators
  58. const (
  59. operatorBetween = "between"
  60. operatorEQUAL = "equal"
  61. operatorGREATERTHAN = "greaterThan"
  62. operatorGREATERTHANOREQUAL = "greaterThanOrEqual"
  63. operatorLESSTHAN = "lessThan"
  64. operatorLESSTHANOREQUAL = "lessThanOrEqual"
  65. operatorNOTBETWEEN = "notBetween"
  66. operatorNOTEQUAL = "notEqual"
  67. )
  68. */
  69. // NewDataValidation return data validation struct
  70. func NewXlsxCellDataValidation(allowBlank, ShowInputMessage, showErrorMessage bool) *xlsxCellDataValidation {
  71. return &xlsxCellDataValidation{
  72. AllowBlank: convBoolToStr(allowBlank),
  73. ShowErrorMessage: convBoolToStr(showErrorMessage),
  74. ShowInputMessage: convBoolToStr(ShowInputMessage),
  75. }
  76. }
  77. // SetError set error notice
  78. func (dd *xlsxCellDataValidation) SetError(style DataValidationErrorStyle, title, msg *string) {
  79. dd.Error = msg
  80. dd.ErrorTitle = title
  81. strStyle := styleStop
  82. switch style {
  83. case StyleStop:
  84. strStyle = styleStop
  85. case StyleWarning:
  86. strStyle = styleWarning
  87. case StyleInformation:
  88. strStyle = styleInformation
  89. }
  90. dd.ErrorStyle = &strStyle
  91. }
  92. // SetInput set prompt notice
  93. func (dd *xlsxCellDataValidation) SetInput(title, msg *string) {
  94. dd.PromptTitle = title
  95. dd.Prompt = msg
  96. }
  97. func (dd *xlsxCellDataValidation) SetDropList(keys []string) {
  98. dd.Formula1 = "\"" + strings.Join(keys, ",") + "\""
  99. dd.Type = typeList
  100. }
  101. // convBoolToStr convert boolean to string , false to 0, true to 1
  102. func convBoolToStr(bl bool) string {
  103. if bl {
  104. return "1"
  105. }
  106. return "0"
  107. }