databook.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2015 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "os"
  7. tablib "github.com/agrison/go-tablib"
  8. )
  9. type Databook struct {
  10. XDatabook *tablib.Databook
  11. }
  12. func NewDatabook() *Databook {
  13. db := &Databook{XDatabook: tablib.NewDatabook()}
  14. return db
  15. }
  16. func NewDatabookWithData(sheetName map[string]string, data interface{}, mustMatch bool, headers ...map[string][]string) (*Databook, error) {
  17. s := len(sheetName)
  18. switch data.(type) {
  19. case map[string]*tablib.Dataset:
  20. dataModel1 := data.(map[string]*tablib.Dataset)
  21. d1 := len(dataModel1)
  22. if s != d1 && mustMatch {
  23. return nil, ErrParamsType
  24. }
  25. databook := tablib.NewDatabook()
  26. for k, _ := range dataModel1 {
  27. if _, ok := sheetName[k]; !ok {
  28. return nil, ErrParamsType
  29. }
  30. databook.AddSheet(sheetName[k], dataModel1[k])
  31. }
  32. db := &Databook{XDatabook: databook}
  33. return db, nil
  34. case map[string][]map[string]interface{}:
  35. dataModel2 := data.(map[string][]map[string]interface{})
  36. d2 := len(dataModel2)
  37. if len(headers) != 1 {
  38. return nil, ErrParamsType
  39. }
  40. h := len(headers[0])
  41. if (s != h || s != d2) && mustMatch {
  42. return nil, ErrParamsType
  43. }
  44. databook := tablib.NewDatabook()
  45. for k, _ := range dataModel2 {
  46. if _, ok := sheetName[k]; !ok {
  47. return nil, ErrParamsType
  48. }
  49. if _, ok := headers[0][k]; !ok {
  50. return nil, ErrParamsType
  51. }
  52. dataset, err := NewDatasetWithData(headers[0][k], dataModel2[k], mustMatch)
  53. if err != nil {
  54. return nil, err
  55. }
  56. databook.AddSheet(sheetName[k], dataset)
  57. }
  58. db := &Databook{XDatabook: databook}
  59. return db, nil
  60. default:
  61. return nil, ErrParamsType
  62. }
  63. }
  64. func (databook *Databook) AddSheet(title string, data interface{}, mustMatch bool, headers ...[]string) error {
  65. switch data.(type) {
  66. case *tablib.Dataset:
  67. dataset := data.(*tablib.Dataset)
  68. databook.XDatabook.AddSheet(title, dataset)
  69. return nil
  70. case []map[string]interface{}:
  71. dataSlice := data.([]map[string]interface{})
  72. if len(headers) != 1 {
  73. return ErrParamsType
  74. }
  75. dataset, err := NewDatasetWithData(headers[0], dataSlice, mustMatch)
  76. if err != nil {
  77. return err
  78. }
  79. databook.XDatabook.AddSheet(title, dataset)
  80. return nil
  81. default:
  82. return ErrParamsType
  83. }
  84. }
  85. func (databook *Databook) HTML() *tablib.Exportable {
  86. return databook.XDatabook.HTML()
  87. }
  88. func (databook *Databook) SaveAsHTML(filename string, perm os.FileMode) error {
  89. html := databook.XDatabook.HTML()
  90. return html.WriteFile(filename, perm)
  91. }
  92. func (databook *Databook) JSON() (*tablib.Exportable, error) {
  93. return databook.XDatabook.JSON()
  94. }
  95. func (databook *Databook) SaveAsJSON(filename string, perm os.FileMode) error {
  96. json, err := databook.XDatabook.JSON()
  97. if err != nil {
  98. return err
  99. }
  100. return json.WriteFile(filename, perm)
  101. }
  102. func (databook *Databook) XLSX() (*tablib.Exportable, error) {
  103. return databook.XDatabook.XLSX()
  104. }
  105. func (databook *Databook) SaveAsXLSX(filename string, perm os.FileMode) error {
  106. xlsx, err := databook.XDatabook.XLSX()
  107. if err != nil {
  108. return err
  109. }
  110. return xlsx.WriteFile(filename, perm)
  111. }
  112. func (databook *Databook) XML() (*tablib.Exportable, error) {
  113. return databook.XDatabook.XML()
  114. }
  115. func (databook *Databook) SaveAsXML(filename string, perm os.FileMode) error {
  116. xml, err := databook.XDatabook.XML()
  117. if err != nil {
  118. return err
  119. }
  120. return xml.WriteFile(filename, perm)
  121. }
  122. func (databook *Databook) YAML() (*tablib.Exportable, error) {
  123. return databook.XDatabook.YAML()
  124. }
  125. func (databook *Databook) SaveAsYAML(filename string, perm os.FileMode) error {
  126. yaml, err := databook.XDatabook.YAML()
  127. if err != nil {
  128. return err
  129. }
  130. return yaml.WriteFile(filename, perm)
  131. }
  132. func (databook *Databook) Sheet(title string) tablib.Sheet {
  133. return databook.XDatabook.Sheet(title)
  134. }
  135. func (databook *Databook) Sheets() map[string]tablib.Sheet {
  136. return databook.XDatabook.Sheets()
  137. }
  138. func (databook *Databook) Size() int {
  139. return databook.XDatabook.Size()
  140. }
  141. func (databook *Databook) Wipe() {
  142. databook.XDatabook.Wipe()
  143. }