Skip to content

Commit

Permalink
upd test
Browse files Browse the repository at this point in the history
  • Loading branch information
aVadim483 committed Jan 2, 2025
1 parent d4b1e1b commit e050739
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 33 deletions.
48 changes: 29 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ $excel->save('simple.xlsx');
```



### Adding Notes

There are currently two types of comments in Excel - **comments** and **notes**
Expand All @@ -280,17 +279,17 @@ You can add notes to any cells using method ```addNote()```

```php

$sheet1->writeCell('Text to A1');
$sheet1->addNote('A1', 'This is a note for cell A1');
$sheet->writeCell('Text to A1');
$sheet->addNote('A1', 'This is a note for cell A1');

$sheet1->writeCell('Text to B1')->addNote('This is a note for B1');
$sheet1->writeTo('C4', 'Text to C4')->addNote('Note for C1');
$sheet->writeCell('Text to B1')->addNote('This is a note for B1');
$sheet->writeTo('C4', 'Text to C4')->addNote('Note for C1');

// If you specify a range of cells, then the note will be added to the left top cell
$sheet1->addNote('E4:F8', "This note\nwill added to E4");
$sheet->addNote('E4:F8', "This note\nwill added to E4");

// You can split text into multiple lines
$sheet1->addNote('D7', "Line 1\nLine 2");
$sheet->addNote('D7', "Line 1\nLine 2");

```

Expand All @@ -302,7 +301,7 @@ You can change some note options. Allowed options of a note are:

```php

$sheet1->addNote('A1', 'This is a note for cell A1', ['width' => '200pt', 'height' => '100pt', 'fill_color' => '#ffcccc']);
$sheet->addNote('A1', 'This is a note for cell A1', ['width' => '200pt', 'height' => '100pt', 'fill_color' => '#ffcccc']);

// Parameters "width" and "height" can be numeric, by default these values are in points
// The "fill_color" parameter can be shortened
Expand All @@ -311,17 +310,17 @@ $noteStyle = [
'height' => 100, // equivalent to '100pt'
'fill_color' => 'fcc', // equivalent to '#ffcccc'
];
$sheet1->writeCell('Text to B1')->addNote('This is a note for B1', $noteStyle);
$sheet->writeCell('Text to B1')->addNote('This is a note for B1', $noteStyle);

// This note is visible when the Excel workbook is displayed
$sheet1->addNote('C8', 'This note is always visible', ['show' => true]);
$sheet->addNote('C8', 'This note is always visible', ['show' => true]);
```

Also, you can use rich text in notes

```php
$richText = new \avadim\FastExcelWriter\RichText('here is <c=f00>red</c> and <c=00f>blue</c> text');
$sheet1->addNote('C8', $richText);
$sheet->addNote('C8', $richText);
```

For more information on using rich text, see here: [Using Rich Text](/docs/03-writing.md#using-rich-text)
Expand All @@ -331,29 +330,40 @@ For more information on using rich text, see here: [Using Rich Text](/docs/03-wr
You can insert image to sheet from local file, URL or image string in base64

```php
$sheet->addImage($cell, $imageFile, $imageStyle);

// Insert an image to the cell A1 from local path
$sheet1->addImage('A1', 'path/to/file');
$sheet->addImage('A1', 'path/to/file');

// Insert an image to the cell A1 from URL
$sheet1->addImage('A1', 'https://site.com/image.jpg');
$sheet->addImage('A1', 'https://site.com/image.jpg');

// Insert an image to the cell A1 from base64 string
$sheet1->addImage('A1', 'data:image/jpeg;base64,/9j/4AAQ...');
$sheet->addImage('A1', 'data:image/jpeg;base64,/9j/4AAQ...');

// Insert an image to the cell B2 and set with to 150 pixels (height will change proportionally)
$sheet1->addImage('B2', 'path/to/file', ['width' => 150]);
$sheet->addImage('B2', 'path/to/file', ['width' => 150]);

// Set height to 150 pixels (with will change proportionally)
$sheet1->addImage('C3', 'path/to/file', ['height' => 150]);
$sheet->addImage('C3', 'path/to/file', ['height' => 150]);

// Set size in pixels
$sheet1->addImage('D4', 'path/to/file', ['width' => 150, 'height' => 150]);
$sheet->addImage('D4', 'path/to/file', ['width' => 150, 'height' => 150]);

// Add hyperlink to the image
$sheet1->addImage('D4', 'path/to/file', ['width' => 150, 'height' => 150, 'hyperlink' => 'https://www.google.com/']);

$sheet->addImage('D4', 'path/to/file', ['width' => 150, 'height' => 150, 'hyperlink' => 'https://www.google.com/']);
```

Available keys of image style:
* 'width' -- width of image
* 'height' -- height of image
* 'hyperlink' -- URL of hyperlink
* 'x' -- offset in pixels relative to the left border of the cell
* 'y' -- offset in pixels relative to the top border of the cell

**IMPORTANT:** in MS Excel, value 'x' cannot be greater than the column width of the parent cell,
and value 'y' cannot be greater than the row height

## Shared Strings

By default, strings are written directly to sheets. This increases the file size a little,
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"require-dev": {
"phpunit/phpunit": "^9.0",
"ext-fileinfo": "*",
"avadim/fast-excel-reader": "^2.19"
"avadim/fast-excel-reader": "^2.22"
},
"scripts": {
"test": "vendor/bin/phpunit tests"
Expand Down
10 changes: 6 additions & 4 deletions src/FastExcelWriter/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2744,19 +2744,19 @@ protected function _setCellData($cellAddress, $value, $styles = null, ?bool $mer
}

if ($value !== null) {
if (is_callable($value)) {
$value = $value($this);
}
if (is_scalar($value)
|| ($value instanceof RichText)
// it's a formula & value ['=A1+B2', 123]
|| (is_array($value) && !empty($value[0]) && is_string($value[0]) && ($value[0][0] === '=') && count($value) === 2)
) {
$this->cells['values'][$rowIdx][$colIdx] = $value;
}
elseif (is_callable($value)) {
$this->cells['values'][$rowIdx][$colIdx] = $value($this);
}
else {
$addr = Excel::cellAddress($colIdx + 1, $rowIdx + 1);
Exception::throwNew('Value for cell %s must be scalar', $addr);
Exception::throwNew('Value for cell %s must be scalar or callable', $addr);
}
if ($changeCurrent) {
$this->currentRowIdx = $rowIdx;
Expand Down Expand Up @@ -3516,6 +3516,8 @@ public function addImage(string $cell, string $imageFile, ?array $imageStyle = [
$imageData['cell'] = $cell;
$imageData['row_index'] = $rowIdx;
$imageData['col_index'] = $colIdx;
$imageData['x'] = $imageStyle['x'] ?? 0;
$imageData['y'] = $imageStyle['y'] ?? 0;
if (!empty($imageStyle['width']) || !empty($imageStyle['height'])) {
if (!empty($imageStyle['width']) && empty($imageStyle['height'])) {
$ratio = $imageStyle['width'] / $imageData['width'];
Expand Down
15 changes: 12 additions & 3 deletions src/FastExcelWriter/Writer/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -898,16 +898,18 @@ protected function _writeDrawingFile(string $entry, array $imageList, array $cha
$objectId = $image['id'];
$rId = $image['r_id'];
$baseName = $image['original'];
$x = Excel::pixelsToEMU($image['x']);
$y = Excel::pixelsToEMU($image['y']);
$width = Excel::pixelsToEMU($image['width']);
$height = Excel::pixelsToEMU($image['height']);

$fileWriter->startElement('xdr:oneCellAnchor');

$fileWriter->startElement('xdr:from');
$fileWriter->writeElement("<xdr:col>{$image['col_index']}</xdr:col>");
$fileWriter->writeElement('<xdr:colOff>0</xdr:colOff>');
$fileWriter->writeElement("<xdr:colOff>{$x}</xdr:colOff>");
$fileWriter->writeElement("<xdr:row>{$image['row_index']}</xdr:row>");
$fileWriter->writeElement('<xdr:rowOff>0</xdr:rowOff>');
$fileWriter->writeElement("<xdr:rowOff>{$y}</xdr:rowOff>");
$fileWriter->endElement();

$fileWriter->writeElement("<xdr:ext cx=\"{$width}\" cy=\"{$height}\"/>");
Expand Down Expand Up @@ -940,7 +942,14 @@ protected function _writeDrawingFile(string $entry, array $imageList, array $cha

$fileWriter->startElement('xdr:spPr');
$fileWriter->writeElement("<a:xfrm rot=\"0\"/><a:prstGeom prst=\"rect\"><a:avLst/></a:prstGeom>");
$fileWriter->endElement();
/* !!
$fileWriter->startElement('a:xfrm');
$fileWriter->writeElement("<a:off x=\"{$x}\" y=\"{$y}\"/>");
$fileWriter->writeElement("<a:ext cx=\"{$width}\" cy=\"{$height}\"/>");
$fileWriter->endElement(); // </a:xfrm>
$fileWriter->writeElement("<a:prstGeom prst=\"rect\"><a:avLst/></a:prstGeom>");
*/
$fileWriter->endElement(); // </xdr:spPr>

$fileWriter->endElement();

Expand Down
28 changes: 22 additions & 6 deletions tests/FastExcelWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected function getValue($cell)
return $this->cells[$m[2]][$m[1]]['v'] ?? null;
}


protected function getValues($cells): array
{
$result = [];
Expand All @@ -33,6 +34,7 @@ protected function getValues($cells): array
return $result;
}


protected function getStyle($cell, $flat = false): array
{
preg_match('/^(\w+)(\d+)$/', strtoupper($cell), $m);
Expand All @@ -55,6 +57,7 @@ protected function getStyle($cell, $flat = false): array
return [];
}


protected function defaultStyle(): array
{
return [
Expand Down Expand Up @@ -127,10 +130,12 @@ public function testExcelWriter0()
->writeCell('A1')
->writeCell('B1')
->nextCell() // C1
->writeCell('D1')
->writeCell(fn($sheet) => $sheet->getCurrentCell()) // D1
->nextCell() // E1
->nextCell() // F1
->writeCell('G1')
->writeCell(function($sheet) {
return $sheet->getCurrentCol() . $sheet->getCurrentRow();
}) // G1
->writeTo('F1', 'F1');
;
// write row 2 go to row 3
Expand Down Expand Up @@ -283,7 +288,8 @@ public function testExcelWriter1()
$this->cells = [];
}

protected function makeTestFile2($testFileName)

protected function makeTestFile2($testFileName): ExcelReader
{
// PREPARE DEMO DATA
$lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua';
Expand Down Expand Up @@ -398,6 +404,7 @@ protected function makeTestFile2($testFileName)
return $this->saveCheckRead($excel, $testFileName);
}


public function testExcelWriter2()
{
$testFileName = __DIR__ . '/test2.xlsx';
Expand Down Expand Up @@ -541,6 +548,7 @@ public function testExcelWriter3()
unlink($testFileName);
}


public function testExcelWriter4()
{
$testFileName = __DIR__ . '/test4.xlsx';
Expand Down Expand Up @@ -589,6 +597,7 @@ public function testExcelWriter4()
unlink($testFileName);
}


public function testExcelWriter5()
{
$testFileName = __DIR__ . '/test5.xlsx';
Expand All @@ -606,8 +615,8 @@ public function testExcelWriter5()
$sheet->writeRow([1, 11, 111])->applyFontStyleBold();
$sheet->writeRow([2, 22, 222]);
$sheet->writeCell(3);
$sheet->writeCell(33);
$sheet->writeCell(333);
$sheet->writeCell('33');
$sheet->writeCell(333.3);

$sheet = $excel->sheet('Demo2');
$sheet->writeHeader(['AAA', 'BBB', 'CCC'])->applyFontStyleBold();
Expand All @@ -618,14 +627,18 @@ public function testExcelWriter5()

$this->assertEquals([1, 11, 111], $this->getValues(['c3', 'd3', 'e3']));
$this->assertEquals([2, 22, 222], $this->getValues(['c4', 'd4', 'e4']));
$this->assertEquals([3, 33, 333], $this->getValues(['c5', 'd5', 'e5']));

$this->assertTrue(3 === $this->getValue('c5'));
$this->assertTrue('33' === $this->getValue('d5'));
$this->assertTrue(333.3 === $this->getValue('e5'));

$cells = $this->excelReader->sheet('Demo2')->readCellsWithStyles();
$this->assertEquals('Century', $cells['A1']['s']['font']['font-name']);

unlink($testFileName);
}


public function testExcelWriterMergedCells()
{
$testFileName = __DIR__ . '/test_merged.xlsx';
Expand Down Expand Up @@ -669,6 +682,7 @@ public function testExcelWriterMergedCells()
unlink($testFileName);
}


public function testExcelWriterNotesAndImages()
{
$testFileName = __DIR__ . '/test_notes_images.xlsx';
Expand Down Expand Up @@ -730,6 +744,7 @@ public function testExcelWriterNotesAndImages()
unlink($testFileName);
}


public function testExcelWriterSingleValue()
{
$testFileName = __DIR__ . '/test_single.xlsx';
Expand Down Expand Up @@ -770,6 +785,7 @@ public function testExcelWriterSingleValue()
unlink($testFileName);
}


public function testCharts()
{
$testFileName = __DIR__ . '/test_charts.xlsx';
Expand Down

0 comments on commit e050739

Please sign in to comment.