comment.go 7.7 KB

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