-
Notifications
You must be signed in to change notification settings - Fork 0
/
x_relationships.go
111 lines (98 loc) · 2.74 KB
/
x_relationships.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package packaging
import (
"encoding/xml"
"fmt"
"path"
)
// Relationships Defines
const (
WorkbookRelationshipsPath = "xl/_rels"
WorkbookRelationshipsFileName = "workbook.xml.rels"
RootRelationshipsPath = "_rels"
RootRelationshipsFileName = ".rels"
)
// XRelationships .rels XMLDocument type
type XRelationships struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/package/2006/relationships Relationships"`
Relationships []*XRelationship `xml:"Relationship"`
}
// XRelationship Relationship type
type XRelationship struct {
ID string `xml:"Id,attr"`
Type string `xml:"Type,attr"`
Target string `xml:"Target,attr"`
Index int `xml:"-"`
}
// NewWorkbookXRelationships create *XRelationships from Worksheets and Themes
func NewWorkbookXRelationships(file *XFile) (workbookRelations *XRelationships) {
workbookRelations = &XRelationships{
Relationships: []*XRelationship{},
}
rID := 0
// worksheets/sheet?.xml
for i := range file.Worksheets {
sheetIndex := i + 1
rID++
workbookRelations.Relationships = append(workbookRelations.Relationships, &XRelationship{
ID: fmt.Sprintf("rId%d", rID),
Type: WorksheetRelationshipType,
Target: fmt.Sprintf(WorksheetFileName, sheetIndex),
Index: rID,
})
}
// theme/theme?.xml
for i := range file.Themes {
themeIndex := i + 1
rID++
workbookRelations.Relationships = append(workbookRelations.Relationships, &XRelationship{
ID: fmt.Sprintf("rId%d", rID),
Type: ThemeRelationshipType,
Target: fmt.Sprintf(ThemeFileName, themeIndex),
Index: rID,
})
}
// styles.xml
rID++
workbookRelations.Relationships = append(workbookRelations.Relationships, &XRelationship{
ID: fmt.Sprintf("rId%d", rID),
Type: StyleSheetRelationshipType,
Target: StyleSheetFileName,
Index: rID,
})
// sharedStrings.xml
if file.SharedStrings.Count > 0 {
rID++
workbookRelations.Relationships = append(workbookRelations.Relationships, &XRelationship{
ID: fmt.Sprintf("rId%d", rID),
Type: SharedStringsRelationshipType,
Target: SharedStringsFileName,
Index: rID,
})
}
return
}
// NewDefaultRootXRelationships create *XRelationships with default template
func NewDefaultRootXRelationships() *XRelationships {
return &XRelationships{
Relationships: []*XRelationship{
{
ID: "rId1",
Type: WorkbookRelationshipType,
Target: path.Join(WorkbookPath, WorkbookFileName),
Index: 1,
},
{
ID: "rId2",
Type: CorePropertiesRelationshipType,
Target: path.Join(CorePropertiesPath, CorePropertiesFileName),
Index: 2,
},
{
ID: "rId3",
Type: ExtendedPropertiesRelationshipType,
Target: path.Join(ExtendedPropertiesPath, ExtendedPropertiesFileName),
Index: 3,
},
},
}
}