comment.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package excelize
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. )
  9. // parseFormatCommentsSet provides function to parse the format settings of the
  10. // comment with default value.
  11. func parseFormatCommentsSet(formatSet string) (*formatComment, error) {
  12. format := formatComment{
  13. Author: "Author:",
  14. Text: " ",
  15. }
  16. err := json.Unmarshal([]byte(formatSet), &format)
  17. return &format, err
  18. }
  19. // AddComment provides the method to add comment in a sheet by given worksheet
  20. // index, cell and format set (such as author and text). Note that the max
  21. // author length is 255 and the max text length is 32512. For example, add a
  22. // comment in Sheet1!$A$30:
  23. //
  24. // xlsx.AddComment("Sheet1", "A30", `{"author":"Excelize: ","text":"This is a comment."}`)
  25. //
  26. func (f *File) AddComment(sheet, cell, format string) error {
  27. formatSet, err := parseFormatCommentsSet(format)
  28. if err != nil {
  29. return err
  30. }
  31. // Read sheet data.
  32. xlsx := f.workSheetReader(sheet)
  33. commentID := f.countComments() + 1
  34. drawingVML := "xl/drawings/vmlDrawing" + strconv.Itoa(commentID) + ".vml"
  35. sheetRelationshipsComments := "../comments" + strconv.Itoa(commentID) + ".xml"
  36. sheetRelationshipsDrawingVML := "../drawings/vmlDrawing" + strconv.Itoa(commentID) + ".vml"
  37. if xlsx.LegacyDrawing != nil {
  38. // The worksheet already has a comments relationships, use the relationships drawing ../drawings/vmlDrawing%d.vml.
  39. sheetRelationshipsDrawingVML = f.getSheetRelationshipsTargetByID(sheet, xlsx.LegacyDrawing.RID)
  40. commentID, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(sheetRelationshipsDrawingVML, "../drawings/vmlDrawing"), ".vml"))
  41. drawingVML = strings.Replace(sheetRelationshipsDrawingVML, "..", "xl", -1)
  42. } else {
  43. // Add first comment for given sheet.
  44. rID := f.addSheetRelationships(sheet, SourceRelationshipDrawingVML, sheetRelationshipsDrawingVML, "")
  45. f.addSheetRelationships(sheet, SourceRelationshipComments, sheetRelationshipsComments, "")
  46. f.addSheetLegacyDrawing(sheet, rID)
  47. }
  48. commentsXML := "xl/comments" + strconv.Itoa(commentID) + ".xml"
  49. f.addComment(commentsXML, cell, formatSet)
  50. var colCount int
  51. for i, l := range strings.Split(formatSet.Text, "\n") {
  52. if ll := len(l); ll > colCount {
  53. if i == 0 {
  54. ll += len(formatSet.Author)
  55. }
  56. colCount = ll
  57. }
  58. }
  59. f.addDrawingVML(commentID, drawingVML, cell, strings.Count(formatSet.Text, "\n")+1, colCount)
  60. f.addContentTypePart(commentID, "comments")
  61. return err
  62. }
  63. // addDrawingVML provides function to create comment as
  64. // xl/drawings/vmlDrawing%d.vml by given commit ID and cell.
  65. func (f *File) addDrawingVML(commentID int, drawingVML, cell string, lineCount, colCount int) {
  66. col := string(strings.Map(letterOnlyMapF, cell))
  67. row, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
  68. xAxis := row - 1
  69. yAxis := TitleToNumber(col)
  70. vml := vmlDrawing{
  71. XMLNSv: "urn:schemas-microsoft-com:vml",
  72. XMLNSo: "urn:schemas-microsoft-com:office:office",
  73. XMLNSx: "urn:schemas-microsoft-com:office:excel",
  74. XMLNSmv: "http://macVmlSchemaUri",
  75. Shapelayout: &xlsxShapelayout{
  76. Ext: "edit",
  77. IDmap: &xlsxIDmap{
  78. Ext: "edit",
  79. Data: commentID,
  80. },
  81. },
  82. Shapetype: &xlsxShapetype{
  83. ID: "_x0000_t202",
  84. Coordsize: "21600,21600",
  85. Spt: 202,
  86. Path: "m0,0l0,21600,21600,21600,21600,0xe",
  87. Stroke: &xlsxStroke{
  88. Joinstyle: "miter",
  89. },
  90. VPath: &vPath{
  91. Gradientshapeok: "t",
  92. Connecttype: "miter",
  93. },
  94. },
  95. }
  96. sp := encodeShape{
  97. Fill: &vFill{
  98. Color2: "#fbfe82",
  99. Angle: -180,
  100. Type: "gradient",
  101. Fill: &oFill{
  102. Ext: "view",
  103. Type: "gradientUnscaled",
  104. },
  105. },
  106. Shadow: &vShadow{
  107. On: "t",
  108. Color: "black",
  109. Obscured: "t",
  110. },
  111. Path: &vPath{
  112. Connecttype: "none",
  113. },
  114. Textbox: &vTextbox{
  115. Style: "mso-direction-alt:auto",
  116. Div: &xlsxDiv{
  117. Style: "text-align:left",
  118. },
  119. },
  120. ClientData: &xClientData{
  121. ObjectType: "Note",
  122. Anchor: fmt.Sprintf(
  123. "%d, 23, %d, 0, %d, %d, %d, 5",
  124. 1+yAxis, 1+xAxis, 2+yAxis+lineCount, colCount+yAxis, 2+xAxis+lineCount),
  125. AutoFill: "True",
  126. Row: xAxis,
  127. Column: yAxis,
  128. },
  129. }
  130. s, _ := xml.Marshal(sp)
  131. shape := xlsxShape{
  132. ID: "_x0000_s1025",
  133. Type: "#_x0000_t202",
  134. Style: "position:absolute;73.5pt;width:108pt;height:59.25pt;z-index:1;visibility:hidden",
  135. Fillcolor: "#fbf6d6",
  136. Strokecolor: "#edeaa1",
  137. Val: string(s[13 : len(s)-14]),
  138. }
  139. c, ok := f.XLSX[drawingVML]
  140. if ok {
  141. d := decodeVmlDrawing{}
  142. _ = xml.Unmarshal([]byte(c), &d)
  143. for _, v := range d.Shape {
  144. s := xlsxShape{
  145. ID: "_x0000_s1025",
  146. Type: "#_x0000_t202",
  147. Style: "position:absolute;73.5pt;width:108pt;height:59.25pt;z-index:1;visibility:hidden",
  148. Fillcolor: "#fbf6d6",
  149. Strokecolor: "#edeaa1",
  150. Val: v.Val,
  151. }
  152. vml.Shape = append(vml.Shape, s)
  153. }
  154. }
  155. vml.Shape = append(vml.Shape, shape)
  156. v, _ := xml.Marshal(vml)
  157. f.XLSX[drawingVML] = v
  158. }
  159. // addComment provides function to create chart as xl/comments%d.xml by given
  160. // cell and format sets.
  161. func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) {
  162. a := formatSet.Author
  163. t := formatSet.Text
  164. if len(a) > 255 {
  165. a = a[0:255]
  166. }
  167. if len(t) > 32512 {
  168. t = t[0:32512]
  169. }
  170. comments := xlsxComments{
  171. Authors: []xlsxAuthor{
  172. {
  173. Author: formatSet.Author,
  174. },
  175. },
  176. }
  177. cmt := xlsxComment{
  178. Ref: cell,
  179. AuthorID: 0,
  180. Text: xlsxText{
  181. R: []xlsxR{
  182. {
  183. RPr: &xlsxRPr{
  184. B: " ",
  185. Sz: &attrValFloat{Val: 9},
  186. Color: &xlsxColor{
  187. Indexed: 81,
  188. },
  189. RFont: &attrValString{Val: "Calibri"},
  190. Family: &attrValInt{Val: 2},
  191. },
  192. T: a,
  193. },
  194. {
  195. RPr: &xlsxRPr{
  196. Sz: &attrValFloat{Val: 9},
  197. Color: &xlsxColor{
  198. Indexed: 81,
  199. },
  200. RFont: &attrValString{Val: "Calibri"},
  201. Family: &attrValInt{Val: 2},
  202. },
  203. T: t,
  204. },
  205. },
  206. },
  207. }
  208. c, ok := f.XLSX[commentsXML]
  209. if ok {
  210. d := xlsxComments{}
  211. _ = xml.Unmarshal([]byte(c), &d)
  212. comments.CommentList.Comment = append(comments.CommentList.Comment, d.CommentList.Comment...)
  213. }
  214. comments.CommentList.Comment = append(comments.CommentList.Comment, cmt)
  215. v, _ := xml.Marshal(comments)
  216. f.saveFileList(commentsXML, v)
  217. }
  218. // countComments provides function to get comments files count storage in the
  219. // folder xl.
  220. func (f *File) countComments() int {
  221. count := 0
  222. for k := range f.XLSX {
  223. if strings.Contains(k, "xl/comments") {
  224. count++
  225. }
  226. }
  227. return count
  228. }