Comments.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * PHPExcel_Writer_Excel2007_Comments
  4. *
  5. * Copyright (c) 2006 - 2015 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Writer_Excel2007
  23. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. class PHPExcel_Writer_Excel2007_Comments extends PHPExcel_Writer_Excel2007_WriterPart
  28. {
  29. /**
  30. * Write comments to XML format
  31. *
  32. * @param PHPExcel_Worksheet $pWorksheet
  33. * @return string XML Output
  34. * @throws PHPExcel_Writer_Exception
  35. */
  36. public function writeComments(PHPExcel_Worksheet $pWorksheet = null)
  37. {
  38. // Create XML writer
  39. $objWriter = null;
  40. if ($this->getParentWriter()->getUseDiskCaching()) {
  41. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  42. } else {
  43. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  44. }
  45. // XML header
  46. $objWriter->startDocument('1.0', 'UTF-8', 'yes');
  47. // Comments cache
  48. $comments = $pWorksheet->getComments();
  49. // Authors cache
  50. $authors = array();
  51. $authorId = 0;
  52. foreach ($comments as $comment) {
  53. if (!isset($authors[$comment->getAuthor()])) {
  54. $authors[$comment->getAuthor()] = $authorId++;
  55. }
  56. }
  57. // comments
  58. $objWriter->startElement('comments');
  59. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
  60. // Loop through authors
  61. $objWriter->startElement('authors');
  62. foreach ($authors as $author => $index) {
  63. $objWriter->writeElement('author', $author);
  64. }
  65. $objWriter->endElement();
  66. // Loop through comments
  67. $objWriter->startElement('commentList');
  68. foreach ($comments as $key => $value) {
  69. $this->writeComment($objWriter, $key, $value, $authors);
  70. }
  71. $objWriter->endElement();
  72. $objWriter->endElement();
  73. // Return
  74. return $objWriter->getData();
  75. }
  76. /**
  77. * Write comment to XML format
  78. *
  79. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  80. * @param string $pCellReference Cell reference
  81. * @param PHPExcel_Comment $pComment Comment
  82. * @param array $pAuthors Array of authors
  83. * @throws PHPExcel_Writer_Exception
  84. */
  85. private function writeComment(PHPExcel_Shared_XMLWriter $objWriter = null, $pCellReference = 'A1', PHPExcel_Comment $pComment = null, $pAuthors = null)
  86. {
  87. // comment
  88. $objWriter->startElement('comment');
  89. $objWriter->writeAttribute('ref', $pCellReference);
  90. $objWriter->writeAttribute('authorId', $pAuthors[$pComment->getAuthor()]);
  91. // text
  92. $objWriter->startElement('text');
  93. $this->getParentWriter()->getWriterPart('stringtable')->writeRichText($objWriter, $pComment->getText());
  94. $objWriter->endElement();
  95. $objWriter->endElement();
  96. }
  97. /**
  98. * Write VML comments to XML format
  99. *
  100. * @param PHPExcel_Worksheet $pWorksheet
  101. * @return string XML Output
  102. * @throws PHPExcel_Writer_Exception
  103. */
  104. public function writeVMLComments(PHPExcel_Worksheet $pWorksheet = null)
  105. {
  106. // Create XML writer
  107. $objWriter = null;
  108. if ($this->getParentWriter()->getUseDiskCaching()) {
  109. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
  110. } else {
  111. $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
  112. }
  113. // XML header
  114. $objWriter->startDocument('1.0', 'UTF-8', 'yes');
  115. // Comments cache
  116. $comments = $pWorksheet->getComments();
  117. // xml
  118. $objWriter->startElement('xml');
  119. $objWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
  120. $objWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
  121. $objWriter->writeAttribute('xmlns:x', 'urn:schemas-microsoft-com:office:excel');
  122. // o:shapelayout
  123. $objWriter->startElement('o:shapelayout');
  124. $objWriter->writeAttribute('v:ext', 'edit');
  125. // o:idmap
  126. $objWriter->startElement('o:idmap');
  127. $objWriter->writeAttribute('v:ext', 'edit');
  128. $objWriter->writeAttribute('data', '1');
  129. $objWriter->endElement();
  130. $objWriter->endElement();
  131. // v:shapetype
  132. $objWriter->startElement('v:shapetype');
  133. $objWriter->writeAttribute('id', '_x0000_t202');
  134. $objWriter->writeAttribute('coordsize', '21600,21600');
  135. $objWriter->writeAttribute('o:spt', '202');
  136. $objWriter->writeAttribute('path', 'm,l,21600r21600,l21600,xe');
  137. // v:stroke
  138. $objWriter->startElement('v:stroke');
  139. $objWriter->writeAttribute('joinstyle', 'miter');
  140. $objWriter->endElement();
  141. // v:path
  142. $objWriter->startElement('v:path');
  143. $objWriter->writeAttribute('gradientshapeok', 't');
  144. $objWriter->writeAttribute('o:connecttype', 'rect');
  145. $objWriter->endElement();
  146. $objWriter->endElement();
  147. // Loop through comments
  148. foreach ($comments as $key => $value) {
  149. $this->writeVMLComment($objWriter, $key, $value);
  150. }
  151. $objWriter->endElement();
  152. // Return
  153. return $objWriter->getData();
  154. }
  155. /**
  156. * Write VML comment to XML format
  157. *
  158. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
  159. * @param string $pCellReference Cell reference
  160. * @param PHPExcel_Comment $pComment Comment
  161. * @throws PHPExcel_Writer_Exception
  162. */
  163. private function writeVMLComment(PHPExcel_Shared_XMLWriter $objWriter = null, $pCellReference = 'A1', PHPExcel_Comment $pComment = null)
  164. {
  165. // Metadata
  166. list($column, $row) = PHPExcel_Cell::coordinateFromString($pCellReference);
  167. $column = PHPExcel_Cell::columnIndexFromString($column);
  168. $id = 1024 + $column + $row;
  169. $id = substr($id, 0, 4);
  170. // v:shape
  171. $objWriter->startElement('v:shape');
  172. $objWriter->writeAttribute('id', '_x0000_s' . $id);
  173. $objWriter->writeAttribute('type', '#_x0000_t202');
  174. $objWriter->writeAttribute('style', 'position:absolute;margin-left:' . $pComment->getMarginLeft() . ';margin-top:' . $pComment->getMarginTop() . ';width:' . $pComment->getWidth() . ';height:' . $pComment->getHeight() . ';z-index:1;visibility:' . ($pComment->getVisible() ? 'visible' : 'hidden'));
  175. $objWriter->writeAttribute('fillcolor', '#' . $pComment->getFillColor()->getRGB());
  176. $objWriter->writeAttribute('o:insetmode', 'auto');
  177. // v:fill
  178. $objWriter->startElement('v:fill');
  179. $objWriter->writeAttribute('color2', '#' . $pComment->getFillColor()->getRGB());
  180. $objWriter->endElement();
  181. // v:shadow
  182. $objWriter->startElement('v:shadow');
  183. $objWriter->writeAttribute('on', 't');
  184. $objWriter->writeAttribute('color', 'black');
  185. $objWriter->writeAttribute('obscured', 't');
  186. $objWriter->endElement();
  187. // v:path
  188. $objWriter->startElement('v:path');
  189. $objWriter->writeAttribute('o:connecttype', 'none');
  190. $objWriter->endElement();
  191. // v:textbox
  192. $objWriter->startElement('v:textbox');
  193. $objWriter->writeAttribute('style', 'mso-direction-alt:auto');
  194. // div
  195. $objWriter->startElement('div');
  196. $objWriter->writeAttribute('style', 'text-align:left');
  197. $objWriter->endElement();
  198. $objWriter->endElement();
  199. // x:ClientData
  200. $objWriter->startElement('x:ClientData');
  201. $objWriter->writeAttribute('ObjectType', 'Note');
  202. // x:MoveWithCells
  203. $objWriter->writeElement('x:MoveWithCells', '');
  204. // x:SizeWithCells
  205. $objWriter->writeElement('x:SizeWithCells', '');
  206. // x:Anchor
  207. //$objWriter->writeElement('x:Anchor', $column . ', 15, ' . ($row - 2) . ', 10, ' . ($column + 4) . ', 15, ' . ($row + 5) . ', 18');
  208. // x:AutoFill
  209. $objWriter->writeElement('x:AutoFill', 'False');
  210. // x:Row
  211. $objWriter->writeElement('x:Row', ($row - 1));
  212. // x:Column
  213. $objWriter->writeElement('x:Column', ($column - 1));
  214. $objWriter->endElement();
  215. $objWriter->endElement();
  216. }
  217. }