ExportArrayTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Style;
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  4. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  5. use PhpOffice\PhpSpreadsheet\Style\Border;
  6. use PhpOffice\PhpSpreadsheet\Style\Color;
  7. use PhpOffice\PhpSpreadsheet\Style\Fill;
  8. use PhpOffice\PhpSpreadsheet\Style\Font;
  9. use PhpOffice\PhpSpreadsheet\Style\Protection;
  10. use PHPUnit\Framework\TestCase;
  11. class ExportArrayTest extends TestCase
  12. {
  13. public function testStyleCopy(): void
  14. {
  15. $spreadsheet = new Spreadsheet();
  16. $sheet = $spreadsheet->getActiveSheet();
  17. $cell1 = $sheet->getCell('A1');
  18. $cell1->setValue('Cell A1');
  19. $cell1style = $cell1->getStyle();
  20. $cell1style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
  21. $cell1style->getFont()->getColor()->setARGB('FFFF0000');
  22. $cell1style->getFont()->setBold(true);
  23. $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
  24. $cell1style->getFill()->setStartColor(new Color('FF0000FF'));
  25. $cell1style->getFill()->setEndColor(new Color('FF00FF00'));
  26. $cell1style->getFont()->setUnderline(true);
  27. self::assertEquals(Font::UNDERLINE_SINGLE, $cell1style->getFont()->getUnderline());
  28. $cell1style->getProtection()->setHidden(Protection::PROTECTION_UNPROTECTED);
  29. $cell1style->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED);
  30. $styleArray = $cell1style->exportArray();
  31. $cell2 = $sheet->getCell('B1');
  32. $cell2->setValue('Cell B1');
  33. $cell2style = $cell2->getStyle();
  34. $cell2style->applyFromArray($styleArray);
  35. self::AssertEquals($cell1style->getAlignment()->getHorizontal(), $cell2style->getAlignment()->getHorizontal());
  36. self::AssertEquals($cell1style->getFont()->getColor()->getARGB(), $cell2style->getFont()->getColor()->getARGB());
  37. self::AssertEquals($cell1style->getFont()->getBold(), $cell2style->getFont()->getBold());
  38. self::AssertEquals($cell1style->getFont()->getUnderline(), $cell2style->getFont()->getUnderline());
  39. self::AssertEquals($cell1style->getFill()->getFillType(), $cell2style->getFill()->getFillType());
  40. self::AssertEquals($cell1style->getFill()->getStartColor()->getARGB(), $cell2style->getFill()->getStartColor()->getARGB());
  41. self::AssertEquals($cell1style->getFill()->getEndColor()->getARGB(), $cell2style->getFill()->getEndColor()->getARGB());
  42. self::AssertEquals($cell1style->getProtection()->getLocked(), $cell2style->getProtection()->getLocked());
  43. self::AssertEquals($cell1style->getProtection()->getHidden(), $cell2style->getProtection()->getHidden());
  44. self::AssertEquals($cell1style->getHashCode(), $cell2style->getHashCode());
  45. self::AssertEquals($cell1style->getAlignment()->getHashCode(), $cell2style->getAlignment()->getHashCode());
  46. self::AssertEquals($cell1style->getFont()->getHashCode(), $cell2style->getFont()->getHashCode());
  47. self::AssertEquals($cell1style->getFill()->getHashCode(), $cell2style->getFill()->getHashCode());
  48. self::AssertEquals($cell1style->getProtection()->getHashCode(), $cell2style->getProtection()->getHashCode());
  49. $spreadsheet->disconnectWorksheets();
  50. }
  51. public function testStyleFromArrayCopy(): void
  52. {
  53. $spreadsheet = new Spreadsheet();
  54. $sheet = $spreadsheet->getActiveSheet();
  55. $cell1 = $sheet->getCell('A1');
  56. $cell1->setValue('Cell A1');
  57. $cell1style = $cell1->getStyle();
  58. $cell1style->getAlignment()->applyFromArray(['horizontal' => Alignment::HORIZONTAL_RIGHT]);
  59. $cell1style->getFont()->getColor()->setARGB('FFFF0000');
  60. $cell1style->getFont()->applyFromArray(['bold' => true]);
  61. $cell1style->getFill()->applyFromArray(['fillType' => Fill::FILL_PATTERN_GRAY125]);
  62. $cell1style->getFill()->getStartColor()->applyFromArray(['argb' => 'FF0000FF']);
  63. $cell1style->getFill()->getEndColor()->setRGB('00FF00');
  64. $cell1style->getFill()->setRotation(45);
  65. $cell1style->getFont()->setUnderline(true);
  66. self::assertEquals(Font::UNDERLINE_SINGLE, $cell1style->getFont()->getUnderline());
  67. $cell1style->getProtection()->applyFromArray(['hidden' => Protection::PROTECTION_UNPROTECTED, 'locked' => Protection::PROTECTION_UNPROTECTED]);
  68. $styleArray = $cell1style->exportArray();
  69. $cell2 = $sheet->getCell('B1');
  70. $cell2->setValue('Cell B1');
  71. $cell2style = $cell2->getStyle();
  72. $cell2style->applyFromArray($styleArray);
  73. self::AssertEquals($cell1style->getAlignment()->getHorizontal(), $cell2style->getAlignment()->getHorizontal());
  74. self::AssertEquals($cell1style->getFont()->getColor()->getARGB(), $cell2style->getFont()->getColor()->getARGB());
  75. self::AssertEquals($cell1style->getFont()->getBold(), $cell2style->getFont()->getBold());
  76. self::AssertEquals($cell1style->getFont()->getUnderline(), $cell2style->getFont()->getUnderline());
  77. self::AssertEquals($cell1style->getFill()->getFillType(), $cell2style->getFill()->getFillType());
  78. self::AssertEquals($cell1style->getFill()->getRotation(), $cell2style->getFill()->getRotation());
  79. self::AssertEquals($cell1style->getFill()->getStartColor()->getARGB(), $cell2style->getFill()->getStartColor()->getARGB());
  80. self::AssertEquals($cell1style->getFill()->getEndColor()->getARGB(), $cell2style->getFill()->getEndColor()->getARGB());
  81. self::AssertEquals($cell1style->getProtection()->getLocked(), $cell2style->getProtection()->getLocked());
  82. self::AssertEquals($cell1style->getProtection()->getHidden(), $cell2style->getProtection()->getHidden());
  83. self::AssertEquals($cell1style->getFill()->getStartColor()->getHashCode(), $cell2style->getFill()->getStartColor()->getHashCode());
  84. self::AssertEquals($cell1style->getFill()->getEndColor()->getHashCode(), $cell2style->getFill()->getEndColor()->getHashCode());
  85. $spreadsheet->disconnectWorksheets();
  86. }
  87. public function testNumberFormat(): void
  88. {
  89. $spreadsheet = new Spreadsheet();
  90. $sheet = $spreadsheet->getActiveSheet();
  91. $cell1 = $sheet->getCell('A1');
  92. $cell1style = $cell1->getStyle();
  93. $fmt2 = '$ #,##0.000';
  94. $cell1style->getNumberFormat()->setFormatCode($fmt2);
  95. $cell1style->getFont()->setUnderline('');
  96. self::assertEquals(Font::UNDERLINE_NONE, $cell1style->getFont()->getUnderline());
  97. $cell1->setValue(2345.679);
  98. $styleArray = $cell1style->exportArray();
  99. self::assertEquals('$ 2,345.679', $cell1->getFormattedValue());
  100. $cell2 = $sheet->getCell('B1');
  101. $cell2->setValue(12345.679);
  102. $cell2style = $cell2->getStyle();
  103. $cell2style->applyFromArray($styleArray);
  104. self::assertEquals('$ 12,345.679', $cell2->getFormattedValue());
  105. self::AssertEquals($cell1style->getNumberFormat()->getHashCode(), $cell2style->getNumberFormat()->getHashCode());
  106. $spreadsheet->disconnectWorksheets();
  107. }
  108. public function testNumberFormatFromArray(): void
  109. {
  110. $spreadsheet = new Spreadsheet();
  111. $sheet = $spreadsheet->getActiveSheet();
  112. $cell1 = $sheet->getCell('A1');
  113. $cell1style = $cell1->getStyle();
  114. $fmt2 = '$ #,##0.000';
  115. $cell1style->getNumberFormat()->applyFromArray(['formatCode' => $fmt2]);
  116. $cell1style->getFont()->setUnderline('');
  117. self::assertEquals(Font::UNDERLINE_NONE, $cell1style->getFont()->getUnderline());
  118. $cell1style->getBorders()->getTop()->setBorderStyle(Border::BORDER_THIN);
  119. $cell1->setValue(2345.679);
  120. $styleArray = $cell1style->exportArray();
  121. self::assertEquals('$ 2,345.679', $cell1->getFormattedValue());
  122. $cell2 = $sheet->getCell('B1');
  123. $cell2->setValue(12345.679);
  124. $cell2style = $cell2->getStyle();
  125. $cell2style->applyFromArray($styleArray);
  126. self::assertEquals('$ 12,345.679', $cell2->getFormattedValue());
  127. self::AssertEquals($cell1style->getNumberFormat()->getHashCode(), $cell2style->getNumberFormat()->getHashCode());
  128. self::AssertEquals($cell1style->getBorders()->getHashCode(), $cell2style->getBorders()->getHashCode());
  129. self::AssertEquals($cell1style->getBorders()->getTop()->getHashCode(), $cell2style->getBorders()->getTop()->getHashCode());
  130. self::AssertEquals($cell1style->getBorders()->getTop()->getBorderStyle(), $cell2style->getBorders()->getTop()->getBorderStyle());
  131. $spreadsheet->disconnectWorksheets();
  132. }
  133. public function testStackedRotation(): void
  134. {
  135. $spreadsheet = new Spreadsheet();
  136. $sheet = $spreadsheet->getActiveSheet();
  137. $cell1 = $sheet->getCell('A1');
  138. $cell1->setValue('Cell A1');
  139. $cell1style = $cell1->getStyle();
  140. $cell1style->getAlignment()->setTextRotation(Alignment::TEXTROTATION_STACK_EXCEL);
  141. self::assertEquals(Alignment::TEXTROTATION_STACK_PHPSPREADSHEET, $cell1style->getAlignment()->getTextRotation());
  142. $styleArray = $cell1style->exportArray();
  143. $cell2 = $sheet->getCell('B1');
  144. $cell2->setValue('Cell B1');
  145. $cell2style = $cell2->getStyle();
  146. $cell2style->applyFromArray($styleArray);
  147. self::AssertEquals($cell1style->getAlignment()->getTextRotation(), $cell2style->getAlignment()->getTextRotation());
  148. $spreadsheet->disconnectWorksheets();
  149. }
  150. public function testFillColors(): void
  151. {
  152. $spreadsheet = new Spreadsheet();
  153. $sheet = $spreadsheet->getActiveSheet();
  154. $cell1 = $sheet->getCell('A2');
  155. $cell1style = $cell1->getStyle();
  156. $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
  157. $cell1style->getFill()->getStartColor()->setArgb('FF112233');
  158. $styleArray = $cell1style->exportArray();
  159. self::assertEquals(
  160. [
  161. 'fillType' => Fill::FILL_PATTERN_GRAY125,
  162. 'rotation' => 0.0,
  163. 'endColor' => ['argb' => 'FF000000'],
  164. 'startColor' => ['argb' => 'FF112233'],
  165. ],
  166. $styleArray['fill'],
  167. 'changed start color with setArgb'
  168. );
  169. $cell1 = $sheet->getCell('A1');
  170. $cell1style = $cell1->getStyle();
  171. $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
  172. $styleArray = $cell1style->exportArray();
  173. self::assertEquals(
  174. [
  175. 'fillType' => Fill::FILL_PATTERN_GRAY125,
  176. 'rotation' => 0.0,
  177. ],
  178. $styleArray['fill'],
  179. 'default colors'
  180. );
  181. $cell1 = $sheet->getCell('A3');
  182. $cell1style = $cell1->getStyle();
  183. $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
  184. $cell1style->getFill()->getEndColor()->setArgb('FF112233');
  185. $styleArray = $cell1style->exportArray();
  186. self::assertEquals(
  187. [
  188. 'fillType' => Fill::FILL_PATTERN_GRAY125,
  189. 'rotation' => 0.0,
  190. 'endColor' => ['argb' => 'FF112233'],
  191. 'startColor' => ['argb' => 'FFFFFFFF'],
  192. ],
  193. $styleArray['fill'],
  194. 'changed end color with setArgb'
  195. );
  196. $cell1 = $sheet->getCell('A4');
  197. $cell1style = $cell1->getStyle();
  198. $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
  199. $cell1style->getFill()->setEndColor(new Color('FF0000FF'));
  200. $styleArray = $cell1style->exportArray();
  201. self::assertEquals(
  202. [
  203. 'fillType' => Fill::FILL_PATTERN_GRAY125,
  204. 'rotation' => 0.0,
  205. 'endColor' => ['argb' => 'FF0000FF'],
  206. 'startColor' => ['argb' => 'FFFFFFFF'],
  207. ],
  208. $styleArray['fill'],
  209. 'changed end color with setEndColor'
  210. );
  211. $cell1 = $sheet->getCell('A5');
  212. $cell1style = $cell1->getStyle();
  213. $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
  214. $cell1style->getFill()->setStartColor(new Color('FF0000FF'));
  215. $styleArray = $cell1style->exportArray();
  216. self::assertEquals(
  217. [
  218. 'fillType' => Fill::FILL_PATTERN_GRAY125,
  219. 'rotation' => 0.0,
  220. 'startColor' => ['argb' => 'FF0000FF'],
  221. 'endColor' => ['argb' => 'FF000000'],
  222. ],
  223. $styleArray['fill'],
  224. 'changed start color with setStartColor'
  225. );
  226. $cell1 = $sheet->getCell('A6');
  227. $cell1->getStyle()->getFill()->applyFromArray(
  228. [
  229. 'fillType' => Fill::FILL_PATTERN_GRAY125,
  230. 'rotation' => 45.0,
  231. 'startColor' => ['argb' => 'FF00FFFF'],
  232. ]
  233. );
  234. $cell1style = $cell1->getStyle();
  235. $styleArray = $cell1style->exportArray();
  236. self::assertEquals(
  237. [
  238. 'fillType' => Fill::FILL_PATTERN_GRAY125,
  239. 'rotation' => 45.0,
  240. 'startColor' => ['argb' => 'FF00FFFF'],
  241. 'endColor' => ['argb' => 'FF000000'],
  242. ],
  243. $styleArray['fill'],
  244. 'applyFromArray with startColor'
  245. );
  246. $cell1 = $sheet->getCell('A7');
  247. $cell1->getStyle()->getFill()->applyFromArray(
  248. [
  249. 'fillType' => Fill::FILL_PATTERN_GRAY125,
  250. ]
  251. );
  252. $cell1style = $cell1->getStyle();
  253. $styleArray = $cell1style->exportArray();
  254. self::assertEquals(
  255. [
  256. 'fillType' => Fill::FILL_PATTERN_GRAY125,
  257. 'rotation' => 0.0,
  258. ],
  259. $styleArray['fill'],
  260. 'applyFromArray without start/endColor'
  261. );
  262. $spreadsheet->disconnectWorksheets();
  263. }
  264. }