File Source for StringTable.php

PHPExcel_Writer_Excel2007

# Source for file StringTable.php

Documentation is available at StringTable.php

1. <?php
2. /**
3. * PHPExcel
4. *
5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25. * @version 1.7.6, 2011-02-27
26. */
29. /**
30. * PHPExcel_Writer_Excel2007_StringTable
31. *
32. * @category PHPExcel
33. * @package PHPExcel_Writer_Excel2007
34. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
35. */
36. class PHPExcel_Writer_Excel2007_StringTable extends PHPExcel_Writer_Excel2007_WriterPart
37. {
38. /**
39. * Create worksheet stringtable
40. *
41. * @param PHPExcel_Worksheet $pSheet Worksheet
42. * @param string[] $pExistingTable Existing table to eventually merge with
43. * @return string[] String table for worksheet
44. * @throws Exception
45. */
46. public function createStringTable($pSheet = null, $pExistingTable = null)
47. {
48. if (! is_null($pSheet)) {
49. // Create string lookup table
50. $aStringTable = array();
51. $cellCollection = null;
52. $aFlippedStringTable = null; // For faster lookup
54. // Is an existing table given?
55. if (! is_null($pExistingTable) && is_array($pExistingTable)) {
56. $aStringTable = $pExistingTable;
57. }
59. // Fill index array
60. $aFlippedStringTable = $this-> flipStringTable($aStringTable);
62. // Loop through cells
63. foreach ($pSheet->getCellCollection() as $cellID) {
64. $cell = $pSheet->getCell($cellID);
65. $cellValue = $cell->getValue();
66. if (! is_object($cellValue) &&
67. ! is_null($cellValue) &&
68. $cellValue !== '' &&
69. !isset($aFlippedStringTable[$cellValue]) &&
70. ($cell->getDataType() == PHPExcel_Cell_DataType:: TYPE_STRING || $cell->getDataType() == PHPExcel_Cell_DataType:: TYPE_STRING2 || $cell->getDataType() == PHPExcel_Cell_DataType:: TYPE_NULL)) {
71. $aStringTable[] = $cellValue;
72. $aFlippedStringTable[$cellValue] = 1;
73. } elseif ($cellValue instanceof PHPExcel_RichText &&
74. ! is_null($cellValue) &&
75. !isset($aFlippedStringTable[$cellValue->getHashCode()])) {
76. $aStringTable[] = $cellValue;
77. $aFlippedStringTable[$cellValue->getHashCode()] = 1;
78. }
79. }
81. // Return
82. return $aStringTable;
83. } else {
84. throw new Exception("Invalid PHPExcel_Worksheet object passed.");
85. }
86. }
88. /**
89. * Write string table to XML format
90. *
91. * @param string[] $pStringTable
92. * @return string XML Output
93. * @throws Exception
94. */
95. public function writeStringTable($pStringTable = null)
96. {
97. if (! is_null($pStringTable)) {
98. // Create XML writer
99. $objWriter = null;
100. if ($this-> getParentWriter()->getUseDiskCaching()) {
101. $objWriter = new PHPExcel_Shared_XMLWriter( PHPExcel_Shared_XMLWriter:: STORAGE_DISK, $this-> getParentWriter()->getDiskCachingDirectory());
102. } else {
103. $objWriter = new PHPExcel_Shared_XMLWriter( PHPExcel_Shared_XMLWriter:: STORAGE_MEMORY);
104. }
106. // XML header
107. $objWriter->startDocument('1.0','UTF-8','yes');
109. // String table
110. $objWriter->startElement('sst');
111. $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
112. $objWriter->writeAttribute('uniqueCount', count($pStringTable));
114. // Loop through string table
115. foreach ($pStringTable as $textElement) {
116. $objWriter->startElement('si');
118. if (! $textElement instanceof PHPExcel_RichText) {
119. $textToWrite = PHPExcel_Shared_String:: ControlCharacterPHP2OOXML( $textElement );
120. $objWriter->startElement('t');
121. if ($textToWrite !== trim($textToWrite)) {
122. $objWriter->writeAttribute('xml:space', 'preserve');
123. }
124. $objWriter->writeRawData($textToWrite);
125. $objWriter->endElement();
126. } else if ($textElement instanceof PHPExcel_RichText) {
127. $this-> writeRichText($objWriter, $textElement);
128. }
130. $objWriter->endElement();
131. }
133. $objWriter->endElement();
135. // Return
136. return $objWriter->getData();
137. } else {
138. throw new Exception("Invalid string table array passed.");
139. }
140. }
142. /**
143. * Write Rich Text
144. *
145. * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer
146. * @param PHPExcel_RichText $pRichText Rich text
147. * @throws Exception
148. */
149. public function writeRichText(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_RichText $pRichText = null)
150. {
151. // Loop through rich text elements
152. $elements = $pRichText->getRichTextElements();
153. foreach ($elements as $element) {
154. // r
155. $objWriter->startElement('r');
157. // rPr
158. if ($element instanceof PHPExcel_RichText_Run) {
159. // rPr
160. $objWriter->startElement('rPr');
162. // rFont
163. $objWriter->startElement('rFont');
164. $objWriter->writeAttribute('val', $element->getFont()->getName());
165. $objWriter->endElement();
167. // Bold
168. $objWriter->startElement('b');
169. $objWriter->writeAttribute('val', ($element->getFont()->getBold() ? 'true' : 'false'));
170. $objWriter->endElement();
172. // Italic
173. $objWriter->startElement('i');
174. $objWriter->writeAttribute('val', ($element->getFont()->getItalic() ? 'true' : 'false'));
175. $objWriter->endElement();
177. // Superscript / subscript
178. if ($element->getFont()->getSuperScript() || $element->getFont()->getSubScript()) {
179. $objWriter->startElement('vertAlign');
180. if ($element->getFont()->getSuperScript()) {
181. $objWriter->writeAttribute('val', 'superscript');
182. } else if ($element->getFont()->getSubScript()) {
183. $objWriter->writeAttribute('val', 'subscript');
184. }
185. $objWriter->endElement();
186. }
188. // Strikethrough
189. $objWriter->startElement('strike');
190. $objWriter->writeAttribute('val', ($element->getFont()->getStrikethrough() ? 'true' : 'false'));
191. $objWriter->endElement();
193. // Color
194. $objWriter->startElement('color');
195. $objWriter->writeAttribute('rgb', $element->getFont()->getColor()->getARGB());
196. $objWriter->endElement();
198. // Size
199. $objWriter->startElement('sz');
200. $objWriter->writeAttribute('val', $element->getFont()->getSize());
201. $objWriter->endElement();
203. // Underline
204. $objWriter->startElement('u');
205. $objWriter->writeAttribute('val', $element->getFont()->getUnderline());
206. $objWriter->endElement();
208. $objWriter->endElement();
209. }
211. // t
212. $objWriter->startElement('t');
213. $objWriter->writeAttribute('xml:space', 'preserve');
214. $objWriter->writeRawData( PHPExcel_Shared_String:: ControlCharacterPHP2OOXML( $element->getText() ));
215. $objWriter->endElement();
217. $objWriter->endElement();
218. }
219. }
221. /**
222. * Flip string table (for index searching)
223. *
224. * @param array $stringTable Stringtable
225. * @return array
226. */
227. public function flipStringTable($stringTable = array()) {
228. // Return value
229. $returnValue = array();
231. // Loop through stringtable and add flipped items to $returnValue
232. foreach ($stringTable as $key => $value) {
233. if (! $value instanceof PHPExcel_RichText) {
234. $returnValue[$value] = $key;
235. } else if ($value instanceof PHPExcel_RichText) {
236. $returnValue[$value->getHashCode()] = $key;
237. }
238. }
240. // Return
241. return $returnValue;
242. }
243. }

Documentation generated on Sun, 27 Feb 2011 16:34:16 -0800 by phpDocumentor 1.4.3