xmlWorkbook.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package xlsx
  2. import (
  3. "archive/zip"
  4. "encoding/xml"
  5. "fmt"
  6. "io"
  7. )
  8. // xmlxWorkbookRels contains xmlxWorkbookRelations
  9. // which maps sheet id and sheet XML
  10. type xlsxWorkbookRels struct {
  11. XMLName xml.Name `xml:"http://schemas.openxmlformats.org/package/2006/relationships Relationships"`
  12. Relationships []xlsxWorkbookRelation `xml:"Relationship"`
  13. }
  14. // xmlxWorkbookRelation maps sheet id and xl/worksheets/sheet%d.xml
  15. type xlsxWorkbookRelation struct {
  16. Id string `xml:",attr"`
  17. Target string `xml:",attr"`
  18. Type string `xml:",attr"`
  19. }
  20. // xlsxWorkbook directly maps the workbook element from the namespace
  21. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  22. // currently I have not checked it for completeness - it does as much
  23. // as I need.
  24. type xlsxWorkbook struct {
  25. XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main workbook"`
  26. FileVersion xlsxFileVersion `xml:"fileVersion"`
  27. WorkbookPr xlsxWorkbookPr `xml:"workbookPr"`
  28. WorkbookProtection xlsxWorkbookProtection `xml:"workbookProtection"`
  29. BookViews xlsxBookViews `xml:"bookViews"`
  30. Sheets xlsxSheets `xml:"sheets"`
  31. DefinedNames xlsxDefinedNames `xml:"definedNames"`
  32. CalcPr xlsxCalcPr `xml:"calcPr"`
  33. }
  34. // xlsxWorkbookProtection directly maps the workbookProtection element from the
  35. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  36. // - currently I have not checked it for completeness - it does as
  37. // much as I need.
  38. type xlsxWorkbookProtection struct {
  39. // We don't need this, yet.
  40. }
  41. // xlsxFileVersion directly maps the fileVersion element from the
  42. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  43. // - currently I have not checked it for completeness - it does as
  44. // much as I need.
  45. type xlsxFileVersion struct {
  46. AppName string `xml:"appName,attr,omitempty"`
  47. LastEdited string `xml:"lastEdited,attr,omitempty"`
  48. LowestEdited string `xml:"lowestEdited,attr,omitempty"`
  49. RupBuild string `xml:"rupBuild,attr,omitempty"`
  50. }
  51. // xlsxWorkbookPr directly maps the workbookPr element from the
  52. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  53. // - currently I have not checked it for completeness - it does as
  54. // much as I need.
  55. type xlsxWorkbookPr struct {
  56. DefaultThemeVersion string `xml:"defaultThemeVersion,attr,omitempty"`
  57. BackupFile bool `xml:"backupFile,attr,omitempty"`
  58. ShowObjects string `xml:"showObjects,attr,omitempty"`
  59. Date1904 bool `xml:"date1904,attr"`
  60. }
  61. // xlsxBookViews directly maps the bookViews element from the
  62. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  63. // - currently I have not checked it for completeness - it does as
  64. // much as I need.
  65. type xlsxBookViews struct {
  66. WorkBookView []xlsxWorkBookView `xml:"workbookView"`
  67. }
  68. // xlsxWorkBookView directly maps the workbookView element from the
  69. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  70. // - currently I have not checked it for completeness - it does as
  71. // much as I need.
  72. type xlsxWorkBookView struct {
  73. ActiveTab int `xml:"activeTab,attr,omitempty"`
  74. FirstSheet int `xml:"firstSheet,attr,omitempty"`
  75. ShowHorizontalScroll bool `xml:"showHorizontalScroll,attr,omitempty"`
  76. ShowVerticalScroll bool `xml:"showVerticalScroll,attr,omitempty"`
  77. ShowSheetTabs bool `xml:"showSheetTabs,attr,omitempty"`
  78. TabRatio int `xml:"tabRatio,attr,omitempty"`
  79. WindowHeight int `xml:"windowHeight,attr,omitempty"`
  80. WindowWidth int `xml:"windowWidth,attr,omitempty"`
  81. XWindow string `xml:"xWindow,attr,omitempty"`
  82. YWindow string `xml:"yWindow,attr,omitempty"`
  83. }
  84. // xlsxSheets directly maps the sheets element from the namespace
  85. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  86. // currently I have not checked it for completeness - it does as much
  87. // as I need.
  88. type xlsxSheets struct {
  89. Sheet []xlsxSheet `xml:"sheet"`
  90. }
  91. // xlsxSheet directly maps the sheet element from the namespace
  92. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  93. // currently I have not checked it for completeness - it does as much
  94. // as I need.
  95. type xlsxSheet struct {
  96. Name string `xml:"name,attr,omitempty"`
  97. SheetId string `xml:"sheetId,attr,omitempty"`
  98. Id string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"`
  99. State string `xml:"state,attr,omitempty"`
  100. }
  101. // xlsxDefinedNames directly maps the definedNames element from the
  102. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  103. // - currently I have not checked it for completeness - it does as
  104. // much as I need.
  105. type xlsxDefinedNames struct {
  106. DefinedName []xlsxDefinedName `xml:"definedName"`
  107. }
  108. // xlsxDefinedName directly maps the definedName element from the
  109. // namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
  110. // - currently I have not checked it for completeness - it does as
  111. // much as I need.
  112. type xlsxDefinedName struct {
  113. Data string `xml:",chardata"`
  114. Name string `xml:"name,attr"`
  115. LocalSheetID string `xml:"localSheetId,attr"`
  116. }
  117. // xlsxCalcPr directly maps the calcPr element from the namespace
  118. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  119. // currently I have not checked it for completeness - it does as much
  120. // as I need.
  121. type xlsxCalcPr struct {
  122. CalcId string `xml:"calcId,attr,omitempty"`
  123. IterateCount int `xml:"iterateCount,attr,omitempty"`
  124. RefMode string `xml:"refMode,attr,omitempty"`
  125. Iterate bool `xml:"iterate,attr,omitempty"`
  126. IterateDelta float64 `xml:"iterateDelta,attr,omitempty"`
  127. }
  128. // getWorksheetFromSheet() is an internal helper function to open a
  129. // sheetN.xml file, refered to by an xlsx.xlsxSheet struct, from the XLSX
  130. // file and unmarshal it an xlsx.xlsxWorksheet struct
  131. func getWorksheetFromSheet(sheet xlsxSheet, worksheets map[string]*zip.File, sheetXMLMap map[string]string) (*xlsxWorksheet, error) {
  132. var rc io.ReadCloser
  133. var decoder *xml.Decoder
  134. var worksheet *xlsxWorksheet
  135. var error error
  136. var sheetName string
  137. worksheet = new(xlsxWorksheet)
  138. sheetName, ok := sheetXMLMap[sheet.Id]
  139. if !ok {
  140. if sheet.SheetId != "" {
  141. sheetName = fmt.Sprintf("sheet%s", sheet.SheetId)
  142. } else {
  143. sheetName = fmt.Sprintf("sheet%s", sheet.Id)
  144. }
  145. }
  146. f := worksheets[sheetName]
  147. rc, error = f.Open()
  148. if error != nil {
  149. return nil, error
  150. }
  151. decoder = xml.NewDecoder(rc)
  152. error = decoder.Decode(worksheet)
  153. if error != nil {
  154. return nil, error
  155. }
  156. return worksheet, nil
  157. }