forked from unidoc/unioffice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
22 changed files
with
613 additions
and
71 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
} |
Oops, something went wrong.