sharedstrings.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 I need.
  8. type xlsxSST struct {
  9. XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main 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. isWrite bool
  33. }
  34. // NewSharedStringRefTable() creates a new, empty RefTable.
  35. func NewSharedStringRefTable() *RefTable {
  36. rt := RefTable{}
  37. rt.knownStrings = make(map[string]int)
  38. return &rt
  39. }
  40. // MakeSharedStringRefTable() takes an xlsxSST struct and converts
  41. // it's contents to an slice of strings used to refer to string values
  42. // by numeric index - this is the model used within XLSX worksheet (a
  43. // numeric reference is stored to a shared cell value).
  44. func MakeSharedStringRefTable(source *xlsxSST) *RefTable {
  45. reftable := NewSharedStringRefTable()
  46. reftable.isWrite = false
  47. for _, si := range source.SI {
  48. if len(si.R) > 0 {
  49. newString := ""
  50. for j := 0; j < len(si.R); j++ {
  51. newString = newString + si.R[j].T
  52. }
  53. reftable.AddString(newString)
  54. } else {
  55. reftable.AddString(si.T)
  56. }
  57. }
  58. return reftable
  59. }
  60. // makeXlsxSST() takes a RefTable and returns and
  61. // equivalent xlsxSST representation.
  62. func (rt *RefTable) makeXLSXSST() xlsxSST {
  63. sst := xlsxSST{}
  64. sst.Count = len(rt.indexedStrings)
  65. sst.UniqueCount = sst.Count
  66. for _, ref := range rt.indexedStrings {
  67. si := xlsxSI{}
  68. si.T = ref
  69. sst.SI = append(sst.SI, si)
  70. }
  71. return sst
  72. }
  73. // Resolvesharedstring() looks up a string value by numeric index from
  74. // a provided reference table (just a slice of strings in the correct
  75. // order). This function only exists to provide clarity or purpose
  76. // via it's name.
  77. func (rt *RefTable) ResolveSharedString(index int) string {
  78. return rt.indexedStrings[index]
  79. }
  80. // AddString adds a string to the reference table and return it's
  81. // numeric index. If the string already exists then it simply returns
  82. // the existing index.
  83. func (rt *RefTable) AddString(str string) int {
  84. if rt.isWrite {
  85. index, ok := rt.knownStrings[str]
  86. if ok {
  87. return index
  88. }
  89. }
  90. rt.indexedStrings = append(rt.indexedStrings, str)
  91. index := len(rt.indexedStrings) - 1
  92. rt.knownStrings[str] = index
  93. return index
  94. }
  95. func (rt *RefTable) Length() int {
  96. return len(rt.indexedStrings)
  97. }