Skip to content

Commit 1382069

Browse files
committed
spreadsheet: save worksheet relationships
1 parent a06fea9 commit 1382069

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

spreadsheet/workbook.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ func New() *Workbook {
5757
wb.wbRels.AddRelationship("styles.xml", common.StylesType)
5858

5959
wb.ContentTypes = common.NewContentTypes()
60-
wb.ContentTypes.AddOverride("/xl/workbook.xml", "application/vnd.openxmlformats-officedocument.sml.sheet.main+xml")
61-
wb.ContentTypes.AddOverride("/xl/styles.xml", "application/vnd.openxmlformats-officedocument.sml.styles+xml")
60+
wb.ContentTypes.AddOverride("/xl/workbook.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml")
61+
wb.ContentTypes.AddOverride("/xl/styles.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml")
6262

6363
wb.SharedStrings = NewSharedStrings()
6464
wb.ContentTypes.AddOverride("/xl/sharedStrings.xml", common.SharedStringsContentType)
@@ -225,17 +225,17 @@ func (wb *Workbook) AddSheet() Sheet {
225225
ws.Dimension = sml.NewCT_SheetDimension()
226226
ws.Dimension.RefAttr = "A1"
227227
wb.xws = append(wb.xws, ws)
228-
228+
wb.xwsRels = append(wb.xwsRels, common.NewRelationships())
229229
ws.SheetData = sml.NewCT_SheetData()
230230

231231
// update the references
232-
rid := wb.wbRels.AddRelationship(fmt.Sprintf("worksheets/sheet%d.xml", rs.SheetIdAttr),
232+
rid := wb.wbRels.AddRelationship(fmt.Sprintf("worksheets/sheet%d.xml", len(wb.x.Sheets.Sheet)),
233233
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet")
234234
rs.IdAttr = rid.ID()
235235

236236
// add the content type
237-
wb.ContentTypes.AddOverride(fmt.Sprintf("/xl/worksheets/sheet%d.xml", rs.SheetIdAttr),
238-
"application/vnd.openxmlformats-officedocument.sml.worksheet+xml")
237+
wb.ContentTypes.AddOverride(fmt.Sprintf("/xl/worksheets/sheet%d.xml", len(wb.x.Sheets.Sheet)),
238+
"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml")
239239

240240
return Sheet{wb, rs, ws}
241241
}
@@ -284,8 +284,9 @@ func (wb *Workbook) Save(w io.Writer) error {
284284
}
285285
}
286286
for i, sheet := range wb.xws {
287-
wbs := wb.x.Sheets.Sheet[i]
288-
zippkg.MarshalXML(z, fmt.Sprintf("xl/worksheets/sheet%d.xml", wbs.SheetIdAttr), sheet)
287+
fn := fmt.Sprintf("xl/worksheets/sheet%d.xml", i+1)
288+
zippkg.MarshalXML(z, fn, sheet)
289+
zippkg.MarshalXML(z, zippkg.RelationsPathFor(fn), wb.xwsRels[i].X())
289290
}
290291
if wb.Thumbnail != nil {
291292
tn, err := z.Create("docProps/thumbnail.jpeg")
@@ -296,7 +297,9 @@ func (wb *Workbook) Save(w io.Writer) error {
296297
return err
297298
}
298299
}
299-
wb.WriteExtraFiles(z)
300+
if err := wb.WriteExtraFiles(z); err != nil {
301+
return err
302+
}
300303
return z.Close()
301304
}
302305

0 commit comments

Comments
 (0)