sharedstrings.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package xlsx
  2. // xlsxSST directly maps the sst element from the namespace
  3. // http://schemas.openxmlformats.org/spreadsheetml/2006/main currently
  4. // I have not checked this for completeness - it does as much as need.
  5. type xlsxSST struct {
  6. Count int `xml:"count,attr"`
  7. UniqueCount int `xml:"uniqueCount,attr"`
  8. SI []xlsxSI `xml:"si"`
  9. }
  10. // xlsxSI directly maps the si element from the namespace
  11. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  12. // currently I have not checked this for completeness - it does as
  13. // much as I need.
  14. type xlsxSI struct {
  15. T string `xml:"t"`
  16. R []xlsxR `xml:"r"`
  17. }
  18. // xlsxR directly maps the r element from the namespace
  19. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  20. // currently I have not checked this for completeness - it does as
  21. // much as I need.
  22. type xlsxR struct {
  23. T string `xml:"t"`
  24. }
  25. // // xlsxT directly maps the t element from the namespace
  26. // // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  27. // // currently I have not checked this for completeness - it does as
  28. // // much as I need.
  29. // type xlsxT struct {
  30. // Data string `xml:"chardata"`
  31. // }
  32. type RefTable []string
  33. // NewSharedStringRefTable() creates a new, empty RefTable.
  34. func NewSharedStringRefTable() RefTable {
  35. return RefTable{}
  36. }
  37. // MakeSharedStringRefTable() takes an xlsxSST struct and converts
  38. // it's contents to an slice of strings used to refer to string values
  39. // by numeric index - this is the model used within XLSX worksheet (a
  40. // numeric reference is stored to a shared cell value).
  41. func MakeSharedStringRefTable(source *xlsxSST) RefTable {
  42. reftable := make(RefTable, len(source.SI))
  43. for i, si := range source.SI {
  44. if len(si.R) > 0 {
  45. for j := 0; j < len(si.R); j++ {
  46. reftable[i] = reftable[i] + si.R[j].T
  47. }
  48. } else {
  49. reftable[i] = si.T
  50. }
  51. }
  52. return reftable
  53. }
  54. // makeXlsxSST() takes a RefTable and returns and
  55. // equivalent xlsxSST representation.
  56. func (rt RefTable) makeXlsxSST() xlsxSST {
  57. sst := xlsxSST{}
  58. sst.Count = len(rt)
  59. sst.UniqueCount = sst.Count
  60. for _, ref := range rt {
  61. si := xlsxSI{}
  62. si.T = ref
  63. sst.SI = append(sst.SI, si)
  64. }
  65. return sst
  66. }
  67. // Resolvesharedstring() looks up a string value by numeric index from
  68. // a provided reference table (just a slice of strings in the correct
  69. // order). This function only exists to provide clarity or purpose
  70. // via it's name.
  71. func (rt RefTable) ResolveSharedString(index int) string {
  72. return rt[index]
  73. }