comment.go 6.8 KB

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