comment.go 5.9 KB

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