comment.go 7.3 KB

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