improved commenting formatting

formula
Rad Cirskis 7 years ago
parent 3a91b28ddb
commit 741810a863

@ -3,6 +3,7 @@ package excelize
import ( import (
"encoding/json" "encoding/json"
"encoding/xml" "encoding/xml"
"fmt"
"strconv" "strconv"
"strings" "strings"
) )
@ -49,14 +50,23 @@ func (f *File) AddComment(sheet, cell, format string) error {
} }
commentsXML := "xl/comments" + strconv.Itoa(commentID) + ".xml" commentsXML := "xl/comments" + strconv.Itoa(commentID) + ".xml"
f.addComment(commentsXML, cell, formatSet) f.addComment(commentsXML, cell, formatSet)
f.addDrawingVML(commentID, drawingVML, cell) var colCount int
for i, l := range strings.Split(formatSet.Text, "\n") {
if ll := len(l); ll > colCount {
if i == 0 {
ll += len(formatSet.Author)
}
colCount = ll
}
}
f.addDrawingVML(commentID, drawingVML, cell, strings.Count(formatSet.Text, "\n")+1, colCount)
f.addContentTypePart(commentID, "comments") f.addContentTypePart(commentID, "comments")
return err return err
} }
// addDrawingVML provides function to create comment as // addDrawingVML provides function to create comment as
// xl/drawings/vmlDrawing%d.vml by given commit ID and cell. // xl/drawings/vmlDrawing%d.vml by given commit ID and cell.
func (f *File) addDrawingVML(commentID int, drawingVML, cell string) { func (f *File) addDrawingVML(commentID int, drawingVML, cell string, lineCount, colCount int) {
col := string(strings.Map(letterOnlyMapF, cell)) col := string(strings.Map(letterOnlyMapF, cell))
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell)) row, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
xAxis := row - 1 xAxis := row - 1
@ -83,7 +93,7 @@ func (f *File) addDrawingVML(commentID int, drawingVML, cell string) {
}, },
VPath: &vPath{ VPath: &vPath{
Gradientshapeok: "t", Gradientshapeok: "t",
Connecttype: "rect", Connecttype: "miter",
}, },
}, },
} }
@ -113,10 +123,12 @@ func (f *File) addDrawingVML(commentID int, drawingVML, cell string) {
}, },
ClientData: &xClientData{ ClientData: &xClientData{
ObjectType: "Note", ObjectType: "Note",
Anchor: "3, 15, 8, 6, 4, 54, 13, 2", Anchor: fmt.Sprintf(
AutoFill: "False", "%d, 23, %d, 0, %d, %d, %d, 5",
Row: xAxis, 1+yAxis, 1+xAxis, 1+yAxis+lineCount, (colCount*991)/175+5, 2+xAxis),
Column: yAxis, AutoFill: "True",
Row: xAxis,
Column: yAxis,
}, },
} }
s, _ := xml.Marshal(sp) s, _ := xml.Marshal(sp)

Loading…
Cancel
Save