comment.go 6.0 KB

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