| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?php
- namespace PhpOffice\PhpSpreadsheetTests\Style;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Style\Alignment;
- use PhpOffice\PhpSpreadsheet\Style\Border;
- use PhpOffice\PhpSpreadsheet\Style\Color;
- use PhpOffice\PhpSpreadsheet\Style\Fill;
- use PhpOffice\PhpSpreadsheet\Style\Font;
- use PhpOffice\PhpSpreadsheet\Style\Protection;
- use PHPUnit\Framework\TestCase;
- class ExportArrayTest extends TestCase
- {
- public function testStyleCopy(): void
- {
- $spreadsheet = new Spreadsheet();
- $sheet = $spreadsheet->getActiveSheet();
- $cell1 = $sheet->getCell('A1');
- $cell1->setValue('Cell A1');
- $cell1style = $cell1->getStyle();
- $cell1style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
- $cell1style->getFont()->getColor()->setARGB('FFFF0000');
- $cell1style->getFont()->setBold(true);
- $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
- $cell1style->getFill()->setStartColor(new Color('FF0000FF'));
- $cell1style->getFill()->setEndColor(new Color('FF00FF00'));
- $cell1style->getFont()->setUnderline(true);
- self::assertEquals(Font::UNDERLINE_SINGLE, $cell1style->getFont()->getUnderline());
- $cell1style->getProtection()->setHidden(Protection::PROTECTION_UNPROTECTED);
- $cell1style->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED);
- $styleArray = $cell1style->exportArray();
- $cell2 = $sheet->getCell('B1');
- $cell2->setValue('Cell B1');
- $cell2style = $cell2->getStyle();
- $cell2style->applyFromArray($styleArray);
- self::AssertEquals($cell1style->getAlignment()->getHorizontal(), $cell2style->getAlignment()->getHorizontal());
- self::AssertEquals($cell1style->getFont()->getColor()->getARGB(), $cell2style->getFont()->getColor()->getARGB());
- self::AssertEquals($cell1style->getFont()->getBold(), $cell2style->getFont()->getBold());
- self::AssertEquals($cell1style->getFont()->getUnderline(), $cell2style->getFont()->getUnderline());
- self::AssertEquals($cell1style->getFill()->getFillType(), $cell2style->getFill()->getFillType());
- self::AssertEquals($cell1style->getFill()->getStartColor()->getARGB(), $cell2style->getFill()->getStartColor()->getARGB());
- self::AssertEquals($cell1style->getFill()->getEndColor()->getARGB(), $cell2style->getFill()->getEndColor()->getARGB());
- self::AssertEquals($cell1style->getProtection()->getLocked(), $cell2style->getProtection()->getLocked());
- self::AssertEquals($cell1style->getProtection()->getHidden(), $cell2style->getProtection()->getHidden());
- self::AssertEquals($cell1style->getHashCode(), $cell2style->getHashCode());
- self::AssertEquals($cell1style->getAlignment()->getHashCode(), $cell2style->getAlignment()->getHashCode());
- self::AssertEquals($cell1style->getFont()->getHashCode(), $cell2style->getFont()->getHashCode());
- self::AssertEquals($cell1style->getFill()->getHashCode(), $cell2style->getFill()->getHashCode());
- self::AssertEquals($cell1style->getProtection()->getHashCode(), $cell2style->getProtection()->getHashCode());
- $spreadsheet->disconnectWorksheets();
- }
- public function testStyleFromArrayCopy(): void
- {
- $spreadsheet = new Spreadsheet();
- $sheet = $spreadsheet->getActiveSheet();
- $cell1 = $sheet->getCell('A1');
- $cell1->setValue('Cell A1');
- $cell1style = $cell1->getStyle();
- $cell1style->getAlignment()->applyFromArray(['horizontal' => Alignment::HORIZONTAL_RIGHT]);
- $cell1style->getFont()->getColor()->setARGB('FFFF0000');
- $cell1style->getFont()->applyFromArray(['bold' => true]);
- $cell1style->getFill()->applyFromArray(['fillType' => Fill::FILL_PATTERN_GRAY125]);
- $cell1style->getFill()->getStartColor()->applyFromArray(['argb' => 'FF0000FF']);
- $cell1style->getFill()->getEndColor()->setRGB('00FF00');
- $cell1style->getFill()->setRotation(45);
- $cell1style->getFont()->setUnderline(true);
- self::assertEquals(Font::UNDERLINE_SINGLE, $cell1style->getFont()->getUnderline());
- $cell1style->getProtection()->applyFromArray(['hidden' => Protection::PROTECTION_UNPROTECTED, 'locked' => Protection::PROTECTION_UNPROTECTED]);
- $styleArray = $cell1style->exportArray();
- $cell2 = $sheet->getCell('B1');
- $cell2->setValue('Cell B1');
- $cell2style = $cell2->getStyle();
- $cell2style->applyFromArray($styleArray);
- self::AssertEquals($cell1style->getAlignment()->getHorizontal(), $cell2style->getAlignment()->getHorizontal());
- self::AssertEquals($cell1style->getFont()->getColor()->getARGB(), $cell2style->getFont()->getColor()->getARGB());
- self::AssertEquals($cell1style->getFont()->getBold(), $cell2style->getFont()->getBold());
- self::AssertEquals($cell1style->getFont()->getUnderline(), $cell2style->getFont()->getUnderline());
- self::AssertEquals($cell1style->getFill()->getFillType(), $cell2style->getFill()->getFillType());
- self::AssertEquals($cell1style->getFill()->getRotation(), $cell2style->getFill()->getRotation());
- self::AssertEquals($cell1style->getFill()->getStartColor()->getARGB(), $cell2style->getFill()->getStartColor()->getARGB());
- self::AssertEquals($cell1style->getFill()->getEndColor()->getARGB(), $cell2style->getFill()->getEndColor()->getARGB());
- self::AssertEquals($cell1style->getProtection()->getLocked(), $cell2style->getProtection()->getLocked());
- self::AssertEquals($cell1style->getProtection()->getHidden(), $cell2style->getProtection()->getHidden());
- self::AssertEquals($cell1style->getFill()->getStartColor()->getHashCode(), $cell2style->getFill()->getStartColor()->getHashCode());
- self::AssertEquals($cell1style->getFill()->getEndColor()->getHashCode(), $cell2style->getFill()->getEndColor()->getHashCode());
- $spreadsheet->disconnectWorksheets();
- }
- public function testNumberFormat(): void
- {
- $spreadsheet = new Spreadsheet();
- $sheet = $spreadsheet->getActiveSheet();
- $cell1 = $sheet->getCell('A1');
- $cell1style = $cell1->getStyle();
- $fmt2 = '$ #,##0.000';
- $cell1style->getNumberFormat()->setFormatCode($fmt2);
- $cell1style->getFont()->setUnderline('');
- self::assertEquals(Font::UNDERLINE_NONE, $cell1style->getFont()->getUnderline());
- $cell1->setValue(2345.679);
- $styleArray = $cell1style->exportArray();
- self::assertEquals('$ 2,345.679', $cell1->getFormattedValue());
- $cell2 = $sheet->getCell('B1');
- $cell2->setValue(12345.679);
- $cell2style = $cell2->getStyle();
- $cell2style->applyFromArray($styleArray);
- self::assertEquals('$ 12,345.679', $cell2->getFormattedValue());
- self::AssertEquals($cell1style->getNumberFormat()->getHashCode(), $cell2style->getNumberFormat()->getHashCode());
- $spreadsheet->disconnectWorksheets();
- }
- public function testNumberFormatFromArray(): void
- {
- $spreadsheet = new Spreadsheet();
- $sheet = $spreadsheet->getActiveSheet();
- $cell1 = $sheet->getCell('A1');
- $cell1style = $cell1->getStyle();
- $fmt2 = '$ #,##0.000';
- $cell1style->getNumberFormat()->applyFromArray(['formatCode' => $fmt2]);
- $cell1style->getFont()->setUnderline('');
- self::assertEquals(Font::UNDERLINE_NONE, $cell1style->getFont()->getUnderline());
- $cell1style->getBorders()->getTop()->setBorderStyle(Border::BORDER_THIN);
- $cell1->setValue(2345.679);
- $styleArray = $cell1style->exportArray();
- self::assertEquals('$ 2,345.679', $cell1->getFormattedValue());
- $cell2 = $sheet->getCell('B1');
- $cell2->setValue(12345.679);
- $cell2style = $cell2->getStyle();
- $cell2style->applyFromArray($styleArray);
- self::assertEquals('$ 12,345.679', $cell2->getFormattedValue());
- self::AssertEquals($cell1style->getNumberFormat()->getHashCode(), $cell2style->getNumberFormat()->getHashCode());
- self::AssertEquals($cell1style->getBorders()->getHashCode(), $cell2style->getBorders()->getHashCode());
- self::AssertEquals($cell1style->getBorders()->getTop()->getHashCode(), $cell2style->getBorders()->getTop()->getHashCode());
- self::AssertEquals($cell1style->getBorders()->getTop()->getBorderStyle(), $cell2style->getBorders()->getTop()->getBorderStyle());
- $spreadsheet->disconnectWorksheets();
- }
- public function testStackedRotation(): void
- {
- $spreadsheet = new Spreadsheet();
- $sheet = $spreadsheet->getActiveSheet();
- $cell1 = $sheet->getCell('A1');
- $cell1->setValue('Cell A1');
- $cell1style = $cell1->getStyle();
- $cell1style->getAlignment()->setTextRotation(Alignment::TEXTROTATION_STACK_EXCEL);
- self::assertEquals(Alignment::TEXTROTATION_STACK_PHPSPREADSHEET, $cell1style->getAlignment()->getTextRotation());
- $styleArray = $cell1style->exportArray();
- $cell2 = $sheet->getCell('B1');
- $cell2->setValue('Cell B1');
- $cell2style = $cell2->getStyle();
- $cell2style->applyFromArray($styleArray);
- self::AssertEquals($cell1style->getAlignment()->getTextRotation(), $cell2style->getAlignment()->getTextRotation());
- $spreadsheet->disconnectWorksheets();
- }
- public function testFillColors(): void
- {
- $spreadsheet = new Spreadsheet();
- $sheet = $spreadsheet->getActiveSheet();
- $cell1 = $sheet->getCell('A2');
- $cell1style = $cell1->getStyle();
- $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
- $cell1style->getFill()->getStartColor()->setArgb('FF112233');
- $styleArray = $cell1style->exportArray();
- self::assertEquals(
- [
- 'fillType' => Fill::FILL_PATTERN_GRAY125,
- 'rotation' => 0.0,
- 'endColor' => ['argb' => 'FF000000'],
- 'startColor' => ['argb' => 'FF112233'],
- ],
- $styleArray['fill'],
- 'changed start color with setArgb'
- );
- $cell1 = $sheet->getCell('A1');
- $cell1style = $cell1->getStyle();
- $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
- $styleArray = $cell1style->exportArray();
- self::assertEquals(
- [
- 'fillType' => Fill::FILL_PATTERN_GRAY125,
- 'rotation' => 0.0,
- ],
- $styleArray['fill'],
- 'default colors'
- );
- $cell1 = $sheet->getCell('A3');
- $cell1style = $cell1->getStyle();
- $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
- $cell1style->getFill()->getEndColor()->setArgb('FF112233');
- $styleArray = $cell1style->exportArray();
- self::assertEquals(
- [
- 'fillType' => Fill::FILL_PATTERN_GRAY125,
- 'rotation' => 0.0,
- 'endColor' => ['argb' => 'FF112233'],
- 'startColor' => ['argb' => 'FFFFFFFF'],
- ],
- $styleArray['fill'],
- 'changed end color with setArgb'
- );
- $cell1 = $sheet->getCell('A4');
- $cell1style = $cell1->getStyle();
- $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
- $cell1style->getFill()->setEndColor(new Color('FF0000FF'));
- $styleArray = $cell1style->exportArray();
- self::assertEquals(
- [
- 'fillType' => Fill::FILL_PATTERN_GRAY125,
- 'rotation' => 0.0,
- 'endColor' => ['argb' => 'FF0000FF'],
- 'startColor' => ['argb' => 'FFFFFFFF'],
- ],
- $styleArray['fill'],
- 'changed end color with setEndColor'
- );
- $cell1 = $sheet->getCell('A5');
- $cell1style = $cell1->getStyle();
- $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
- $cell1style->getFill()->setStartColor(new Color('FF0000FF'));
- $styleArray = $cell1style->exportArray();
- self::assertEquals(
- [
- 'fillType' => Fill::FILL_PATTERN_GRAY125,
- 'rotation' => 0.0,
- 'startColor' => ['argb' => 'FF0000FF'],
- 'endColor' => ['argb' => 'FF000000'],
- ],
- $styleArray['fill'],
- 'changed start color with setStartColor'
- );
- $cell1 = $sheet->getCell('A6');
- $cell1->getStyle()->getFill()->applyFromArray(
- [
- 'fillType' => Fill::FILL_PATTERN_GRAY125,
- 'rotation' => 45.0,
- 'startColor' => ['argb' => 'FF00FFFF'],
- ]
- );
- $cell1style = $cell1->getStyle();
- $styleArray = $cell1style->exportArray();
- self::assertEquals(
- [
- 'fillType' => Fill::FILL_PATTERN_GRAY125,
- 'rotation' => 45.0,
- 'startColor' => ['argb' => 'FF00FFFF'],
- 'endColor' => ['argb' => 'FF000000'],
- ],
- $styleArray['fill'],
- 'applyFromArray with startColor'
- );
- $cell1 = $sheet->getCell('A7');
- $cell1->getStyle()->getFill()->applyFromArray(
- [
- 'fillType' => Fill::FILL_PATTERN_GRAY125,
- ]
- );
- $cell1style = $cell1->getStyle();
- $styleArray = $cell1style->exportArray();
- self::assertEquals(
- [
- 'fillType' => Fill::FILL_PATTERN_GRAY125,
- 'rotation' => 0.0,
- ],
- $styleArray['fill'],
- 'applyFromArray without start/endColor'
- );
- $spreadsheet->disconnectWorksheets();
- }
- }
|