Skip to content

Commit

Permalink
spreadsheet: add comment support
Browse files Browse the repository at this point in the history
This adds comment support for sheets.  Excel requires a VML drawing with
the comment box shape for each comment to display the comment.
LibreOffice displays comments fine with or without the shape, and
creates the shape for its own comments.  For the sake of compatibility,
we create comment shapes as well.

I know of no other use for the legacy VML support other than comment
boxes...
  • Loading branch information
tbaliance committed Sep 10, 2017
1 parent 2b50dca commit 3bc4675
Show file tree
Hide file tree
Showing 22 changed files with 613 additions and 71 deletions.
Binary file added _examples/spreadsheet/comments/comments.xlsx
Binary file not shown.
23 changes: 23 additions & 0 deletions _examples/spreadsheet/comments/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2017 Baliance. All rights reserved.
package main

import (
"log"

"baliance.com/gooxml/spreadsheet"
)

func main() {
ss := spreadsheet.New()
sheet := ss.AddSheet()

sheet.Cell("A1").SetString("Hello World!")
sheet.Comments().AddCommentWithStyle("A1", "Gopher", "This looks interesting.")
sheet.Comments().AddCommentWithStyle("C10", "Gopher", "This is a different comment.")

if err := ss.Validate(); err != nil {
log.Fatalf("error validating sheet: %s", err)
}

ss.SaveToFile("comments.xlsx")
}
3 changes: 1 addition & 2 deletions creator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ func TestRawEncode(t *testing.T) {
end := strings.LastIndex(xmlStr, "</w:hdrShapeDefaults>")

gotRaw := xmlStr[beg+20 : end]

exp := "<o:shapedefaults v:ext=\"edit\" spidmax=\"2049\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:v=\"urn:schemas-microsoft-com:vml\"><o:idmap v:ext=\"edit\" data=\"1\"/></o:shapedefaults>"
exp := "<o:shapedefaults xmlns=\"urn:schemas-microsoft-com:office:office\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:s=\"http://schemas.openxmlformats.org/officeDocument/2006/sharedTypes\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" spidmax=\"2049\" ext=\"edit\"/>"
if gotRaw != exp {
t.Errorf("expected\n%q\ngot\n%q\n", exp, gotRaw)
}
Expand Down
38 changes: 20 additions & 18 deletions document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,11 @@ func Read(r io.ReaderAt, size int64) (*Document, error) {
decMap := zippkg.DecodeMap{}
decMap.SetOnNewRelationshipFunc(doc.onNewRelationship)
// we should discover all contents by starting with these two files
decMap.AddTarget(gooxml.ContentTypesFilename, doc.ContentTypes.X())
decMap.AddTarget(gooxml.BaseRelsFilename, doc.Rels.X())
decMap.Decode(files)
decMap.AddTarget(zippkg.Target{Path: gooxml.ContentTypesFilename, Ifc: doc.ContentTypes.X()})
decMap.AddTarget(zippkg.Target{Path: gooxml.BaseRelsFilename, Ifc: doc.Rels.X()})
if err := decMap.Decode(files); err != nil {
return nil, err
}

for _, f := range files {
if f == nil {
Expand Down Expand Up @@ -467,23 +469,23 @@ func (d *Document) FormFields() []FormField {
return ret
}

func (doc *Document) onNewRelationship(decMap *zippkg.DecodeMap, target, typ string, files []*zip.File, rel *relationships.Relationship) error {
func (doc *Document) onNewRelationship(decMap *zippkg.DecodeMap, target, typ string, files []*zip.File, rel *relationships.Relationship, src zippkg.Target) error {
dt := gooxml.DocTypeDocument

switch typ {
case gooxml.OfficeDocumentType:
doc.x = wml.NewDocument()
decMap.AddTarget(target, doc.x)
decMap.AddTarget(zippkg.Target{Path: target, Ifc: doc.x})
// look for the document relationships file as well
decMap.AddTarget(zippkg.RelationsPathFor(target), doc.docRels.X())
decMap.AddTarget(zippkg.Target{Path: zippkg.RelationsPathFor(target), Ifc: doc.docRels.X()})
rel.TargetAttr = gooxml.RelativeFilename(dt, typ, 0)

case gooxml.CorePropertiesType:
decMap.AddTarget(target, doc.CoreProperties.X())
decMap.AddTarget(zippkg.Target{Path: target, Ifc: doc.CoreProperties.X()})
rel.TargetAttr = gooxml.RelativeFilename(dt, typ, 0)

case gooxml.ExtendedPropertiesType:
decMap.AddTarget(target, doc.AppProperties.X())
decMap.AddTarget(zippkg.Target{Path: target, Ifc: doc.AppProperties.X()})
rel.TargetAttr = gooxml.RelativeFilename(dt, typ, 0)

case gooxml.ThumbnailType:
Expand All @@ -507,55 +509,55 @@ func (doc *Document) onNewRelationship(decMap *zippkg.DecodeMap, target, typ str
}

case gooxml.SettingsType:
decMap.AddTarget(target, doc.Settings.X())
decMap.AddTarget(zippkg.Target{Path: target, Ifc: doc.Settings.X()})
rel.TargetAttr = gooxml.RelativeFilename(dt, typ, 0)

case gooxml.NumberingType:
doc.Numbering = NewNumbering()
decMap.AddTarget(target, doc.Numbering.X())
decMap.AddTarget(zippkg.Target{Path: target, Ifc: doc.Numbering.X()})
rel.TargetAttr = gooxml.RelativeFilename(dt, typ, 0)

case gooxml.StylesType:
doc.Styles.Clear()
decMap.AddTarget(target, doc.Styles.X())
decMap.AddTarget(zippkg.Target{Path: target, Ifc: doc.Styles.X()})
rel.TargetAttr = gooxml.RelativeFilename(dt, typ, 0)

case gooxml.HeaderType:
hdr := wml.NewHdr()
decMap.AddTarget(zippkg.Target{Path: target, Ifc: hdr, Index: uint32(len(doc.headers))})
doc.headers = append(doc.headers, hdr)
decMap.AddTarget(target, hdr)
rel.TargetAttr = gooxml.RelativeFilename(dt, typ, len(doc.headers))

case gooxml.FooterType:
ftr := wml.NewFtr()
decMap.AddTarget(zippkg.Target{Path: target, Ifc: ftr, Index: uint32(len(doc.footers))})
doc.footers = append(doc.footers, ftr)
decMap.AddTarget(target, ftr)
rel.TargetAttr = gooxml.RelativeFilename(dt, typ, len(doc.footers))

case gooxml.ThemeType:
thm := dml.NewTheme()
decMap.AddTarget(zippkg.Target{Path: target, Ifc: thm, Index: uint32(len(doc.themes))})
doc.themes = append(doc.themes, thm)
decMap.AddTarget(target, thm)
rel.TargetAttr = gooxml.RelativeFilename(dt, typ, len(doc.themes))

case gooxml.WebSettingsType:
doc.webSettings = wml.NewWebSettings()
decMap.AddTarget(target, doc.webSettings)
decMap.AddTarget(zippkg.Target{Path: target, Ifc: doc.webSettings})
rel.TargetAttr = gooxml.RelativeFilename(dt, typ, 0)

case gooxml.FontTableType:
doc.fontTable = wml.NewFonts()
decMap.AddTarget(target, doc.fontTable)
decMap.AddTarget(zippkg.Target{Path: target, Ifc: doc.fontTable})
rel.TargetAttr = gooxml.RelativeFilename(dt, typ, 0)

case gooxml.EndNotesType:
doc.endNotes = wml.NewEndnotes()
decMap.AddTarget(target, doc.endNotes)
decMap.AddTarget(zippkg.Target{Path: target, Ifc: doc.endNotes})
rel.TargetAttr = gooxml.RelativeFilename(dt, typ, 0)

case gooxml.FootNotesType:
doc.footNotes = wml.NewFootnotes()
decMap.AddTarget(target, doc.footNotes)
decMap.AddTarget(zippkg.Target{Path: target, Ifc: doc.footNotes})
rel.TargetAttr = gooxml.RelativeFilename(dt, typ, 0)

case gooxml.ImageType:
Expand Down
24 changes: 24 additions & 0 deletions filenames.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func RelativeFilename(dt DocType, typ string, index int) string {
return fmt.Sprintf("../charts/chart%d.xml", index)
case DrawingType, DrawingContentType:
return fmt.Sprintf("../drawings/drawing%d.xml", index)
case CommentsType, CommentsContentType:
return fmt.Sprintf("../comments%d.xml", index)

case VMLDrawingType, VMLDrawingContentType:
return fmt.Sprintf("../drawings/vmlDrawing%d.vml", index)

case ThemeType, ThemeContentType:
return fmt.Sprintf("theme/theme%d.xml", index)
Expand Down Expand Up @@ -128,6 +133,8 @@ func AbsoluteFilename(dt DocType, typ string, index int) string {
return "xl/styles.xml"
case DocTypeDocument:
return "word/styles.xml"
default:
log.Printf("unsupported type %s pair and %v", typ, dt)
}

case ChartType, ChartContentType:
Expand All @@ -145,6 +152,23 @@ func AbsoluteFilename(dt DocType, typ string, index int) string {
default:
log.Printf("unsupported type %s pair and %v", typ, dt)
}

case CommentsType, CommentsContentType:
switch dt {
case DocTypeSpreadsheet:
return fmt.Sprintf("xl/comments%d.xml", index)
default:
log.Printf("unsupported type %s pair and %v", typ, dt)
}

case VMLDrawingType, VMLDrawingContentType:
switch dt {
case DocTypeSpreadsheet:
return fmt.Sprintf("xl/drawings/vmlDrawing%d.vml", index)
default:
log.Fatalf("unsupported type %s pair and %v", typ, dt)
}

// SML
case WorksheetType, WorksheetContentType:
return fmt.Sprintf("xl/worksheets/sheet%d.xml", index)
Expand Down
6 changes: 6 additions & 0 deletions optional.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ package gooxml

import "fmt"

// Float32 returns a copy of v as a pointer.
func Float32(v float32) *float32 {
x := v
return &x
}

// Float64 returns a copy of v as a pointer.
func Float64(v float64) *float64 {
x := v
Expand Down
32 changes: 18 additions & 14 deletions schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ package gooxml
// Consts for content types used throughout the package
const (
// Common
OfficeDocumentType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
StylesType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
ThemeType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"
ThemeContentType = "application/vnd.openxmlformats-officedocument.theme+xml"
SettingsType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings"
ImageType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
CommentsType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"
ThumbnailType = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail"
DrawingType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"
DrawingContentType = "application/vnd.openxmlformats-officedocument.drawing+xml"
ChartType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart"
ChartContentType = "application/vnd.openxmlformats-officedocument.drawingml.chart+xml"
HyperLinkType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"

OfficeDocumentType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
StylesType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
ThemeType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"
ThemeContentType = "application/vnd.openxmlformats-officedocument.theme+xml"
SettingsType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings"
ImageType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
CommentsType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"
CommentsContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml"
ThumbnailType = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail"
DrawingType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"
DrawingContentType = "application/vnd.openxmlformats-officedocument.drawing+xml"
ChartType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart"
ChartContentType = "application/vnd.openxmlformats-officedocument.drawingml.chart+xml"
HyperLinkType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
ExtendedPropertiesType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties"
CorePropertiesType = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"

Expand All @@ -50,4 +50,8 @@ const (
SlideMasterContentType = "application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml"
SlideLayoutType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout"
SlideLayoutContentType = "application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml"

// VML
VMLDrawingType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing"
VMLDrawingContentType = "application/vnd.openxmlformats-officedocument.vmlDrawing"
)
50 changes: 50 additions & 0 deletions spreadsheet/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2017 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting [email protected].

package spreadsheet

import sml "baliance.com/gooxml/schema/schemas.openxmlformats.org/spreadsheetml"

// Comment is a single comment within a sheet.
type Comment struct {
w *Workbook
x *sml.CT_Comment
cmts *sml.Comments
}

// X returns the inner wrapped XML type.
func (c Comment) X() *sml.CT_Comment {
return c.x
}

// CellReference returns the cell reference within a sheet that a comment refers
// to (e.g. "A1")
func (c Comment) CellReference() string {
return c.x.RefAttr
}

// SetCellReference sets the cell reference within a sheet that a comment refers
// to (e.g. "A1")
func (c Comment) SetCellReference(cellRef string) {
c.x.RefAttr = cellRef
}

// Author returns the author of the comment
func (c Comment) Author() string {
if c.x.AuthorIdAttr < uint32(len(c.cmts.Authors.Author)) {
return c.cmts.Authors.Author[c.x.AuthorIdAttr]
}
return ""
}

// SetAuthor sets the author of the comment. If the comment body contains the
// author's name (as is the case with Excel and Comments.AddCommentWithStyle, it
// will not be changed). This method only changes the metadata author of the
// comment.
func (c Comment) SetAuthor(author string) {
c.x.AuthorIdAttr = Comments{c.w, c.cmts}.getOrCreateAuthor(author)
}
85 changes: 85 additions & 0 deletions spreadsheet/comments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2017 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting [email protected].

package spreadsheet

import (
"baliance.com/gooxml/color"
sml "baliance.com/gooxml/schema/schemas.openxmlformats.org/spreadsheetml"
"baliance.com/gooxml/vmldrawing"
)

// Comments is the container for comments for a single sheet.
type Comments struct {
w *Workbook
x *sml.Comments
}

// MakeComments constructs a new Comments wrapper.
func MakeComments(w *Workbook, x *sml.Comments) Comments {
return Comments{w, x}
}

// X returns the inner wrapped XML type.
func (c Comments) X() *sml.Comments {
return c.x
}

// Comments returns the list of comments for this sheet
func (c Comments) Comments() []Comment {
ret := []Comment{}
for _, cmt := range c.x.CommentList.Comment {
ret = append(ret, Comment{c.w, cmt, c.x})
}
return ret
}

func (c Comments) getOrCreateAuthor(author string) uint32 {
for i, knownAuthor := range c.x.Authors.Author {
if knownAuthor == author {
return uint32(i)
}
}

// didn't find the author, so add a new one
authIdx := uint32(len(c.x.Authors.Author))
c.x.Authors.Author = append(c.x.Authors.Author, author)
return authIdx
}

// AddComment adds a new comment and returns a RichText which will contain the
// styled comment text.
func (c Comments) AddComment(cellRef string, author string) RichText {

cmt := sml.NewCT_Comment()
c.x.CommentList.Comment = append(c.x.CommentList.Comment, cmt)
cmt.RefAttr = cellRef
cmt.AuthorIdAttr = c.getOrCreateAuthor(author)
cmt.Text = sml.NewCT_Rst()
return RichText{cmt.Text}
}

// AddCommentWithStyle adds a new comment styled in a default way
func (c Comments) AddCommentWithStyle(cellRef string, author string, comment string) {
rt := c.AddComment(cellRef, author)
run := rt.AddRun()
run.SetBold(true)
run.SetSize(10)
run.SetColor(color.Black)
run.SetFont("Calibri")
run.SetText(author + ":")

run = rt.AddRun()
run.SetSize(10)
run.SetFont("Calibri")
run.SetColor(color.Black)
run.SetText("\r\n" + comment + "\r\n")

col, rowIdx, _ := ParseCellReference(cellRef)
colIdx := ColumnToIndex(col)
c.w.vmlDrawings[0].Shape = append(c.w.vmlDrawings[0].Shape, vmldrawing.NewCommentShape(int64(colIdx), int64(rowIdx-1)))
}
Loading

0 comments on commit 3bc4675

Please sign in to comment.