comment.go 7.7 KB

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