lib.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. package xlsx
  2. import (
  3. "archive/zip"
  4. "encoding/xml"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "math"
  9. "path"
  10. "runtime"
  11. "strconv"
  12. "strings"
  13. )
  14. // XLSXReaderError is the standard error type for otherwise undefined
  15. // errors in the XSLX reading process.
  16. type XLSXReaderError struct {
  17. Err string
  18. }
  19. // String() returns a string value from an XLSXReaderError struct in
  20. // order that it might comply with the os.Error interface.
  21. func (e *XLSXReaderError) Error() string {
  22. return e.Err
  23. }
  24. // getRangeFromString is an internal helper function that converts
  25. // XLSX internal range syntax to a pair of integers. For example,
  26. // the range string "1:3" yield the upper and lower intergers 1 and 3.
  27. func getRangeFromString(rangeString string) (lower int, upper int, error error) {
  28. var parts []string
  29. parts = strings.SplitN(rangeString, ":", 2)
  30. if parts[0] == "" {
  31. error = errors.New(fmt.Sprintf("Invalid range '%s'\n", rangeString))
  32. }
  33. if parts[1] == "" {
  34. error = errors.New(fmt.Sprintf("Invalid range '%s'\n", rangeString))
  35. }
  36. lower, error = strconv.Atoi(parts[0])
  37. if error != nil {
  38. error = errors.New(fmt.Sprintf("Invalid range (not integer in lower bound) %s\n", rangeString))
  39. }
  40. upper, error = strconv.Atoi(parts[1])
  41. if error != nil {
  42. error = errors.New(fmt.Sprintf("Invalid range (not integer in upper bound) %s\n", rangeString))
  43. }
  44. return lower, upper, error
  45. }
  46. // lettersToNumeric is used to convert a character based column
  47. // reference to a zero based numeric column identifier.
  48. func lettersToNumeric(letters string) int {
  49. sum, mul, n := 0, 1, 0
  50. for i := len(letters) - 1; i >= 0; i, mul, n = i-1, mul*26, 1 {
  51. c := letters[i]
  52. switch {
  53. case 'A' <= c && c <= 'Z':
  54. n += int(c - 'A')
  55. case 'a' <= c && c <= 'z':
  56. n += int(c - 'a')
  57. }
  58. sum += n * mul
  59. }
  60. return sum
  61. }
  62. // Get the largestDenominator that is a multiple of a basedDenominator
  63. // and fits at least once into a given numerator.
  64. func getLargestDenominator(numerator, multiple, baseDenominator, power int) (int, int) {
  65. if numerator / multiple == 0 {
  66. return 1, power
  67. }
  68. next, nextPower := getLargestDenominator(
  69. numerator, multiple * baseDenominator, baseDenominator, power + 1)
  70. if next > multiple {
  71. return next, nextPower
  72. }
  73. return multiple, power
  74. }
  75. // Convers a list of numbers representing a column into a alphabetic
  76. // representation, as used in the spreadsheet.
  77. func formatColumnName(colId []int) string {
  78. lastPart := len(colId) - 1
  79. result := ""
  80. for n, part := range(colId) {
  81. if n == lastPart {
  82. // The least significant number is in the
  83. // range 0-25, all other numbers are 1-26,
  84. // hence we use a differente offset for the
  85. // last part.
  86. result += string(part + 65)
  87. } else {
  88. // Don't output leading 0s, as there is no
  89. // representation of 0 in this format.
  90. if part > 0 {
  91. result += string(part + 64)
  92. }
  93. }
  94. }
  95. return result
  96. }
  97. func smooshBase26Slice(b26 []int) []int {
  98. // Smoosh values together, eliminating 0s from all but the
  99. // least significant part.
  100. lastButOnePart := len(b26) - 2
  101. for i := lastButOnePart; i > 0; i-- {
  102. part := b26[i]
  103. if part == 0 {
  104. greaterPart := b26[i-1]
  105. if greaterPart > 0 {
  106. b26[i-1] = greaterPart - 1
  107. b26[i] = 26
  108. }
  109. }
  110. }
  111. return b26
  112. }
  113. func intToBase26(x int) (parts []int) {
  114. // Excel column codes are pure evil - in essence they're just
  115. // base26, but they don't represent the number 0.
  116. b26Denominator, _ := getLargestDenominator(x, 1, 26, 0)
  117. // This loop terminates because integer division of 1 / 26
  118. // returns 0.
  119. for d := b26Denominator; d > 0; d = d / 26 {
  120. value := x / d
  121. remainder := x % d
  122. parts = append(parts, value)
  123. x = remainder
  124. }
  125. return parts
  126. }
  127. // numericToLetters is used to convert a zero based, numeric column
  128. // indentifier into a character code.
  129. func numericToLetters(colRef int) string {
  130. parts := intToBase26(colRef)
  131. return formatColumnName(smooshBase26Slice(parts))
  132. }
  133. // letterOnlyMapF is used in conjunction with strings.Map to return
  134. // only the characters A-Z and a-z in a string
  135. func letterOnlyMapF(rune rune) rune {
  136. switch {
  137. case 'A' <= rune && rune <= 'Z':
  138. return rune
  139. case 'a' <= rune && rune <= 'z':
  140. return rune - 32
  141. }
  142. return -1
  143. }
  144. // intOnlyMapF is used in conjunction with strings.Map to return only
  145. // the numeric portions of a string.
  146. func intOnlyMapF(rune rune) rune {
  147. if rune >= 48 && rune < 58 {
  148. return rune
  149. }
  150. return -1
  151. }
  152. // getCoordsFromCellIDString returns the zero based cartesian
  153. // coordinates from a cell name in Excel format, e.g. the cellIDString
  154. // "A1" returns 0, 0 and the "B3" return 1, 2.
  155. func getCoordsFromCellIDString(cellIDString string) (x, y int, error error) {
  156. var letterPart string = strings.Map(letterOnlyMapF, cellIDString)
  157. y, error = strconv.Atoi(strings.Map(intOnlyMapF, cellIDString))
  158. if error != nil {
  159. return x, y, error
  160. }
  161. y -= 1 // Zero based
  162. x = lettersToNumeric(letterPart)
  163. return x, y, error
  164. }
  165. // getCellIDStringFromCoords returns the Excel format cell name that
  166. // represents a pair of zero based cartesian coordinates.
  167. func getCellIDStringFromCoords(x, y int) string {
  168. letterPart := numericToLetters(x);
  169. numericPart := y + 1
  170. return fmt.Sprintf("%s%d", letterPart, numericPart)
  171. }
  172. // getMaxMinFromDimensionRef return the zero based cartesian maximum
  173. // and minimum coordinates from the dimension reference embedded in a
  174. // XLSX worksheet. For example, the dimension reference "A1:B2"
  175. // returns "0,0", "1,1".
  176. func getMaxMinFromDimensionRef(ref string) (minx, miny, maxx, maxy int, err error) {
  177. var parts []string
  178. parts = strings.Split(ref, ":")
  179. minx, miny, err = getCoordsFromCellIDString(parts[0])
  180. if err != nil {
  181. return -1, -1, -1, -1, err
  182. }
  183. if len(parts) == 1 {
  184. maxx, maxy = minx, miny
  185. return
  186. }
  187. maxx, maxy, err = getCoordsFromCellIDString(parts[1])
  188. if err != nil {
  189. return -1, -1, -1, -1, err
  190. }
  191. return
  192. }
  193. // calculateMaxMinFromWorkSheet works out the dimensions of a spreadsheet
  194. // that doesn't have a DimensionRef set. The only case currently
  195. // known where this is true is with XLSX exported from Google Docs.
  196. func calculateMaxMinFromWorksheet(worksheet *xlsxWorksheet) (minx, miny, maxx, maxy int, err error) {
  197. // Note, this method could be very slow for large spreadsheets.
  198. var x, y int
  199. var maxVal int
  200. if runtime.GOARCH == "386" {
  201. maxVal = math.MaxInt32
  202. } else {
  203. maxVal = math.MaxInt64
  204. }
  205. minx = maxVal
  206. miny = maxVal
  207. maxy = 0
  208. maxx = 0
  209. for _, row := range worksheet.SheetData.Row {
  210. for _, cell := range row.C {
  211. x, y, err = getCoordsFromCellIDString(cell.R)
  212. if err != nil {
  213. return -1, -1, -1, -1, err
  214. }
  215. if x < minx {
  216. minx = x
  217. }
  218. if x > maxx {
  219. maxx = x
  220. }
  221. if y < miny {
  222. miny = y
  223. }
  224. if y > maxy {
  225. maxy = y
  226. }
  227. }
  228. }
  229. if minx == maxVal { minx = 0 }
  230. if miny == maxVal { miny = 0 }
  231. return
  232. }
  233. // makeRowFromSpan will, when given a span expressed as a string,
  234. // return an empty Row large enough to encompass that span and
  235. // populate it with empty cells. All rows start from cell 1 -
  236. // regardless of the lower bound of the span.
  237. func makeRowFromSpan(spans string) *Row {
  238. var error error
  239. var upper int
  240. var row *Row
  241. var cell *Cell
  242. row = new(Row)
  243. _, upper, error = getRangeFromString(spans)
  244. if error != nil {
  245. panic(error)
  246. }
  247. error = nil
  248. row.Cells = make([]*Cell, upper)
  249. for i := 0; i < upper; i++ {
  250. cell = new(Cell)
  251. cell.Value = ""
  252. row.Cells[i] = cell
  253. }
  254. return row
  255. }
  256. // makeRowFromRaw returns the Row representation of the xlsxRow.
  257. func makeRowFromRaw(rawrow xlsxRow) *Row {
  258. var upper int
  259. var row *Row
  260. var cell *Cell
  261. row = new(Row)
  262. upper = -1
  263. for _, rawcell := range rawrow.C {
  264. x, _, error := getCoordsFromCellIDString(rawcell.R)
  265. if error != nil {
  266. panic(fmt.Sprintf("Invalid Cell Coord, %s\n", rawcell.R))
  267. }
  268. if x > upper {
  269. upper = x
  270. }
  271. }
  272. upper++
  273. row.Cells = make([]*Cell, upper)
  274. for i := 0; i < upper; i++ {
  275. cell = new(Cell)
  276. cell.Value = ""
  277. row.Cells[i] = cell
  278. }
  279. return row
  280. }
  281. // getValueFromCellData attempts to extract a valid value, usable in
  282. // CSV form from the raw cell value. Note - this is not actually
  283. // general enough - we should support retaining tabs and newlines.
  284. func getValueFromCellData(rawcell xlsxC, reftable *RefTable) string {
  285. var value string = ""
  286. var data string = rawcell.V
  287. if len(data) > 0 {
  288. vval := strings.Trim(data, " \t\n\r")
  289. switch rawcell.T {
  290. case "s": // Shared String
  291. ref, error := strconv.Atoi(vval)
  292. if error != nil {
  293. panic(error)
  294. }
  295. value = reftable.ResolveSharedString(ref)
  296. default:
  297. value = vval
  298. }
  299. }
  300. return value
  301. }
  302. // readRowsFromSheet is an internal helper function that extracts the
  303. // rows from a XSLXWorksheet, poulates them with Cells and resolves
  304. // the value references from the reference table and stores them in
  305. func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, int, int) {
  306. var rows []*Row
  307. var row *Row
  308. var minCol, maxCol, minRow, maxRow, colCount, rowCount int
  309. var reftable *RefTable
  310. var err error
  311. var insertRowIndex, insertColIndex int
  312. if len(Worksheet.SheetData.Row) == 0 {
  313. return nil, 0, 0
  314. }
  315. reftable = file.referenceTable
  316. if len(Worksheet.Dimension.Ref) > 0 {
  317. minCol, minRow, maxCol, maxRow, err = getMaxMinFromDimensionRef(Worksheet.Dimension.Ref)
  318. } else {
  319. minCol, minRow, maxCol, maxRow, err = calculateMaxMinFromWorksheet(Worksheet)
  320. }
  321. if err != nil {
  322. panic(err.Error())
  323. }
  324. rowCount = (maxRow - minRow) + 1
  325. colCount = (maxCol - minCol) + 1
  326. rows = make([]*Row, rowCount)
  327. insertRowIndex = minRow
  328. for rowIndex := 0; rowIndex < len(Worksheet.SheetData.Row); rowIndex++ {
  329. rawrow := Worksheet.SheetData.Row[rowIndex]
  330. // Some spreadsheets will omit blank rows from the
  331. // stored data
  332. for rawrow.R > (insertRowIndex + 1) {
  333. // Put an empty Row into the array
  334. rows[insertRowIndex-minRow] = new(Row)
  335. insertRowIndex++
  336. }
  337. // range is not empty and only one range exist
  338. if len(rawrow.Spans) != 0 && strings.Count(rawrow.Spans, ":") == 1 {
  339. row = makeRowFromSpan(rawrow.Spans)
  340. } else {
  341. row = makeRowFromRaw(rawrow)
  342. }
  343. insertColIndex = minCol
  344. for _, rawcell := range rawrow.C {
  345. x, _, _ := getCoordsFromCellIDString(rawcell.R)
  346. // Some spreadsheets will omit blank cells
  347. // from the data.
  348. for x > insertColIndex {
  349. // Put an empty Cell into the array
  350. row.Cells[insertColIndex-minCol] = new(Cell)
  351. insertColIndex++
  352. }
  353. cellX := insertColIndex - minCol
  354. row.Cells[cellX].Value = getValueFromCellData(rawcell, reftable)
  355. row.Cells[cellX].styleIndex = rawcell.S
  356. row.Cells[cellX].styles = file.styles
  357. row.Cells[cellX].numFmtRefTable = file.numFmtRefTable
  358. row.Cells[cellX].date1904 = file.Date1904
  359. insertColIndex++
  360. }
  361. if len(rows) > insertRowIndex-minRow {
  362. rows[insertRowIndex-minRow] = row
  363. }
  364. insertRowIndex++
  365. }
  366. return rows, colCount, rowCount
  367. }
  368. type indexedSheet struct {
  369. Index int
  370. Sheet *Sheet
  371. Error error
  372. }
  373. // readSheetFromFile is the logic of converting a xlsxSheet struct
  374. // into a Sheet struct. This work can be done in parallel and so
  375. // readSheetsFromZipFile will spawn an instance of this function per
  376. // sheet and get the results back on the provided channel.
  377. func readSheetFromFile(sc chan *indexedSheet, index int, rsheet xlsxSheet, fi *File, sheetXMLMap map[string]string) {
  378. result := &indexedSheet{Index: index, Sheet: nil, Error: nil}
  379. worksheet, error := getWorksheetFromSheet(rsheet, fi.worksheets, sheetXMLMap)
  380. if error != nil {
  381. result.Error = error
  382. sc <- result
  383. return
  384. }
  385. sheet := new(Sheet)
  386. sheet.Rows, sheet.MaxCol, sheet.MaxRow = readRowsFromSheet(worksheet, fi)
  387. result.Sheet = sheet
  388. sc <- result
  389. }
  390. // readSheetsFromZipFile is an internal helper function that loops
  391. // over the Worksheets defined in the XSLXWorkbook and loads them into
  392. // Sheet objects stored in the Sheets slice of a xlsx.File struct.
  393. func readSheetsFromZipFile(f *zip.File, file *File, sheetXMLMap map[string]string) (map[string]*Sheet, []*Sheet, error) {
  394. var workbook *xlsxWorkbook
  395. var error error
  396. var rc io.ReadCloser
  397. var decoder *xml.Decoder
  398. var sheetCount int
  399. workbook = new(xlsxWorkbook)
  400. rc, error = f.Open()
  401. if error != nil {
  402. return nil, nil, error
  403. }
  404. decoder = xml.NewDecoder(rc)
  405. error = decoder.Decode(workbook)
  406. if error != nil {
  407. return nil, nil, error
  408. }
  409. file.Date1904 = workbook.WorkbookPr.Date1904
  410. sheetCount = len(workbook.Sheets.Sheet)
  411. sheetsByName := make(map[string]*Sheet, sheetCount)
  412. sheets := make([]*Sheet, sheetCount)
  413. sheetChan := make(chan *indexedSheet, sheetCount)
  414. for i, rawsheet := range workbook.Sheets.Sheet {
  415. go readSheetFromFile(sheetChan, i, rawsheet, file, sheetXMLMap)
  416. }
  417. for j := 0; j < sheetCount; j++ {
  418. sheet := <-sheetChan
  419. if sheet.Error != nil {
  420. return nil, nil, sheet.Error
  421. }
  422. sheetsByName[workbook.Sheets.Sheet[sheet.Index].Name] = sheet.Sheet
  423. sheets[j] = sheet.Sheet
  424. }
  425. return sheetsByName, sheets, nil
  426. }
  427. // readSharedStringsFromZipFile() is an internal helper function to
  428. // extract a reference table from the sharedStrings.xml file within
  429. // the XLSX zip file.
  430. func readSharedStringsFromZipFile(f *zip.File) (*RefTable, error) {
  431. var sst *xlsxSST
  432. var error error
  433. var rc io.ReadCloser
  434. var decoder *xml.Decoder
  435. var reftable *RefTable
  436. // In a file with no strings it's possible that
  437. // sharedStrings.xml doesn't exist. In this case the value
  438. // passed as f will be nil.
  439. if f == nil {
  440. return nil, nil
  441. }
  442. rc, error = f.Open()
  443. if error != nil {
  444. return nil, error
  445. }
  446. sst = new(xlsxSST)
  447. decoder = xml.NewDecoder(rc)
  448. error = decoder.Decode(sst)
  449. if error != nil {
  450. return nil, error
  451. }
  452. reftable = MakeSharedStringRefTable(sst)
  453. return reftable, nil
  454. }
  455. // readStylesFromZipFile() is an internal helper function to
  456. // extract a style table from the style.xml file within
  457. // the XLSX zip file.
  458. func readStylesFromZipFile(f *zip.File) (*xlsxStyles, error) {
  459. var style *xlsxStyles
  460. var error error
  461. var rc io.ReadCloser
  462. var decoder *xml.Decoder
  463. rc, error = f.Open()
  464. if error != nil {
  465. return nil, error
  466. }
  467. style = new(xlsxStyles)
  468. decoder = xml.NewDecoder(rc)
  469. error = decoder.Decode(style)
  470. if error != nil {
  471. return nil, error
  472. }
  473. return style, nil
  474. }
  475. func buildNumFmtRefTable(style *xlsxStyles) map[int]xlsxNumFmt {
  476. refTable := make(map[int]xlsxNumFmt)
  477. for _, numFmt := range style.NumFmts {
  478. refTable[numFmt.NumFmtId] = numFmt
  479. }
  480. return refTable
  481. }
  482. type WorkBookRels map[string]string
  483. func (w *WorkBookRels) MakeXLSXWorkbookRels() xlsxWorkbookRels {
  484. relCount := len(*w)
  485. xWorkbookRels := xlsxWorkbookRels{}
  486. xWorkbookRels.Relationships = make([]xlsxWorkbookRelation, relCount + 2)
  487. for k, v := range(*w) {
  488. index, err := strconv.Atoi(k[3:len(k)])
  489. if err != nil {
  490. panic(err.Error())
  491. }
  492. xWorkbookRels.Relationships[index -1] = xlsxWorkbookRelation{
  493. Id: k,
  494. Target: v,
  495. Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"}
  496. }
  497. relCount++
  498. sheetId := fmt.Sprintf("rId%d", relCount)
  499. xWorkbookRels.Relationships[relCount -1] = xlsxWorkbookRelation{
  500. Id: sheetId,
  501. Target: "sharedStrings.xml",
  502. Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"}
  503. relCount++
  504. sheetId = fmt.Sprintf("rId%d", relCount)
  505. xWorkbookRels.Relationships[relCount -1] = xlsxWorkbookRelation{
  506. Id: sheetId,
  507. Target: "styles.xml",
  508. Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"}
  509. return xWorkbookRels
  510. }
  511. // readWorkbookRelationsFromZipFile is an internal helper function to
  512. // extract a map of relationship ID strings to the name of the
  513. // worksheet.xml file they refer to. The resulting map can be used to
  514. // reliably derefence the worksheets in the XLSX file.
  515. func readWorkbookRelationsFromZipFile(workbookRels *zip.File) (WorkBookRels, error) {
  516. var sheetXMLMap WorkBookRels
  517. var wbRelationships *xlsxWorkbookRels
  518. var rc io.ReadCloser
  519. var decoder *xml.Decoder
  520. var err error
  521. rc, err = workbookRels.Open()
  522. if err != nil {
  523. return nil, err
  524. }
  525. decoder = xml.NewDecoder(rc)
  526. wbRelationships = new(xlsxWorkbookRels)
  527. err = decoder.Decode(wbRelationships)
  528. if err != nil {
  529. return nil, err
  530. }
  531. sheetXMLMap = make(WorkBookRels)
  532. for _, rel := range wbRelationships.Relationships {
  533. if strings.HasSuffix(rel.Target, ".xml") && rel.Type == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" {
  534. _, filename := path.Split(rel.Target)
  535. sheetXMLMap[rel.Id] = strings.Replace(filename, ".xml", "", 1)
  536. }
  537. }
  538. return sheetXMLMap, nil
  539. }
  540. // ReadZip() takes a pointer to a zip.ReadCloser and returns a
  541. // xlsx.File struct populated with its contents. In most cases
  542. // ReadZip is not used directly, but is called internally by OpenFile.
  543. func ReadZip(f *zip.ReadCloser) (*File, error) {
  544. defer f.Close()
  545. return ReadZipReader(&f.Reader)
  546. }
  547. // ReadZipReader() can be used to read an XLSX in memory without
  548. // touching the filesystem.
  549. func ReadZipReader(r *zip.Reader) (*File, error) {
  550. var err error
  551. var file *File
  552. var reftable *RefTable
  553. var sharedStrings *zip.File
  554. var sheetXMLMap map[string]string
  555. var sheetsByName map[string]*Sheet
  556. var sheets []*Sheet
  557. var style *xlsxStyles
  558. var styles *zip.File
  559. var v *zip.File
  560. var workbook *zip.File
  561. var workbookRels *zip.File
  562. var worksheets map[string]*zip.File
  563. file = new(File)
  564. worksheets = make(map[string]*zip.File, len(r.File))
  565. for _, v = range r.File {
  566. switch v.Name {
  567. case "xl/sharedStrings.xml":
  568. sharedStrings = v
  569. case "xl/workbook.xml":
  570. workbook = v
  571. case "xl/_rels/workbook.xml.rels":
  572. workbookRels = v
  573. case "xl/styles.xml":
  574. styles = v
  575. default:
  576. if len(v.Name) > 14 {
  577. if v.Name[0:13] == "xl/worksheets" {
  578. worksheets[v.Name[14:len(v.Name)-4]] = v
  579. }
  580. }
  581. }
  582. }
  583. sheetXMLMap, err = readWorkbookRelationsFromZipFile(workbookRels)
  584. if err != nil {
  585. return nil, err
  586. }
  587. file.worksheets = worksheets
  588. reftable, err = readSharedStringsFromZipFile(sharedStrings)
  589. if err != nil {
  590. return nil, err
  591. }
  592. file.referenceTable = reftable
  593. if styles != nil {
  594. style, err = readStylesFromZipFile(styles)
  595. if err != nil {
  596. return nil, err
  597. }
  598. file.styles = style
  599. }
  600. sheetsByName, sheets, err = readSheetsFromZipFile(workbook, file, sheetXMLMap)
  601. if err != nil {
  602. return nil, err
  603. }
  604. if sheets == nil {
  605. readerErr := new(XLSXReaderError)
  606. readerErr.Err = "No sheets found in XLSX File"
  607. return nil, readerErr
  608. }
  609. file.Sheet = sheetsByName
  610. file.Sheets = sheets
  611. return file, nil
  612. }