Skip to content

Commit f8229b0

Browse files
committed
XlsxFastEditorRow::getCell* functions accept column name
* `XlsxFastEditorRow::getCell*()` functions also accept a column name such as `'D'` in addition of accepting the full cell name such as `'D4'` * `XlsxFastEditorRow::getCell*()` functions are now case-insensitive for the cell name such as `'d4'` or `'D4'`
1 parent db33c80 commit f8229b0

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

src/XlsxFastEditor.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ public static function cellOrderCompare(string $ref1, string $ref2): int
575575
* The corresponding row can also be automatically created if it does not exist already, but the worksheet cannot be automatically created.
576576
*
577577
* @param int $sheetNumber Worksheet number (base 1)
578-
* @param $cellName Cell name such as `B4`
578+
* @param string $cellName Cell name such as `B4`
579579
* @param int $accessMode To control the behaviour when the cell does not exist:
580580
* set to `XlsxFastEditor::ACCESS_MODE_NULL` to return `null` (default),
581581
* set to `XlsxFastEditor::ACCESS_MODE_EXCEPTION` to raise an `XlsxFastEditorInputException` exception,
@@ -614,7 +614,7 @@ public function getCell(int $sheetNumber, string $cellName, int $accessMode = Xl
614614
case XlsxFastEditor::ACCESS_MODE_AUTOCREATE:
615615
$rowNumber = (int)preg_replace('/[^\d]+/', '', $cellName);
616616
if ($rowNumber === 0) {
617-
throw new \InvalidArgumentException("Invalid cell reference {$cellName}!");
617+
throw new \InvalidArgumentException("Invalid line in cell reference {$cellName}!");
618618
}
619619
$row = $this->getRow($sheetNumber, $rowNumber, $accessMode);
620620
return $row->getCell($cellName, $accessMode);
@@ -630,7 +630,7 @@ public function getCell(int $sheetNumber, string $cellName, int $accessMode = Xl
630630
/**
631631
* Access the specified cell in the specified worksheet, or null if if does not exist.
632632
* @param int $sheetNumber Worksheet number (base 1)
633-
* @param $cellName Cell name such as `B4`
633+
* @param string $cellName Cell name such as `B4`
634634
* @return XlsxFastEditorCell|null A cell, potentially `null` if the cell does not exist
635635
* @throws \InvalidArgumentException if `$cellName` has an invalid format
636636
* @throws XlsxFastEditorFileFormatException
@@ -650,7 +650,7 @@ public function getCellOrNull(int $sheetNumber, string $cellName): ?XlsxFastEdit
650650
* Access the specified cell in the specified worksheet, or autocreate it if it does not already exist.
651651
* The corresponding row can also be automatically created if it does not exist already, but the worksheet cannot be automatically created.
652652
* @param int $sheetNumber Worksheet number (base 1)
653-
* @param $cellName Cell name such as `B4`
653+
* @param string $cellName Cell name such as `B4`
654654
* @return XlsxFastEditorCell A cell
655655
* @throws \InvalidArgumentException if `$cellName` has an invalid format
656656
* @throws XlsxFastEditorFileFormatException
@@ -670,7 +670,7 @@ public function getCellAutocreate(int $sheetNumber, string $cellName): XlsxFastE
670670
* Read a formula in the given worksheet at the given cell location.
671671
*
672672
* @param int $sheetNumber Worksheet number (base 1)
673-
* @param $cellName Cell name such as `B4`
673+
* @param string $cellName Cell name such as `B4`
674674
* @throws \InvalidArgumentException if `$cellName` has an invalid format
675675
* @throws XlsxFastEditorFileFormatException
676676
* @throws XlsxFastEditorXmlException
@@ -685,7 +685,7 @@ public function readFormula(int $sheetNumber, string $cellName): ?string
685685
* Read a float in the given worksheet at the given cell location.
686686
*
687687
* @param int $sheetNumber Worksheet number (base 1)
688-
* @param $cellName Cell name such as `B4`
688+
* @param string $cellName Cell name such as `B4`
689689
* @throws \InvalidArgumentException if `$cellName` has an invalid format
690690
* @throws XlsxFastEditorFileFormatException
691691
* @throws XlsxFastEditorXmlException
@@ -700,7 +700,7 @@ public function readFloat(int $sheetNumber, string $cellName): ?float
700700
* Read a date/time in the given worksheet at the given cell location.
701701
*
702702
* @param int $sheetNumber Worksheet number (base 1)
703-
* @param $cellName Cell name such as `B4`
703+
* @param string $cellName Cell name such as `B4`
704704
* @throws \InvalidArgumentException if `$cellName` has an invalid format
705705
* @throws XlsxFastEditorFileFormatException
706706
* @throws XlsxFastEditorXmlException
@@ -715,7 +715,7 @@ public function readDateTime(int $sheetNumber, string $cellName): ?\DateTimeImmu
715715
* Read an integer in the given worksheet at the given cell location.
716716
*
717717
* @param int $sheetNumber Worksheet number (base 1)
718-
* @param $cellName Cell name such as `B4`
718+
* @param string $cellName Cell name such as `B4`
719719
* @throws \InvalidArgumentException if `$cellName` has an invalid format
720720
* @throws XlsxFastEditorFileFormatException
721721
* @throws XlsxFastEditorXmlException
@@ -754,7 +754,7 @@ public function _getSharedString(int $stringNumber): ?string
754754
* compatible with the shared string approach.
755755
*
756756
* @param int $sheetNumber Worksheet number (base 1)
757-
* @param $cellName Cell name such as `B4`
757+
* @param string $cellName Cell name such as `B4`
758758
* @throws \InvalidArgumentException if `$cellName` has an invalid format
759759
* @throws XlsxFastEditorFileFormatException
760760
* @throws XlsxFastEditorXmlException
@@ -796,7 +796,7 @@ public function _getHyperlink(int $sheetNumber, string $rId): ?string
796796
* Read a hyperlink in the given worksheet at the given cell location.
797797
*
798798
* @param int $sheetNumber Worksheet number (base 1)
799-
* @param $cellName Cell name such as `B4`
799+
* @param string $cellName Cell name such as `B4`
800800
* @throws \InvalidArgumentException if `$cellName` has an invalid format
801801
* @throws XlsxFastEditorFileFormatException
802802
* @throws XlsxFastEditorXmlException
@@ -843,7 +843,7 @@ public function _setHyperlink(int $sheetNumber, string $rId, string $value): boo
843843
* Warning: does not support the creation of a new hyperlink.
844844
*
845845
* @param int $sheetNumber Worksheet number (base 1)
846-
* @param $cellName Cell name such as `B4`
846+
* @param string $cellName Cell name such as `B4`
847847
* @return bool True if the hyperlink could be replaced, false otherwise.
848848
* @throws \InvalidArgumentException if `$cellName` has an invalid format
849849
* @throws XlsxFastEditorFileFormatException
@@ -861,7 +861,7 @@ public function writeHyperlink(int $sheetNumber, string $cellName, string $value
861861
* Removes the formulas of the cell, if any.
862862
*
863863
* @param int $sheetNumber Worksheet number (base 1)
864-
* @param $cellName Cell name such as `B4`
864+
* @param string $cellName Cell name such as `B4`
865865
* @throws \InvalidArgumentException if `$cellName` has an invalid format
866866
* @throws XlsxFastEditorFileFormatException
867867
* @throws XlsxFastEditorXmlException
@@ -878,7 +878,7 @@ public function writeFormula(int $sheetNumber, string $cellName, string $value):
878878
* Removes the formulas of the cell, if any.
879879
*
880880
* @param int $sheetNumber Worksheet number (base 1)
881-
* @param $cellName Cell name such as `B4`
881+
* @param string $cellName Cell name such as `B4`
882882
* @param float $value
883883
* @throws \InvalidArgumentException if `$cellName` has an invalid format
884884
* @throws XlsxFastEditorFileFormatException
@@ -896,7 +896,7 @@ public function writeFloat(int $sheetNumber, string $cellName, float $value): vo
896896
* Removes the formulas of the cell, if any.
897897
*
898898
* @param int $sheetNumber Worksheet number (base 1)
899-
* @param $cellName Cell name such as `B4`
899+
* @param string $cellName Cell name such as `B4`
900900
* @param int $value
901901
* @throws \InvalidArgumentException if `$cellName` has an invalid format
902902
* @throws XlsxFastEditorFileFormatException
@@ -959,7 +959,7 @@ public function _makeNewSharedString(string $value): int
959959
* Removes the formulas of the cell, if any.
960960
*
961961
* @param int $sheetNumber Worksheet number (base 1)
962-
* @param $cellName Cell name such as `B4`
962+
* @param string $cellName Cell name such as `B4`
963963
* @throws \InvalidArgumentException if `$cellName` has an invalid format
964964
* @throws XlsxFastEditorFileFormatException
965965
* @throws XlsxFastEditorXmlException

src/XlsxFastEditorRow.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function getFirstCell(): ?XlsxFastEditorCell
111111

112112
/**
113113
* Get the cell of the given name if it exists.
114-
* @param $cellName Cell name such as `B4`
114+
* @param string $cellName Column name such as `'B'` or full cell name such as `'B4'`
115115
* @param int $accessMode To control the behaviour when the cell does not exist:
116116
* set to `XlsxFastEditor::ACCESS_MODE_NULL` to return `null` (default),
117117
* set to `XlsxFastEditor::ACCESS_MODE_EXCEPTION` to raise an `XlsxFastEditorInputException` exception,
@@ -125,9 +125,13 @@ public function getFirstCell(): ?XlsxFastEditorCell
125125
*/
126126
public function getCell(string $cellName, int $accessMode = XlsxFastEditor::ACCESS_MODE_NULL): ?XlsxFastEditorCell
127127
{
128-
if (!ctype_alnum($cellName)) {
128+
if (ctype_alpha($cellName)) {
129+
$cellName .= $this->number();
130+
} elseif (!ctype_alnum($cellName)) {
129131
throw new \InvalidArgumentException("Invalid cell reference {$cellName}!");
130132
}
133+
$cellName = strtoupper($cellName);
134+
131135
$xpath = $this->getXPath();
132136
$cs = $xpath->query("./o:c[@r='$cellName'][1]", $this->r);
133137
$c = null;
@@ -140,6 +144,11 @@ public function getCell(string $cellName, int $accessMode = XlsxFastEditor::ACCE
140144

141145
if ($c === null && $accessMode === XlsxFastEditor::ACCESS_MODE_AUTOCREATE) {
142146
// The cell <c> was not found
147+
$rowNumber = (int)preg_replace('/[^\d]+/', '', $cellName);
148+
if ($rowNumber !== $this->number()) {
149+
throw new \InvalidArgumentException("Invalid line in cell reference {$cellName} for line {$this->number()}!");
150+
}
151+
143152
$dom = $xpath->document;
144153
try {
145154
$c = $dom->createElement('c');
@@ -171,7 +180,7 @@ public function getCell(string $cellName, int $accessMode = XlsxFastEditor::ACCE
171180

172181
/**
173182
* Get the cell of the given name, or null if if does not exist.
174-
* @param $cellName Cell name such as `B4`
183+
* @param string $cellName Column name such as `'B'` or full cell name such as `'B4'`
175184
* @return XlsxFastEditorCell|null A cell, potentially `null` if the cell does not exist
176185
* @throws \InvalidArgumentException if `$cellName` has an invalid format
177186
* @throws XlsxFastEditorXmlException
@@ -188,7 +197,7 @@ public function getCellOrNull(string $cellName): ?XlsxFastEditorCell
188197

189198
/**
190199
* Get the cell of the given name, or autocreate it if it does not already exist.
191-
* @param $cellName Cell name such as `B4`
200+
* @param string $cellName Column name such as `'B'` or full cell name such as `'B4'`
192201
* @return XlsxFastEditorCell A cell
193202
* @throws \InvalidArgumentException if `$cellName` has an invalid format
194203
* @throws XlsxFastEditorXmlException

tests/test.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,21 @@
6060
assert($xlsxFastEditor->getRow($sheet1, 1)?->getFirstCell()?->name() === 'A1');
6161
assert($xlsxFastEditor->getRow($sheet1, 2)?->number() === 2);
6262
assert($xlsxFastEditor->getRow($sheet1, 3)?->getLastCell()?->name() === 'F3');
63-
assert($xlsxFastEditor->getRow($sheet1, 4)?->getCellOrNull('D4')?->name() === 'D4');
6463
assert($xlsxFastEditor->getLastRow($sheet1)?->number() === 4);
6564

65+
$row4 = $xlsxFastEditor->getRow($sheet1, 4);
66+
assert($row4 !== null);
67+
assert($row4->getCellOrNull('D4')?->name() === 'D4');
68+
assert($row4->getCellOrNull('d4')?->name() === 'D4');
69+
assert($row4->getCellOrNull('D')?->name() === 'D4');
70+
$ex = null;
71+
try {
72+
assert($row4->getCellAutocreate('D5')->name() === 'D5');
73+
} catch (\InvalidArgumentException $aex) {
74+
$ex = $aex;
75+
}
76+
assert($ex instanceof \InvalidArgumentException);
77+
6678
assert(XlsxFastEditor::cellOrderCompare('B3', 'AA23') < 0);
6779
assert(XlsxFastEditor::cellOrderCompare('AA23', 'AB23') < 0);
6880
assert(XlsxFastEditor::cellOrderCompare('BB22', 'BB123') < 0);

0 commit comments

Comments
 (0)