notes.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package xlsx
  2. /*
  3. notes.go - Cell note (comment) support for the Sheets webapp.
  4. Webapp cells carry an optional "n" note string. On export every noted
  5. cell becomes a classic xlsx comment: xl/commentsN.xml holds the text
  6. and a companion VML part (xl/drawings/vmlDrawingN.vml, referenced as
  7. the sheet's legacyDrawing) gives Excel the hidden note box to show on
  8. hover. On import, commentsN.xml maps back to the "n" field.
  9. */
  10. import (
  11. "fmt"
  12. "sort"
  13. "strings"
  14. "imuslab.com/arozos/mod/office/internal/xmltree"
  15. "imuslab.com/arozos/mod/office/model"
  16. )
  17. type xlsxNote struct {
  18. ref string
  19. col, row int
  20. text string
  21. }
  22. // sheetNotes collects the sheet's notes in a deterministic order
  23. func sheetNotes(ws *model.WorkSheet) []xlsxNote {
  24. var out []xlsxNote
  25. for k, cell := range ws.Cells {
  26. if cell == nil || strings.TrimSpace(cell.N) == "" {
  27. continue
  28. }
  29. col, row, ok := model.ParseCellRef(k)
  30. if !ok {
  31. continue
  32. }
  33. out = append(out, xlsxNote{ref: model.CellRef(col, row), col: col, row: row, text: cell.N})
  34. }
  35. sort.Slice(out, func(a, b int) bool {
  36. if out[a].row != out[b].row {
  37. return out[a].row < out[b].row
  38. }
  39. return out[a].col < out[b].col
  40. })
  41. return out
  42. }
  43. func buildCommentsXML(notes []xlsxNote) string {
  44. var sb strings.Builder
  45. sb.WriteString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` + "\n")
  46. sb.WriteString(`<comments xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">`)
  47. sb.WriteString(`<authors><author>ArozOS Sheets</author></authors><commentList>`)
  48. for _, n := range notes {
  49. sb.WriteString(`<comment ref="` + n.ref + `" authorId="0"><text><r>` +
  50. `<rPr><sz val="9"/><rFont val="Tahoma"/></rPr>` +
  51. `<t xml:space="preserve">` + xmltree.Escape(n.text) + `</t></r></text></comment>`)
  52. }
  53. sb.WriteString(`</commentList></comments>`)
  54. return sb.String()
  55. }
  56. // buildVmlXML renders the legacy drawing part Excel needs to display the
  57. // note boxes (hidden, shown on hover, anchored next to their cell)
  58. func buildVmlXML(notes []xlsxNote) string {
  59. var sb strings.Builder
  60. sb.WriteString(`<xml xmlns:v="urn:schemas-microsoft-com:vml"` +
  61. ` xmlns:o="urn:schemas-microsoft-com:office:office"` +
  62. ` xmlns:x="urn:schemas-microsoft-com:office:excel">` +
  63. `<o:shapelayout v:ext="edit"><o:idmap v:ext="edit" data="1"/></o:shapelayout>` +
  64. `<v:shapetype id="_x0000_t202" coordsize="21600,21600" o:spt="202" path="m,l,21600r21600,l21600,xe">` +
  65. `<v:stroke joinstyle="miter"/><v:path gradientshapeok="t" o:connecttype="rect"/></v:shapetype>`)
  66. for i, n := range notes {
  67. // anchor the note box one column right / same row, sized 3x3 cells
  68. anchor := fmt.Sprintf("%d, 15, %d, 2, %d, 15, %d, 16",
  69. n.col+1, maxInt(0, n.row), n.col+3, n.row+3)
  70. sb.WriteString(fmt.Sprintf(`<v:shape id="_x0000_s%d" type="#_x0000_t202"`+
  71. ` style="position:absolute;margin-left:80pt;margin-top:2pt;width:104pt;height:52pt;`+
  72. `z-index:%d;visibility:hidden" fillcolor="#ffffe1" o:insetmode="auto">`,
  73. 1025+i, i+1))
  74. sb.WriteString(`<v:fill color2="#ffffe1"/><v:shadow on="t" color="black" obscured="t"/>` +
  75. `<v:path o:connecttype="none"/><v:textbox style="mso-direction-alt:auto"/>` +
  76. `<x:ClientData ObjectType="Note"><x:MoveWithCells/><x:SizeWithCells/>` +
  77. `<x:Anchor>` + anchor + `</x:Anchor><x:AutoFill>False</x:AutoFill>` +
  78. fmt.Sprintf(`<x:Row>%d</x:Row><x:Column>%d</x:Column>`, n.row, n.col) +
  79. `</x:ClientData></v:shape>`)
  80. }
  81. sb.WriteString(`</xml>`)
  82. return sb.String()
  83. }
  84. // parseSheetComments reads a worksheet's comments part (if any) back into
  85. // the cells' "n" note field
  86. func parseSheetComments(files map[string][]byte, sheetPart string, ws *model.WorkSheet) {
  87. sheetDir := pathDir(sheetPart)
  88. relsRaw, ok := files[sheetDir+"/_rels/"+pathBase(sheetPart)+".rels"]
  89. if !ok {
  90. return
  91. }
  92. relsTree, err := xmltree.Parse(relsRaw)
  93. if err != nil {
  94. return
  95. }
  96. commentsPart := ""
  97. for _, rel := range relsTree.All("Relationship") {
  98. if strings.HasSuffix(rel.Attr("Type"), "/comments") {
  99. commentsPart = xmltree.ResolvePart(sheetDir, rel.Attr("Target"))
  100. }
  101. }
  102. if commentsPart == "" {
  103. return
  104. }
  105. tree, err := xmltree.Parse(files[commentsPart])
  106. if err != nil {
  107. return
  108. }
  109. cl := tree.First("commentList")
  110. if cl == nil {
  111. return
  112. }
  113. for _, cm := range cl.All("comment") {
  114. col, row, ok := model.ParseCellRef(cm.Attr("ref"))
  115. if !ok {
  116. continue
  117. }
  118. var texts []string
  119. if t := cm.First("text"); t != nil {
  120. xmltree.CollectText(t, &texts)
  121. }
  122. note := strings.TrimSpace(strings.Join(texts, ""))
  123. if note == "" {
  124. continue
  125. }
  126. k := model.CellRef(col, row)
  127. if ws.Cells[k] == nil {
  128. ws.Cells[k] = &model.WorkCell{}
  129. }
  130. ws.Cells[k].N = note
  131. }
  132. }