Skip to content

Commit

Permalink
RowTemplate::cloneCellTo(); minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aVadim483 committed Oct 26, 2023
1 parent f0d23b5 commit 845f01f
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 79 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ require 'path/to/fast-excel-templator/src/autoload.php';

Example of template

![img1-tpl.png](img1-tpl.png)
![demo1-tpl.png](demo1-tpl.png)

From this template you can get a file like this

![img1-out.png](img1-out.png)
![demo1-out.png](demo1-out.png)

```php
// Open template and set output file
Expand Down Expand Up @@ -116,3 +116,13 @@ Excel::template($template, $output);
* insertRow($rowNumber, $rowTemplate, ?array $cellData = [])
* replaceRow($rowNumber, $rowTemplate, ?array $cellData = [])
* insertRowAfterLast($rowTemplate, ?array $cellData = [])

## Do you like FastExcelTemplator?

if you find this package useful you can support and donate to me for a cup of coffee:

* USDT (TRC20) TSsUFvJehQBJCKeYgNNR1cpswY6JZnbZK7
* USDT (ERC20) 0x5244519D65035aF868a010C2f68a086F473FC82b
* ETH 0x5244519D65035aF868a010C2f68a086F473FC82b

Or just give me a star on GitHub :)
76 changes: 76 additions & 0 deletions demo/demo1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

use avadim\FastExcelTemplator\Excel;

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../src/autoload.php';

$tpl = __DIR__ . '/files/demo1-tpl.xlsx';
$out = __DIR__ . '/files/demo1-out.xlsx';

$time = microtime(true);

// Open template and set output file
$excel = Excel::template($tpl, $out);
$sheet = $excel->sheet();

$fillData = [
'{{COMPANY}}' => 'Comp Stock Shop',
'{{ADDRESS}}' => '123 ABC Street',
'{{CITY}}' => 'Peace City, TN',
];
$replaceData = ['{{BULK_QTY}}' => 12, '{{DATE}}' => date('m/d/Y')];
$list = [
[
'number' => 'AA-8465-947563',
'description' => 'NEC Mate Type ML PC-MK29MLZD1FWG (Corei5-3470S/2GB/250GB/Multi/No OF/Win 11 Home)',
'price1' => 816,
'price2' => 683,
],
[
'number' => 'QR-7956-048914',
'description' => 'Acer Aspire TC-1760-UA92 (Core i5-12400/12GB 3200MHz DDR4/512GB SSD/Win 11 Home)',
'price1' => 414,
'price2' => 339,
],
[
'number' => 'BD-3966-285936',
'description' => 'Dell Optiplex 7050 SFF (Core i7-7700/32GB DDR4/1TB SSD/Win 10 Pro/Renewed)',
'price1' => 249,
'price2' => 198,
],
];

// Replace strings and substrings on the sheet
$sheet
->fill($fillData)
->replace($replaceData)
;

// Get the specified row (number 6) as a template
$rowTemplate = $sheet->getRowTemplate(6);
$count = 0;
foreach ($list as $record) {
$rowData = [
// In the column A wil be written value from field 'number'
'A' => $record['number'],
// In the column B wil be written value from field 'description'
'B' => $record['description'],
// And so on...
'C' => $record['price1'],
'D' => $record['price2'],
];
if ($count++ === 0) {
// First we replace the row 6 (this is template row)
$sheet->replaceRow(6, $rowTemplate, $rowData);
}
else {
// Then we add new rows after last touched
$sheet->insertRowAfterLast($rowTemplate, $rowData);
}
}

$excel->save();

echo 'Output file: ' . $out . '<br>';
echo 'Elapsed time: ' . round(microtime(true) - $time, 3) . ' sec';
73 changes: 73 additions & 0 deletions demo/demo2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

use avadim\FastExcelTemplator\Excel;

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../src/autoload.php';

$tpl = __DIR__ . '/files/demo2-tpl.xlsx';
$out = __DIR__ . '/files/demo2-out.xlsx';

$time = microtime(true);

// Open template and set output file
$excel = Excel::template($tpl);
$sheet = $excel->sheet();

$data = [
'Aberdeen City' => [
'January' => 137145,
'February' => 120000,
'March' => 140000,
'April' => 134995,
'May' => 147500,
'June' => 150000,
],
'North Ayrshire' => [
'January' => 114500,
'February' => 110000,
'March' => 135000,
'April' => 100000,
'May' => 127500,
'June' => 125000,
],
'Moray' => [
'January' => 180000,
'February' => 182625,
'March' => 195250,
'April' => 180000,
'May' => 188000,
'June' => 194000,
],
];

$head = $sheet->getRowTemplate(4);
$head->cloneCellTo('D', 'E-I');
$sheet->replaceRow(4, $head, ['D' => 'January', 'E' => 'February', 'F' => 'March', 'G' => 'April', 'H' => 'May', 'I' => 'June']);

$rowTemplate = $sheet->getRowTemplate(5);
$rowTemplate->cloneCellTo('D', ['E', 'F', 'G-I']);
$cnt = 0;
foreach ($data as $locName => $locData) {
$rowData = [
'B' => ++$cnt,
'C' => $locName,
'D' => $locData['January'],
'E' => $locData['February'],
'F' => $locData['March'],
'G' => $locData['April'],
'H' => $locData['May'],
'I' => $locData['June'],
];
if ($cnt === 1) {
$sheet->replaceRow(5, $rowTemplate, $rowData);
}
else {
$sheet->insertRowAfterLast($rowTemplate, $rowData);
}
}

$excel->save($out);

echo 'Output file: ' . $out . '<br>';
echo 'Elapsed time: ' . round(microtime(true) - $time, 3) . ' sec';
File renamed without changes.
Binary file added demo/files/demo2-tpl.xlsx
Binary file not shown.
113 changes: 37 additions & 76 deletions demo/index.php
Original file line number Diff line number Diff line change
@@ -1,76 +1,37 @@
<?php

use avadim\FastExcelTemplator\Excel;

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../src/autoload.php';

$tpl = __DIR__ . '/files/demo-tpl.xlsx';
$out = __DIR__ . '/files/demo-out.xlsx';

$time = microtime(true);

// Open template and set output file
$excel = Excel::template($tpl, $out);
$sheet = $excel->sheet();

$fillData = [
'{{COMPANY}}' => 'Comp Stock Shop',
'{{ADDRESS}}' => '123 ABC Street',
'{{CITY}}' => 'Peace City, TN',
];
$replaceData = ['{{BULK_QTY}}' => 12, '{{DATE}}' => date('m/d/Y')];
$list = [
[
'number' => 'AA-8465-947563',
'description' => 'NEC Mate Type ML PC-MK29MLZD1FWG (Corei5-3470S/2GB/250GB/Multi/No OF/Win 11 Home)',
'price1' => 816,
'price2' => 683,
],
[
'number' => 'QR-7956-048914',
'description' => 'Acer Aspire TC-1760-UA92 (Core i5-12400/12GB 3200MHz DDR4/512GB SSD/Win 11 Home)',
'price1' => 414,
'price2' => 339,
],
[
'number' => 'BD-3966-285936',
'description' => 'Dell Optiplex 7050 SFF (Core i7-7700/32GB DDR4/1TB SSD/Win 10 Pro/Renewed)',
'price1' => 249,
'price2' => 198,
],
];

// Replace strings and substrings on the sheet
$sheet
->fill($fillData)
->replace($replaceData)
;

// Get the specified row (number 6) as a template
$rowTemplate = $sheet->getRowTemplate(6);
$count = 0;
foreach ($list as $record) {
$rowData = [
// In the column A wil be written value from field 'number'
'A' => $record['number'],
// In the column B wil be written value from field 'description'
'B' => $record['description'],
// And so on...
'C' => $record['price1'],
'D' => $record['price2'],
];
if ($count++ === 0) {
// First we replace the row 6 (this is template row)
$sheet->replaceRow(6, $rowTemplate, $rowData);
}
else {
// Then we add new rows after last touched
$sheet->insertRowAfterLast($rowTemplate, $rowData);
}
}

$excel->save();

echo 'Output file: ' . $out . '<br>';
echo 'Elapsed time: ' . round(microtime(true) - $time, 3) . ' sec';
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo FastExcelTemplator</title>
<style>
td {vertical-align: top;}
td img {width: 98%;}
</style>
</head>
<body>
<table>
<tr>
<td colspan="2">
<h2>Demo 1</h2>
<a href="demo1.php" target="_blank">demo1.php</a>
</td>
</tr>
<tr>
<td><img src="../demo1-tpl.png"></td>
<td>&nbsp;</td>
<td><img src="../demo1-out.png"></td>
</tr>
<tr>
<td colspan="2">
<h2>Demo 2</h2>
<a href="demo2.php" target="_blank">demo2.php</a>
</td>
</tr>
<tr>
<td><img src="../demo2-tpl.png"></td>
<td>&nbsp;</td>
<td><img src="../demo2-out.png"></td>
</tr>
</table>
</body>
</html>
File renamed without changes
File renamed without changes
28 changes: 28 additions & 0 deletions src/FastExcelTemplator/RowTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace avadim\FastExcelTemplator;

use avadim\FastExcelHelper\Helper;

class RowTemplate implements \Iterator
{
protected array $domCells = [];
Expand All @@ -20,6 +22,32 @@ public function addCell($colLetter, $cell)
$this->domCells[$colLetter] = $cell;
}

/**
* @param $colLetter
* @param $colTarget
*
* @return void
*/
public function cloneCellTo($colLetter, $colTarget)
{
if (preg_match('/^([a-z]+)(\d+)/i', $colLetter, $m)) {
$colLetter = $m[1];
}
$colLetter = strtoupper($colLetter);
if (isset($this->domCells[$colLetter])) {
$colTarget = Helper::colLetterRange($colTarget);
foreach ($colTarget as $col) {
if (is_object($this->domCells[$colLetter])) {
$cell = clone $this->domCells[$colLetter];
}
else {
$cell = $this->domCells[$colLetter];
}
$this->addCell($col, $cell);
}
}
}

/**
* @param array $attributes
*
Expand Down
2 changes: 1 addition & 1 deletion src/FastExcelTemplator/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public function transferRows(?int $maxRowNum = 0, ?bool $idle = false)
{
if (!$maxRowNum || $maxRowNum > $this->lastReadRowNum) {
foreach ($this->readRow() as $rowNum => $rowData) {
if (!$idle) {
if (!$idle && (!$maxRowNum || $rowNum <= $maxRowNum)) {
$rowNumOut = $rowNum + $this->insertedRowsCount;
if (isset($rowData['__row']['ht'])) {
$this->sheetWriter->setRowHeight($rowNumOut, $rowData['__row']['ht']);
Expand Down

0 comments on commit 845f01f

Please sign in to comment.