sharedstrings.go 2.9 KB

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