comment.go 5.8 KB

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