lib.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. package xlsx
  2. import (
  3. "archive/zip"
  4. "encoding/xml"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "path"
  9. "strconv"
  10. "strings"
  11. )
  12. // XLSXReaderError is the standard error type for otherwise undefined
  13. // errors in the XSLX reading process.
  14. type XLSXReaderError struct {
  15. Err string
  16. }
  17. // Error returns a string value from an XLSXReaderError struct in order
  18. // that it might comply with the builtin.error interface.
  19. func (e *XLSXReaderError) Error() string {
  20. return e.Err
  21. }
  22. // getRangeFromString is an internal helper function that converts
  23. // XLSX internal range syntax to a pair of integers. For example,
  24. // the range string "1:3" yield the upper and lower intergers 1 and 3.
  25. func getRangeFromString(rangeString string) (lower int, upper int, error error) {
  26. var parts []string
  27. parts = strings.SplitN(rangeString, ":", 2)
  28. if parts[0] == "" {
  29. error = errors.New(fmt.Sprintf("Invalid range '%s'\n", rangeString))
  30. }
  31. if parts[1] == "" {
  32. error = errors.New(fmt.Sprintf("Invalid range '%s'\n", rangeString))
  33. }
  34. lower, error = strconv.Atoi(parts[0])
  35. if error != nil {
  36. error = errors.New(fmt.Sprintf("Invalid range (not integer in lower bound) %s\n", rangeString))
  37. }
  38. upper, error = strconv.Atoi(parts[1])
  39. if error != nil {
  40. error = errors.New(fmt.Sprintf("Invalid range (not integer in upper bound) %s\n", rangeString))
  41. }
  42. return lower, upper, error
  43. }
  44. // lettersToNumeric is used to convert a character based column
  45. // reference to a zero based numeric column identifier.
  46. func lettersToNumeric(letters string) int {
  47. sum, mul, n := 0, 1, 0
  48. for i := len(letters) - 1; i >= 0; i, mul, n = i-1, mul*26, 1 {
  49. c := letters[i]
  50. switch {
  51. case 'A' <= c && c <= 'Z':
  52. n += int(c - 'A')
  53. case 'a' <= c && c <= 'z':
  54. n += int(c - 'a')
  55. }
  56. sum += n * mul
  57. }
  58. return sum
  59. }
  60. // Get the largestDenominator that is a multiple of a basedDenominator
  61. // and fits at least once into a given numerator.
  62. func getLargestDenominator(numerator, multiple, baseDenominator, power int) (int, int) {
  63. if numerator/multiple == 0 {
  64. return 1, power
  65. }
  66. next, nextPower := getLargestDenominator(
  67. numerator, multiple*baseDenominator, baseDenominator, power+1)
  68. if next > multiple {
  69. return next, nextPower
  70. }
  71. return multiple, power
  72. }
  73. // Convers a list of numbers representing a column into a alphabetic
  74. // representation, as used in the spreadsheet.
  75. func formatColumnName(colId []int) string {
  76. lastPart := len(colId) - 1
  77. result := ""
  78. for n, part := range colId {
  79. if n == lastPart {
  80. // The least significant number is in the
  81. // range 0-25, all other numbers are 1-26,
  82. // hence we use a differente offset for the
  83. // last part.
  84. result += string(part + 65)
  85. } else {
  86. // Don't output leading 0s, as there is no
  87. // representation of 0 in this format.
  88. if part > 0 {
  89. result += string(part + 64)
  90. }
  91. }
  92. }
  93. return result
  94. }
  95. func smooshBase26Slice(b26 []int) []int {
  96. // Smoosh values together, eliminating 0s from all but the
  97. // least significant part.
  98. lastButOnePart := len(b26) - 2
  99. for i := lastButOnePart; i > 0; i-- {
  100. part := b26[i]
  101. if part == 0 {
  102. greaterPart := b26[i-1]
  103. if greaterPart > 0 {
  104. b26[i-1] = greaterPart - 1
  105. b26[i] = 26
  106. }
  107. }
  108. }
  109. return b26
  110. }
  111. func intToBase26(x int) (parts []int) {
  112. // Excel column codes are pure evil - in essence they're just
  113. // base26, but they don't represent the number 0.
  114. b26Denominator, _ := getLargestDenominator(x, 1, 26, 0)
  115. // This loop terminates because integer division of 1 / 26
  116. // returns 0.
  117. for d := b26Denominator; d > 0; d = d / 26 {
  118. value := x / d
  119. remainder := x % d
  120. parts = append(parts, value)
  121. x = remainder
  122. }
  123. return parts
  124. }
  125. // numericToLetters is used to convert a zero based, numeric column
  126. // indentifier into a character code.
  127. func numericToLetters(colRef int) string {
  128. parts := intToBase26(colRef)
  129. return formatColumnName(smooshBase26Slice(parts))
  130. }
  131. // letterOnlyMapF is used in conjunction with strings.Map to return
  132. // only the characters A-Z and a-z in a string
  133. func letterOnlyMapF(rune rune) rune {
  134. switch {
  135. case 'A' <= rune && rune <= 'Z':
  136. return rune
  137. case 'a' <= rune && rune <= 'z':
  138. return rune - 32
  139. }
  140. return -1
  141. }
  142. // intOnlyMapF is used in conjunction with strings.Map to return only
  143. // the numeric portions of a string.
  144. func intOnlyMapF(rune rune) rune {
  145. if rune >= 48 && rune < 58 {
  146. return rune
  147. }
  148. return -1
  149. }
  150. // GetCoordsFromCellIDString returns the zero based cartesian
  151. // coordinates from a cell name in Excel format, e.g. the cellIDString
  152. // "A1" returns 0, 0 and the "B3" return 1, 2.
  153. func GetCoordsFromCellIDString(cellIDString string) (x, y int, error error) {
  154. var letterPart string = strings.Map(letterOnlyMapF, cellIDString)
  155. y, error = strconv.Atoi(strings.Map(intOnlyMapF, cellIDString))
  156. if error != nil {
  157. return x, y, error
  158. }
  159. y -= 1 // Zero based
  160. x = lettersToNumeric(letterPart)
  161. return x, y, error
  162. }
  163. // GetCellIDStringFromCoords returns the Excel format cell name that
  164. // represents a pair of zero based cartesian coordinates.
  165. func GetCellIDStringFromCoords(x, y int) string {
  166. letterPart := numericToLetters(x)
  167. numericPart := y + 1
  168. return fmt.Sprintf("%s%d", letterPart, numericPart)
  169. }
  170. // getMaxMinFromDimensionRef return the zero based cartesian maximum
  171. // and minimum coordinates from the dimension reference embedded in a
  172. // XLSX worksheet. For example, the dimension reference "A1:B2"
  173. // returns "0,0", "1,1".
  174. func getMaxMinFromDimensionRef(ref string) (minx, miny, maxx, maxy int, err error) {
  175. var parts []string
  176. parts = strings.Split(ref, ":")
  177. minx, miny, err = GetCoordsFromCellIDString(parts[0])
  178. if err != nil {
  179. return -1, -1, -1, -1, err
  180. }
  181. if len(parts) == 1 {
  182. maxx, maxy = minx, miny
  183. return
  184. }
  185. maxx, maxy, err = GetCoordsFromCellIDString(parts[1])
  186. if err != nil {
  187. return -1, -1, -1, -1, err
  188. }
  189. return
  190. }
  191. // calculateMaxMinFromWorkSheet works out the dimensions of a spreadsheet
  192. // that doesn't have a DimensionRef set. The only case currently
  193. // known where this is true is with XLSX exported from Google Docs.
  194. func calculateMaxMinFromWorksheet(worksheet *xlsxWorksheet) (minx, miny, maxx, maxy int, err error) {
  195. // Note, this method could be very slow for large spreadsheets.
  196. var x, y int
  197. var maxVal int
  198. maxVal = int(^uint(0) >> 1)
  199. minx = maxVal
  200. miny = maxVal
  201. maxy = 0
  202. maxx = 0
  203. for _, row := range worksheet.SheetData.Row {
  204. for _, cell := range row.C {
  205. x, y, err = GetCoordsFromCellIDString(cell.R)
  206. if err != nil {
  207. return -1, -1, -1, -1, err
  208. }
  209. if x < minx {
  210. minx = x
  211. }
  212. if x > maxx {
  213. maxx = x
  214. }
  215. if y < miny {
  216. miny = y
  217. }
  218. if y > maxy {
  219. maxy = y
  220. }
  221. }
  222. }
  223. if minx == maxVal {
  224. minx = 0
  225. }
  226. if miny == maxVal {
  227. miny = 0
  228. }
  229. return
  230. }
  231. // makeRowFromSpan will, when given a span expressed as a string,
  232. // return an empty Row large enough to encompass that span and
  233. // populate it with empty cells. All rows start from cell 1 -
  234. // regardless of the lower bound of the span.
  235. func makeRowFromSpan(spans string, sheet *Sheet) *Row {
  236. var error error
  237. var upper int
  238. var row *Row
  239. var cell *Cell
  240. row = new(Row)
  241. row.Sheet = sheet
  242. _, upper, error = getRangeFromString(spans)
  243. if error != nil {
  244. panic(error)
  245. }
  246. error = nil
  247. row.Cells = make([]*Cell, upper)
  248. for i := 0; i < upper; i++ {
  249. cell = new(Cell)
  250. cell.Value = ""
  251. row.Cells[i] = cell
  252. }
  253. return row
  254. }
  255. // makeRowFromRaw returns the Row representation of the xlsxRow.
  256. func makeRowFromRaw(rawrow xlsxRow, sheet *Sheet) *Row {
  257. var upper int
  258. var row *Row
  259. var cell *Cell
  260. row = new(Row)
  261. row.Sheet = sheet
  262. upper = -1
  263. for _, rawcell := range rawrow.C {
  264. if rawcell.R != "" {
  265. x, _, error := GetCoordsFromCellIDString(rawcell.R)
  266. if error != nil {
  267. panic(fmt.Sprintf("Invalid Cell Coord, %s\n", rawcell.R))
  268. }
  269. if x > upper {
  270. upper = x
  271. }
  272. continue
  273. }
  274. upper++
  275. }
  276. upper++
  277. row.OutlineLevel = rawrow.OutlineLevel
  278. row.Cells = make([]*Cell, upper)
  279. for i := 0; i < upper; i++ {
  280. cell = new(Cell)
  281. cell.Value = ""
  282. row.Cells[i] = cell
  283. }
  284. return row
  285. }
  286. func makeEmptyRow(sheet *Sheet) *Row {
  287. row := new(Row)
  288. row.Cells = make([]*Cell, 0)
  289. row.Sheet = sheet
  290. return row
  291. }
  292. type sharedFormula struct {
  293. x, y int
  294. formula string
  295. }
  296. func formulaForCell(rawcell xlsxC, sharedFormulas map[int]sharedFormula) string {
  297. var res string
  298. f := rawcell.F
  299. if f == nil {
  300. return ""
  301. }
  302. if f.T == "shared" {
  303. x, y, err := GetCoordsFromCellIDString(rawcell.R)
  304. if err != nil {
  305. res = f.Content
  306. } else {
  307. if f.Ref != "" {
  308. res = f.Content
  309. sharedFormulas[f.Si] = sharedFormula{x, y, res}
  310. } else {
  311. sharedFormula := sharedFormulas[f.Si]
  312. dx := x - sharedFormula.x
  313. dy := y - sharedFormula.y
  314. orig := []byte(sharedFormula.formula)
  315. var start, end int
  316. var stringLiteral bool
  317. for end = 0; end < len(orig); end++ {
  318. c := orig[end]
  319. if c == '"' {
  320. stringLiteral = !stringLiteral
  321. }
  322. if stringLiteral {
  323. continue // Skip characters in quotes
  324. }
  325. if c >= 'A' && c <= 'Z' || c == '$' {
  326. res += string(orig[start:end])
  327. start = end
  328. end++
  329. foundNum := false
  330. for ; end < len(orig); end++ {
  331. idc := orig[end]
  332. if idc >= '0' && idc <= '9' || idc == '$' {
  333. foundNum = true
  334. } else if idc >= 'A' && idc <= 'Z' {
  335. if foundNum {
  336. break
  337. }
  338. } else {
  339. break
  340. }
  341. }
  342. if foundNum {
  343. cellID := string(orig[start:end])
  344. res += shiftCell(cellID, dx, dy)
  345. start = end
  346. }
  347. }
  348. }
  349. if start < len(orig) {
  350. res += string(orig[start:])
  351. }
  352. }
  353. }
  354. } else {
  355. res = f.Content
  356. }
  357. return strings.Trim(res, " \t\n\r")
  358. }
  359. // shiftCell returns the cell shifted according to dx and dy taking into consideration of absolute
  360. // references with dollar sign ($)
  361. func shiftCell(cellID string, dx, dy int) string {
  362. fx, fy, _ := GetCoordsFromCellIDString(cellID)
  363. // Is fixed column?
  364. fixedCol := strings.Index(cellID, "$") == 0
  365. // Is fixed row?
  366. fixedRow := strings.LastIndex(cellID, "$") > 0
  367. if !fixedCol {
  368. // Shift column
  369. fx += dx
  370. }
  371. if !fixedRow {
  372. // Shift row
  373. fy += dy
  374. }
  375. // New shifted cell
  376. shiftedCellID := GetCellIDStringFromCoords(fx, fy)
  377. if !fixedCol && !fixedRow {
  378. return shiftedCellID
  379. }
  380. // There are absolute references, need to put the $ back into the formula.
  381. letterPart := strings.Map(letterOnlyMapF, shiftedCellID)
  382. numberPart := strings.Map(intOnlyMapF, shiftedCellID)
  383. result := ""
  384. if fixedCol {
  385. result += "$"
  386. }
  387. result += letterPart
  388. if fixedRow {
  389. result += "$"
  390. }
  391. result += numberPart
  392. return result
  393. }
  394. // fillCellData attempts to extract a valid value, usable in
  395. // CSV form from the raw cell value. Note - this is not actually
  396. // general enough - we should support retaining tabs and newlines.
  397. func fillCellData(rawcell xlsxC, reftable *RefTable, sharedFormulas map[int]sharedFormula, cell *Cell) {
  398. var data = rawcell.V
  399. if len(data) > 0 {
  400. vval := strings.Trim(data, " \t\n\r")
  401. switch rawcell.T {
  402. case "s": // Shared String
  403. ref, error := strconv.Atoi(vval)
  404. if error != nil {
  405. panic(error)
  406. }
  407. cell.Value = reftable.ResolveSharedString(ref)
  408. cell.cellType = CellTypeString
  409. case "b": // Boolean
  410. cell.Value = vval
  411. cell.cellType = CellTypeBool
  412. case "e": // Error
  413. cell.Value = vval
  414. cell.formula = formulaForCell(rawcell, sharedFormulas)
  415. cell.cellType = CellTypeError
  416. default:
  417. if rawcell.F == nil {
  418. // Numeric
  419. cell.Value = vval
  420. cell.cellType = CellTypeNumeric
  421. } else {
  422. // Formula
  423. cell.Value = vval
  424. cell.formula = formulaForCell(rawcell, sharedFormulas)
  425. cell.cellType = CellTypeFormula
  426. }
  427. }
  428. } else {
  429. if rawcell.Is != nil {
  430. fillCellDataFromInlineString(rawcell, cell)
  431. }
  432. }
  433. }
  434. // fillCellDataFromInlineString attempts to get inline string data and put it into a Cell.
  435. func fillCellDataFromInlineString(rawcell xlsxC, cell *Cell) {
  436. if rawcell.Is.T != "" {
  437. cell.Value = strings.Trim(rawcell.Is.T, " \t\n\r")
  438. cell.cellType = CellTypeInline
  439. } else {
  440. cell.Value = ""
  441. for _, r := range rawcell.Is.R {
  442. cell.Value += r.T
  443. }
  444. }
  445. }
  446. // readRowsFromSheet is an internal helper function that extracts the
  447. // rows from a XSLXWorksheet, populates them with Cells and resolves
  448. // the value references from the reference table and stores them in
  449. // the rows and columns.
  450. func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File, sheet *Sheet) ([]*Row, []*Col, int, int) {
  451. var rows []*Row
  452. var cols []*Col
  453. var row *Row
  454. var minCol, maxCol, minRow, maxRow, colCount, rowCount int
  455. var reftable *RefTable
  456. var err error
  457. var insertRowIndex, insertColIndex int
  458. sharedFormulas := map[int]sharedFormula{}
  459. if len(Worksheet.SheetData.Row) == 0 {
  460. return nil, nil, 0, 0
  461. }
  462. reftable = file.referenceTable
  463. if len(Worksheet.Dimension.Ref) > 0 {
  464. minCol, minRow, maxCol, maxRow, err = getMaxMinFromDimensionRef(Worksheet.Dimension.Ref)
  465. } else {
  466. minCol, minRow, maxCol, maxRow, err = calculateMaxMinFromWorksheet(Worksheet)
  467. }
  468. if err != nil {
  469. panic(err.Error())
  470. }
  471. rowCount = maxRow + 1
  472. colCount = maxCol + 1
  473. rows = make([]*Row, rowCount)
  474. cols = make([]*Col, colCount)
  475. insertRowIndex = minRow
  476. for i := range cols {
  477. cols[i] = &Col{
  478. Hidden: false,
  479. }
  480. }
  481. if Worksheet.Cols != nil {
  482. // Columns can apply to a range, for convenience we expand the
  483. // ranges out into individual column definitions.
  484. for _, rawcol := range Worksheet.Cols.Col {
  485. // Note, below, that sometimes column definitions can
  486. // exist outside the defined dimensions of the
  487. // spreadsheet - we deliberately exclude these
  488. // columns.
  489. for i := rawcol.Min; i <= rawcol.Max && i <= colCount; i++ {
  490. col := &Col{
  491. Min: rawcol.Min,
  492. Max: rawcol.Max,
  493. Hidden: rawcol.Hidden,
  494. Width: rawcol.Width,
  495. OutlineLevel: rawcol.OutlineLevel}
  496. cols[i-1] = col
  497. if file.styles != nil {
  498. col.style = file.styles.getStyle(rawcol.Style)
  499. col.numFmt = file.styles.getNumberFormat(rawcol.Style)
  500. }
  501. }
  502. }
  503. }
  504. // insert leading empty rows that is in front of minRow
  505. for rowIndex := 0; rowIndex < minRow; rowIndex++ {
  506. rows[rowIndex] = makeEmptyRow(sheet)
  507. }
  508. numRows := len(rows)
  509. for rowIndex := 0; rowIndex < len(Worksheet.SheetData.Row); rowIndex++ {
  510. rawrow := Worksheet.SheetData.Row[rowIndex]
  511. // Some spreadsheets will omit blank rows from the
  512. // stored data
  513. for rawrow.R > (insertRowIndex + 1) {
  514. // Put an empty Row into the array
  515. if insertRowIndex < numRows {
  516. rows[insertRowIndex] = makeEmptyRow(sheet)
  517. }
  518. insertRowIndex++
  519. }
  520. // range is not empty and only one range exist
  521. if len(rawrow.Spans) != 0 && strings.Count(rawrow.Spans, ":") == 1 {
  522. row = makeRowFromSpan(rawrow.Spans, sheet)
  523. } else {
  524. row = makeRowFromRaw(rawrow, sheet)
  525. }
  526. row.Hidden = rawrow.Hidden
  527. height, err := strconv.ParseFloat(rawrow.Ht, 64)
  528. if err == nil {
  529. row.Height = height
  530. }
  531. row.isCustom = rawrow.CustomHeight
  532. row.OutlineLevel = rawrow.OutlineLevel
  533. insertColIndex = minCol
  534. for _, rawcell := range rawrow.C {
  535. h, v, err := Worksheet.MergeCells.getExtent(rawcell.R)
  536. if err != nil {
  537. panic(err.Error())
  538. }
  539. x, _, _ := GetCoordsFromCellIDString(rawcell.R)
  540. // K1000000: Prevent panic when the range specified in the spreadsheet
  541. // view exceeds the actual number of columns in the dataset.
  542. // Some spreadsheets will omit blank cells
  543. // from the data.
  544. for x > insertColIndex {
  545. // Put an empty Cell into the array
  546. if insertColIndex < len(row.Cells) {
  547. row.Cells[insertColIndex] = new(Cell)
  548. }
  549. insertColIndex++
  550. }
  551. cellX := insertColIndex
  552. if cellX < len(row.Cells) {
  553. cell := row.Cells[cellX]
  554. cell.HMerge = h
  555. cell.VMerge = v
  556. fillCellData(rawcell, reftable, sharedFormulas, cell)
  557. if file.styles != nil {
  558. cell.style = file.styles.getStyle(rawcell.S)
  559. cell.NumFmt = file.styles.getNumberFormat(rawcell.S)
  560. }
  561. cell.date1904 = file.Date1904
  562. // Cell is considered hidden if the row or the column of this cell is hidden
  563. cell.Hidden = rawrow.Hidden || (len(cols) > cellX && cols[cellX].Hidden)
  564. insertColIndex++
  565. }
  566. }
  567. if len(rows) > insertRowIndex {
  568. rows[insertRowIndex] = row
  569. }
  570. insertRowIndex++
  571. }
  572. return rows, cols, colCount, rowCount
  573. }
  574. type indexedSheet struct {
  575. Index int
  576. Sheet *Sheet
  577. Error error
  578. }
  579. func readSheetViews(xSheetViews xlsxSheetViews) []SheetView {
  580. if xSheetViews.SheetView == nil || len(xSheetViews.SheetView) == 0 {
  581. return nil
  582. }
  583. sheetViews := []SheetView{}
  584. for _, xSheetView := range xSheetViews.SheetView {
  585. sheetView := SheetView{}
  586. if xSheetView.Pane != nil {
  587. xlsxPane := xSheetView.Pane
  588. pane := &Pane{}
  589. pane.XSplit = xlsxPane.XSplit
  590. pane.YSplit = xlsxPane.YSplit
  591. pane.TopLeftCell = xlsxPane.TopLeftCell
  592. pane.ActivePane = xlsxPane.ActivePane
  593. pane.State = xlsxPane.State
  594. sheetView.Pane = pane
  595. }
  596. sheetViews = append(sheetViews, sheetView)
  597. }
  598. return sheetViews
  599. }
  600. // readSheetFromFile is the logic of converting a xlsxSheet struct
  601. // into a Sheet struct. This work can be done in parallel and so
  602. // readSheetsFromZipFile will spawn an instance of this function per
  603. // sheet and get the results back on the provided channel.
  604. func readSheetFromFile(sc chan *indexedSheet, index int, rsheet xlsxSheet, fi *File, sheetXMLMap map[string]string) (errRes error) {
  605. result := &indexedSheet{Index: index, Sheet: nil, Error: nil}
  606. defer func() {
  607. if e := recover(); e != nil {
  608. switch e.(type) {
  609. case error:
  610. result.Error = e.(error)
  611. errRes = e.(error)
  612. default:
  613. result.Error = errors.New("unexpected error")
  614. }
  615. // The only thing here, is if one close the channel. but its not the case
  616. sc <- result
  617. }
  618. }()
  619. worksheet, error := getWorksheetFromSheet(rsheet, fi.worksheets, sheetXMLMap)
  620. if error != nil {
  621. result.Error = error
  622. sc <- result
  623. return error
  624. }
  625. sheet := new(Sheet)
  626. sheet.File = fi
  627. sheet.Rows, sheet.Cols, sheet.MaxCol, sheet.MaxRow = readRowsFromSheet(worksheet, fi, sheet)
  628. sheet.Hidden = rsheet.State == sheetStateHidden || rsheet.State == sheetStateVeryHidden
  629. sheet.SheetViews = readSheetViews(worksheet.SheetViews)
  630. sheet.SheetFormat.DefaultColWidth = worksheet.SheetFormatPr.DefaultColWidth
  631. sheet.SheetFormat.DefaultRowHeight = worksheet.SheetFormatPr.DefaultRowHeight
  632. sheet.SheetFormat.OutlineLevelCol = worksheet.SheetFormatPr.OutlineLevelCol
  633. sheet.SheetFormat.OutlineLevelRow = worksheet.SheetFormatPr.OutlineLevelRow
  634. result.Sheet = sheet
  635. sc <- result
  636. return nil
  637. }
  638. // readSheetsFromZipFile is an internal helper function that loops
  639. // over the Worksheets defined in the XSLXWorkbook and loads them into
  640. // Sheet objects stored in the Sheets slice of a xlsx.File struct.
  641. func readSheetsFromZipFile(f *zip.File, file *File, sheetXMLMap map[string]string) (map[string]*Sheet, []*Sheet, error) {
  642. var workbook *xlsxWorkbook
  643. var err error
  644. var rc io.ReadCloser
  645. var decoder *xml.Decoder
  646. var sheetCount int
  647. workbook = new(xlsxWorkbook)
  648. rc, err = f.Open()
  649. if err != nil {
  650. return nil, nil, err
  651. }
  652. decoder = xml.NewDecoder(rc)
  653. err = decoder.Decode(workbook)
  654. if err != nil {
  655. return nil, nil, err
  656. }
  657. file.Date1904 = workbook.WorkbookPr.Date1904
  658. for entryNum := range workbook.DefinedNames.DefinedName {
  659. file.DefinedNames = append(file.DefinedNames, &workbook.DefinedNames.DefinedName[entryNum])
  660. }
  661. // Only try and read sheets that have corresponding files.
  662. // Notably this excludes chartsheets don't right now
  663. var workbookSheets []xlsxSheet
  664. for _, sheet := range workbook.Sheets.Sheet {
  665. if f := worksheetFileForSheet(sheet, file.worksheets, sheetXMLMap); f != nil {
  666. workbookSheets = append(workbookSheets, sheet)
  667. }
  668. }
  669. sheetCount = len(workbookSheets)
  670. sheetsByName := make(map[string]*Sheet, sheetCount)
  671. sheets := make([]*Sheet, sheetCount)
  672. sheetChan := make(chan *indexedSheet, sheetCount)
  673. go func() {
  674. defer close(sheetChan)
  675. err = nil
  676. for i, rawsheet := range workbookSheets {
  677. if err := readSheetFromFile(sheetChan, i, rawsheet, file, sheetXMLMap); err != nil {
  678. return
  679. }
  680. }
  681. }()
  682. for j := 0; j < sheetCount; j++ {
  683. sheet := <-sheetChan
  684. if sheet.Error != nil {
  685. return nil, nil, sheet.Error
  686. }
  687. sheetName := workbookSheets[sheet.Index].Name
  688. sheetsByName[sheetName] = sheet.Sheet
  689. sheet.Sheet.Name = sheetName
  690. sheets[sheet.Index] = sheet.Sheet
  691. }
  692. return sheetsByName, sheets, nil
  693. }
  694. // readSharedStringsFromZipFile() is an internal helper function to
  695. // extract a reference table from the sharedStrings.xml file within
  696. // the XLSX zip file.
  697. func readSharedStringsFromZipFile(f *zip.File) (*RefTable, error) {
  698. var sst *xlsxSST
  699. var error error
  700. var rc io.ReadCloser
  701. var decoder *xml.Decoder
  702. var reftable *RefTable
  703. // In a file with no strings it's possible that
  704. // sharedStrings.xml doesn't exist. In this case the value
  705. // passed as f will be nil.
  706. if f == nil {
  707. return nil, nil
  708. }
  709. rc, error = f.Open()
  710. if error != nil {
  711. return nil, error
  712. }
  713. sst = new(xlsxSST)
  714. decoder = xml.NewDecoder(rc)
  715. error = decoder.Decode(sst)
  716. if error != nil {
  717. return nil, error
  718. }
  719. reftable = MakeSharedStringRefTable(sst)
  720. return reftable, nil
  721. }
  722. // readStylesFromZipFile() is an internal helper function to
  723. // extract a style table from the style.xml file within
  724. // the XLSX zip file.
  725. func readStylesFromZipFile(f *zip.File, theme *theme) (*xlsxStyleSheet, error) {
  726. var style *xlsxStyleSheet
  727. var error error
  728. var rc io.ReadCloser
  729. var decoder *xml.Decoder
  730. rc, error = f.Open()
  731. if error != nil {
  732. return nil, error
  733. }
  734. style = newXlsxStyleSheet(theme)
  735. decoder = xml.NewDecoder(rc)
  736. error = decoder.Decode(style)
  737. if error != nil {
  738. return nil, error
  739. }
  740. buildNumFmtRefTable(style)
  741. return style, nil
  742. }
  743. func buildNumFmtRefTable(style *xlsxStyleSheet) {
  744. for _, numFmt := range style.NumFmts.NumFmt {
  745. // We do this for the side effect of populating the NumFmtRefTable.
  746. style.addNumFmt(numFmt)
  747. }
  748. }
  749. func readThemeFromZipFile(f *zip.File) (*theme, error) {
  750. rc, err := f.Open()
  751. if err != nil {
  752. return nil, err
  753. }
  754. var themeXml xlsxTheme
  755. err = xml.NewDecoder(rc).Decode(&themeXml)
  756. if err != nil {
  757. return nil, err
  758. }
  759. return newTheme(themeXml), nil
  760. }
  761. type WorkBookRels map[string]string
  762. func (w *WorkBookRels) MakeXLSXWorkbookRels() xlsxWorkbookRels {
  763. relCount := len(*w)
  764. xWorkbookRels := xlsxWorkbookRels{}
  765. xWorkbookRels.Relationships = make([]xlsxWorkbookRelation, relCount+3)
  766. for k, v := range *w {
  767. index, err := strconv.Atoi(k[3:])
  768. if err != nil {
  769. panic(err.Error())
  770. }
  771. xWorkbookRels.Relationships[index-1] = xlsxWorkbookRelation{
  772. Id: k,
  773. Target: v,
  774. Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"}
  775. }
  776. relCount++
  777. sheetId := fmt.Sprintf("rId%d", relCount)
  778. xWorkbookRels.Relationships[relCount-1] = xlsxWorkbookRelation{
  779. Id: sheetId,
  780. Target: "sharedStrings.xml",
  781. Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"}
  782. relCount++
  783. sheetId = fmt.Sprintf("rId%d", relCount)
  784. xWorkbookRels.Relationships[relCount-1] = xlsxWorkbookRelation{
  785. Id: sheetId,
  786. Target: "theme/theme1.xml",
  787. Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"}
  788. relCount++
  789. sheetId = fmt.Sprintf("rId%d", relCount)
  790. xWorkbookRels.Relationships[relCount-1] = xlsxWorkbookRelation{
  791. Id: sheetId,
  792. Target: "styles.xml",
  793. Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"}
  794. return xWorkbookRels
  795. }
  796. // readWorkbookRelationsFromZipFile is an internal helper function to
  797. // extract a map of relationship ID strings to the name of the
  798. // worksheet.xml file they refer to. The resulting map can be used to
  799. // reliably derefence the worksheets in the XLSX file.
  800. func readWorkbookRelationsFromZipFile(workbookRels *zip.File) (WorkBookRels, error) {
  801. var sheetXMLMap WorkBookRels
  802. var wbRelationships *xlsxWorkbookRels
  803. var rc io.ReadCloser
  804. var decoder *xml.Decoder
  805. var err error
  806. rc, err = workbookRels.Open()
  807. if err != nil {
  808. return nil, err
  809. }
  810. decoder = xml.NewDecoder(rc)
  811. wbRelationships = new(xlsxWorkbookRels)
  812. err = decoder.Decode(wbRelationships)
  813. if err != nil {
  814. return nil, err
  815. }
  816. sheetXMLMap = make(WorkBookRels)
  817. for _, rel := range wbRelationships.Relationships {
  818. if strings.HasSuffix(rel.Target, ".xml") && rel.Type == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" {
  819. _, filename := path.Split(rel.Target)
  820. sheetXMLMap[rel.Id] = strings.Replace(filename, ".xml", "", 1)
  821. }
  822. }
  823. return sheetXMLMap, nil
  824. }
  825. // ReadZip() takes a pointer to a zip.ReadCloser and returns a
  826. // xlsx.File struct populated with its contents. In most cases
  827. // ReadZip is not used directly, but is called internally by OpenFile.
  828. func ReadZip(f *zip.ReadCloser) (*File, error) {
  829. defer f.Close()
  830. return ReadZipReader(&f.Reader)
  831. }
  832. // ReadZipReader() can be used to read an XLSX in memory without
  833. // touching the filesystem.
  834. func ReadZipReader(r *zip.Reader) (*File, error) {
  835. var err error
  836. var file *File
  837. var reftable *RefTable
  838. var sharedStrings *zip.File
  839. var sheetXMLMap map[string]string
  840. var sheetsByName map[string]*Sheet
  841. var sheets []*Sheet
  842. var style *xlsxStyleSheet
  843. var styles *zip.File
  844. var themeFile *zip.File
  845. var v *zip.File
  846. var workbook *zip.File
  847. var workbookRels *zip.File
  848. var worksheets map[string]*zip.File
  849. file = NewFile()
  850. // file.numFmtRefTable = make(map[int]xlsxNumFmt, 1)
  851. worksheets = make(map[string]*zip.File, len(r.File))
  852. for _, v = range r.File {
  853. switch v.Name {
  854. case "xl/sharedStrings.xml":
  855. sharedStrings = v
  856. case "xl/workbook.xml":
  857. workbook = v
  858. case "xl/_rels/workbook.xml.rels":
  859. workbookRels = v
  860. case "xl/styles.xml":
  861. styles = v
  862. case "xl/theme/theme1.xml":
  863. themeFile = v
  864. default:
  865. if len(v.Name) > 14 {
  866. if v.Name[0:13] == "xl/worksheets" {
  867. worksheets[v.Name[14:len(v.Name)-4]] = v
  868. }
  869. }
  870. }
  871. }
  872. if workbookRels == nil {
  873. return nil, fmt.Errorf("xl/_rels/workbook.xml.rels not found in input xlsx.")
  874. }
  875. sheetXMLMap, err = readWorkbookRelationsFromZipFile(workbookRels)
  876. if err != nil {
  877. return nil, err
  878. }
  879. if len(worksheets) == 0 {
  880. return nil, fmt.Errorf("Input xlsx contains no worksheets.")
  881. }
  882. file.worksheets = worksheets
  883. reftable, err = readSharedStringsFromZipFile(sharedStrings)
  884. if err != nil {
  885. return nil, err
  886. }
  887. file.referenceTable = reftable
  888. if themeFile != nil {
  889. theme, err := readThemeFromZipFile(themeFile)
  890. if err != nil {
  891. return nil, err
  892. }
  893. file.theme = theme
  894. }
  895. if styles != nil {
  896. style, err = readStylesFromZipFile(styles, file.theme)
  897. if err != nil {
  898. return nil, err
  899. }
  900. file.styles = style
  901. }
  902. sheetsByName, sheets, err = readSheetsFromZipFile(workbook, file, sheetXMLMap)
  903. if err != nil {
  904. return nil, err
  905. }
  906. if sheets == nil {
  907. readerErr := new(XLSXReaderError)
  908. readerErr.Err = "No sheets found in XLSX File"
  909. return nil, readerErr
  910. }
  911. file.Sheet = sheetsByName
  912. file.Sheets = sheets
  913. return file, nil
  914. }