-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
shared_strings.go
77 lines (60 loc) · 1.86 KB
/
shared_strings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) 2017 Andrey Gayvoronsky <[email protected]>
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package xlsx
import (
"github.com/plandem/ooxml"
"github.com/plandem/ooxml/index"
"github.com/plandem/xlsx/internal"
"github.com/plandem/xlsx/internal/ml"
"github.com/plandem/xlsx/internal/ml/primitives"
)
//sharedStrings is a higher level object that wraps ml.SharedStrings with functionality
type sharedStrings struct {
ml ml.SharedStrings
index index.Index
doc *Spreadsheet
file *ooxml.PackageFile
}
func newSharedStrings(f interface{}, doc *Spreadsheet) *sharedStrings {
ss := &sharedStrings{
doc: doc,
}
ss.file = ooxml.NewPackageFile(doc.pkg, f, &ss.ml, nil)
if ss.file.IsNew() {
ss.doc.pkg.ContentTypes().RegisterContent(ss.file.FileName(), internal.ContentTypeSharedStrings)
ss.doc.relationships.AddFile(internal.RelationTypeSharedStrings, ss.file.FileName())
ss.file.MarkAsUpdated()
}
return ss
}
func (ss *sharedStrings) afterLoad() {
for i, s := range ss.ml.StringItem {
_ = ss.index.Add(s, i)
}
}
//get returns string item stored at index
func (ss *sharedStrings) get(index int) *ml.StringItem {
ss.file.LoadIfRequired(ss.afterLoad)
if index < len(ss.ml.StringItem) {
return ss.ml.StringItem[index]
}
return nil
}
//addString adds a new string and return index for it
func (ss *sharedStrings) addString(value string) int {
return ss.addText(&ml.StringItem{Text: primitives.Text(value)})
}
//addText adds a new StringItem and return index for it
func (ss *sharedStrings) addText(si *ml.StringItem) int {
ss.file.LoadIfRequired(ss.afterLoad)
//return sid if already exists
if sid, ok := ss.index.Get(si); ok {
return sid
}
sid := len(ss.ml.StringItem)
ss.ml.StringItem = append(ss.ml.StringItem, si)
_ = ss.index.Add(si, sid)
ss.file.MarkAsUpdated()
return sid
}