package xlsx
/*
notes.go - Cell note (comment) support for the Sheets webapp.
Webapp cells carry an optional "n" note string. On export every noted
cell becomes a classic xlsx comment: xl/commentsN.xml holds the text
and a companion VML part (xl/drawings/vmlDrawingN.vml, referenced as
the sheet's legacyDrawing) gives Excel the hidden note box to show on
hover. On import, commentsN.xml maps back to the "n" field.
*/
import (
"fmt"
"sort"
"strings"
"imuslab.com/arozos/mod/office/internal/xmltree"
"imuslab.com/arozos/mod/office/model"
)
type xlsxNote struct {
ref string
col, row int
text string
}
// sheetNotes collects the sheet's notes in a deterministic order
func sheetNotes(ws *model.WorkSheet) []xlsxNote {
var out []xlsxNote
for k, cell := range ws.Cells {
if cell == nil || strings.TrimSpace(cell.N) == "" {
continue
}
col, row, ok := model.ParseCellRef(k)
if !ok {
continue
}
out = append(out, xlsxNote{ref: model.CellRef(col, row), col: col, row: row, text: cell.N})
}
sort.Slice(out, func(a, b int) bool {
if out[a].row != out[b].row {
return out[a].row < out[b].row
}
return out[a].col < out[b].col
})
return out
}
func buildCommentsXML(notes []xlsxNote) string {
var sb strings.Builder
sb.WriteString(`` + "\n")
sb.WriteString(``)
sb.WriteString(`ArozOS Sheets`)
for _, n := range notes {
sb.WriteString(`` +
`` +
`` + xmltree.Escape(n.text) + ``)
}
sb.WriteString(``)
return sb.String()
}
// buildVmlXML renders the legacy drawing part Excel needs to display the
// note boxes (hidden, shown on hover, anchored next to their cell)
func buildVmlXML(notes []xlsxNote) string {
var sb strings.Builder
sb.WriteString(`` +
`` +
`` +
``)
for i, n := range notes {
// anchor the note box one column right / same row, sized 3x3 cells
anchor := fmt.Sprintf("%d, 15, %d, 2, %d, 15, %d, 16",
n.col+1, maxInt(0, n.row), n.col+3, n.row+3)
sb.WriteString(fmt.Sprintf(``,
1025+i, i+1))
sb.WriteString(`` +
`` +
`` +
`` + anchor + `False` +
fmt.Sprintf(`%d%d`, n.row, n.col) +
``)
}
sb.WriteString(``)
return sb.String()
}
// parseSheetComments reads a worksheet's comments part (if any) back into
// the cells' "n" note field
func parseSheetComments(files map[string][]byte, sheetPart string, ws *model.WorkSheet) {
sheetDir := pathDir(sheetPart)
relsRaw, ok := files[sheetDir+"/_rels/"+pathBase(sheetPart)+".rels"]
if !ok {
return
}
relsTree, err := xmltree.Parse(relsRaw)
if err != nil {
return
}
commentsPart := ""
for _, rel := range relsTree.All("Relationship") {
if strings.HasSuffix(rel.Attr("Type"), "/comments") {
commentsPart = xmltree.ResolvePart(sheetDir, rel.Attr("Target"))
}
}
if commentsPart == "" {
return
}
tree, err := xmltree.Parse(files[commentsPart])
if err != nil {
return
}
cl := tree.First("commentList")
if cl == nil {
return
}
for _, cm := range cl.All("comment") {
col, row, ok := model.ParseCellRef(cm.Attr("ref"))
if !ok {
continue
}
var texts []string
if t := cm.First("text"); t != nil {
xmltree.CollectText(t, &texts)
}
note := strings.TrimSpace(strings.Join(texts, ""))
if note == "" {
continue
}
k := model.CellRef(col, row)
if ws.Cells[k] == nil {
ws.Cells[k] = &model.WorkCell{}
}
ws.Cells[k].N = note
}
}