Skip to content

Commit c62b1d3

Browse files
committed
Add doc about faster functions
1 parent 9b320ee commit c62b1d3

File tree

6 files changed

+31
-4
lines changed

6 files changed

+31
-4
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ try {
8686

8787
// Methods for rows
8888
$rowNumber = $row->number();
89+
$cell = $row->getCellOrNull('D2');
90+
$cell = $row->getCellOrNull('D');
8991

9092
// Navigation methods for existing cells
9193
$cell = $row->getFirstCell();
92-
$cell = $row->getCellOrNull('D2');
93-
$cell = $row->getCellOrNull('D');
9494
$cell = $cell->getPreviousCell();
9595
$cell = $cell->getNextCell();
9696
$cell = $row->getLastCell();
@@ -125,6 +125,10 @@ try {
125125
}
126126
```
127127

128+
## Tips
129+
130+
* ℹ️ Iterators (`rowsIterator()`, `cellsIterator()`) and navigation methods (`getNextCell()`, etc.) are faster than accessing a cell by name (`getCellOrNull()`) or a row by number (`getRow()`).
131+
128132
## Requirements
129133

130134
PHP 7.4+ with ZIP and XML extensions.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"php-lint": "find . -type d -name 'vendor' -prune -o -name '*.php' -print0 | xargs -0 -n1 -P4 php -l 1>/dev/null",
4141
"phpcs": "phpcs . -s",
4242
"phpcbf": "phpcbf . -p -s",
43-
"phpstan": "phpstan analyse --memory-limit 512M .",
43+
"phpstan": "phpstan analyse .",
4444
"asserts": "php -d zend.assertions tests/test.php",
4545
"test": [
4646
"@php-lint",

src/XlsxFastEditor.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,10 @@ public static function cellOrderCompare(string $ref1, string $ref2): int
576576
* Access the specified cell in the specified worksheet. Can create it automatically if asked to.
577577
* The corresponding row can also be automatically created if it does not exist already, but the worksheet cannot be automatically created.
578578
*
579+
* ℹ️ Instead of calling multiple times this function, consider the faster navigation methods
580+
* `XlsxFastEditor::rowsIterator()`, `XlsxFastEditor::getFirstRow()`, `XlsxFastEditorRow::cellsIterator()`,
581+
* `XlsxFastEditorRow::getNextRow()`, `XlsxFastEditorRow::getFirstCell()`, `XlsxFastEditorCell::getNextCell()`, etc.
582+
*
579583
* @param int $sheetNumber Worksheet number (base 1)
580584
* @param string $cellName Cell name such as `B4`
581585
* @param int $accessMode To control the behaviour when the cell does not exist:
@@ -631,6 +635,11 @@ public function getCell(int $sheetNumber, string $cellName, int $accessMode = Xl
631635

632636
/**
633637
* Access the specified cell in the specified worksheet, or null if if does not exist.
638+
*
639+
* ℹ️ Instead of calling multiple times this function, consider the faster navigation methods
640+
* `XlsxFastEditor::rowsIterator()`, `XlsxFastEditor::getFirstRow()`, `XlsxFastEditorRow::cellsIterator()`,
641+
* `XlsxFastEditorRow::getNextRow()`, `XlsxFastEditorRow::getFirstCell()`, `XlsxFastEditorCell::getNextCell()`, etc.
642+
*
634643
* @param int $sheetNumber Worksheet number (base 1)
635644
* @param string $cellName Cell name such as `B4`
636645
* @return XlsxFastEditorCell|null A cell, potentially `null` if the cell does not exist

src/XlsxFastEditorCell.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function column(): string
6262

6363
/**
6464
* Access the previous existing cell, if any, `null` otherwise.
65+
* ℹ️ This is a faster method than `XlsxFastEditorRow::getCellOrNull()`
6566
*/
6667
public function getPreviousCell(): ?XlsxFastEditorCell
6768
{
@@ -77,6 +78,7 @@ public function getPreviousCell(): ?XlsxFastEditorCell
7778

7879
/**
7980
* Access the next existing cell, if any, `null` otherwise.
81+
* ℹ️ This is a faster method than `XlsxFastEditorRow::getCellOrNull()`
8082
*/
8183
public function getNextCell(): ?XlsxFastEditorCell
8284
{

src/XlsxFastEditorRow.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ private function getXPath(): \DOMXPath
5050

5151
/**
5252
* Access the previous existing row, if any, `null` otherwise.
53+
* ℹ️ This is a faster method than `XlsxFastEditor::getRow()`
5354
*/
5455
public function getPreviousRow(): ?XlsxFastEditorRow
5556
{
@@ -65,6 +66,7 @@ public function getPreviousRow(): ?XlsxFastEditorRow
6566

6667
/**
6768
* Access the next existing row, if any, `null` otherwise.
69+
* ℹ️ This is a faster method than `XlsxFastEditor::getRow()`
6870
*/
6971
public function getNextRow(): ?XlsxFastEditorRow
7072
{
@@ -80,6 +82,7 @@ public function getNextRow(): ?XlsxFastEditorRow
8082

8183
/**
8284
* To iterate over all the existing cells of the row.
85+
* ℹ️ This is a faster method than `XlsxFastEditorRow::getCellOrNull()`
8386
* @return \Traversable<XlsxFastEditorCell>
8487
*/
8588
public function cellsIterator(): \Traversable
@@ -95,6 +98,7 @@ public function cellsIterator(): \Traversable
9598

9699
/**
97100
* Get the first existing cell for a given line.
101+
* ℹ️ This is a faster method than `XlsxFastEditorRow::getCellOrNull()`
98102
* @return XlsxFastEditorCell|null The first cell of the given line if it exists, `null` otherwise.
99103
*/
100104
public function getFirstCell(): ?XlsxFastEditorCell
@@ -111,6 +115,10 @@ public function getFirstCell(): ?XlsxFastEditorCell
111115

112116
/**
113117
* Get the cell of the given name if it exists.
118+
*
119+
* ℹ️ Instead of calling multiple times this function, consider the faster navigation methods
120+
* `XlsxFastEditorRow::cellsIterator()`, `XlsxFastEditorRow::getFirstCell()`, `XlsxFastEditorCell::getNextCell()`, etc.
121+
*
114122
* @param string $cellName Column name such as `'B'` or full cell name such as `'B4'`
115123
* @param int $accessMode To control the behaviour when the cell does not exist:
116124
* set to `XlsxFastEditor::ACCESS_MODE_NULL` to return `null` (default),
@@ -180,6 +188,10 @@ public function getCell(string $cellName, int $accessMode = XlsxFastEditor::ACCE
180188

181189
/**
182190
* Get the cell of the given name, or null if if does not exist.
191+
*
192+
* ℹ️ Instead of calling multiple times this function, consider the faster navigation methods
193+
* `XlsxFastEditorRow::cellsIterator()`, `XlsxFastEditorRow::getFirstCell()`, `XlsxFastEditorCell::getNextCell()`, etc.
194+
*
183195
* @param string $cellName Column name such as `'B'` or full cell name such as `'B4'`
184196
* @return XlsxFastEditorCell|null A cell, potentially `null` if the cell does not exist
185197
* @throws \InvalidArgumentException if `$cellName` has an invalid format

tests/test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157

158158
// Verify by hand that the resulting file opens without warning in Microsoft Excel.
159159
// Verify by hand that the cell Sheet1!E4 has its formula recalculated (to -999) when opening in Excel.
160-
// unlink(__DIR__ . '/copy.xlsx');
160+
// unlink(__DIR__ . '/_copy.xlsx');
161161
} catch (XlsxFastEditorException $xlsxe) {
162162
die($xlsxe);
163163
}

0 commit comments

Comments
 (0)