support for column ranges
This commit is contained in:
parent
3422ac1bac
commit
b0e11f931b
|
|
@ -0,0 +1,24 @@
|
||||||
|
<?php
|
||||||
|
namespace CSO\Excel2Html;
|
||||||
|
use CSO\Excel2Html\Exceptions\MalformattedRangeStringException;
|
||||||
|
|
||||||
|
class ConverterHelpers{
|
||||||
|
/**
|
||||||
|
* Converts a range string to a columns array
|
||||||
|
* @param string $range range string like 'A-C'
|
||||||
|
* @return string[] array of all columns in the range like ['A','B','C']
|
||||||
|
*/
|
||||||
|
public static function RangeToColumnArray(string $range): array{
|
||||||
|
$rangeArray = explode("-",$range);
|
||||||
|
if(count($rangeArray) !== 2){
|
||||||
|
throw new MalformattedRangeStringException();
|
||||||
|
}
|
||||||
|
|
||||||
|
$columns = [];
|
||||||
|
for($column = $rangeArray[0]; $column !== $rangeArray[1]; ++$column) {
|
||||||
|
array_push($columns, $column);
|
||||||
|
}
|
||||||
|
array_push($columns, $column);
|
||||||
|
return $columns;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -26,7 +26,18 @@ class HtmlConverter {
|
||||||
//ctor
|
//ctor
|
||||||
private function __construct(Worksheet $worksheet, int $styleOption = 0, array|null $columns = null, float $scale = 1.0){
|
private function __construct(Worksheet $worksheet, int $styleOption = 0, array|null $columns = null, float $scale = 1.0){
|
||||||
$this->styleOption = $styleOption;
|
$this->styleOption = $styleOption;
|
||||||
$this->columns = $columns;
|
if(!is_null($columns)){
|
||||||
|
$tmpColumns = [];
|
||||||
|
foreach($columns as $col){
|
||||||
|
if(str_contains($col, "-")){
|
||||||
|
$tmpColumns = array_merge($tmpColumns, ConverterHelpers::RangeToColumnArray($col));
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
array_push($tmpColumns, $col);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->columns = $tmpColumns;
|
||||||
|
}
|
||||||
$this->scale = $scale;
|
$this->scale = $scale;
|
||||||
$this->worksheet = $worksheet;
|
$this->worksheet = $worksheet;
|
||||||
$this->html = '';
|
$this->html = '';
|
||||||
|
|
@ -190,7 +201,8 @@ class HtmlConverter {
|
||||||
do {
|
do {
|
||||||
++$row;
|
++$row;
|
||||||
$array = $this->worksheet->rangeToArray($minCol.$row.':'.$maxCol.$row);
|
$array = $this->worksheet->rangeToArray($minCol.$row.':'.$maxCol.$row);
|
||||||
$value = trim(implode($array));
|
$valString = implode($array[$row]);
|
||||||
|
$value = trim($valString);
|
||||||
} while ($row <= $highestRow && is_null($value));
|
} while ($row <= $highestRow && is_null($value));
|
||||||
++$row;
|
++$row;
|
||||||
return $row;
|
return $row;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
<?php
|
||||||
|
namespace CSO\Excel2Html\Exceptions;
|
||||||
|
|
||||||
|
class MalformattedRangeStringException extends \Exception {
|
||||||
|
|
||||||
|
public function __construct(string $message = "", int $code = 0, \Throwable $previous = null){
|
||||||
|
parent::__construct($message, $code, $previous);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
use CSO\Excel2Html\ConverterHelpers;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
final class ConverterHelpersTest extends TestCase{
|
||||||
|
public function testColumnRangeWorkingAsExpected(): void{
|
||||||
|
$columns = ConverterHelpers::RangeToColumnArray("Y-AC");
|
||||||
|
$res = implode("|", $columns);
|
||||||
|
$this->assertSame('Y|Z|AA|AB|AC', $res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
use CSO\Excel2Html\ConverterHelpers;
|
||||||
use CSO\Excel2Html\Exceptions\SheetNotFoudException;
|
use CSO\Excel2Html\Exceptions\SheetNotFoudException;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use CSO\Excel2Html\HtmlConverter;
|
use CSO\Excel2Html\HtmlConverter;
|
||||||
|
|
@ -61,6 +62,17 @@ final class HtmlConverterTest extends TestCase{
|
||||||
$expected = file_get_contents('tests/assets/results/testProp.html');
|
$expected = file_get_contents('tests/assets/results/testProp.html');
|
||||||
$this->assertSame($expected, $res);
|
$this->assertSame($expected, $res);
|
||||||
}
|
}
|
||||||
|
public function testHtmlPropIsSameWithRange(): void{
|
||||||
|
$conv = HtmlConverter::fromFilepath(
|
||||||
|
'tests/assets/test.xlsx',
|
||||||
|
styleOption: StyleOptions::WITH_COLUMN_WIDTH | StyleOptions::COLUMN_WIDTH_PROPORTIONAL,
|
||||||
|
worksheetName:'TestTable',
|
||||||
|
columns:['A', 'B-F'],
|
||||||
|
scale: 1.1);
|
||||||
|
$res = $conv->getHtml();
|
||||||
|
$expected = file_get_contents('tests/assets/results/testProp.html');
|
||||||
|
$this->assertSame($expected, $res);
|
||||||
|
}
|
||||||
|
|
||||||
//Exceptions
|
//Exceptions
|
||||||
public function testCannotReadFromUnknownSheet(): void{
|
public function testCannotReadFromUnknownSheet(): void{
|
||||||
|
|
@ -72,29 +84,4 @@ final class HtmlConverterTest extends TestCase{
|
||||||
columns:['A', 'B', 'C', 'D', 'E', 'F'],
|
columns:['A', 'B', 'C', 'D', 'E', 'F'],
|
||||||
scale: 1.1);
|
scale: 1.1);
|
||||||
}
|
}
|
||||||
public function testCannotReadFromUnknownColumn(): void{
|
|
||||||
$this->expectExceptionMessage('Invalid cell coordinate -1');
|
|
||||||
$conv = HtmlConverter::fromFilepath(
|
|
||||||
'tests/assets/test.xlsx',
|
|
||||||
styleOption: StyleOptions::WITH_COLUMN_WIDTH | StyleOptions::COLUMN_WIDTH_PROPORTIONAL,
|
|
||||||
worksheetName:'TestTable',
|
|
||||||
columns:['A', 'B', 'C', 'D', 'E', 'F', '-1'],
|
|
||||||
scale: 1.1);
|
|
||||||
$res = $conv->getHtml();
|
|
||||||
}
|
|
||||||
public function testColumnRangeWorkingAsExpected(): void{
|
|
||||||
$columnRange = function($startColumn, $endColumn) {
|
|
||||||
++$endColumn;
|
|
||||||
for($column = $startColumn; $column !== $endColumn; ++$column) {
|
|
||||||
yield $column;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
$res = '';
|
|
||||||
foreach($columnRange('Y', 'AC') as $column) {
|
|
||||||
$res .= '|'.$column;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->assertSame('|Y|Z|AA|AB|AC', $res);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue