Compare commits

..

5 Commits

7 changed files with 126 additions and 28 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
vendor/
vendor/
*.cache

24
phpunit.xml Normal file
View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
shortenArraysForExportThreshold="10"
beStrictAboutOutputDuringTests="true"
displayDetailsOnPhpunitDeprecations="true"
failOnPhpunitDeprecation="true"
failOnRisky="true"
failOnWarning="true"
displayDetailsOnTestsThatTriggerWarnings="true">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>
<source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<directory>src</directory>
</include>
</source>
</phpunit>

29
src/ConverterHelpers.php Normal file
View File

@ -0,0 +1,29 @@
<?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
|| strlen($rangeArray[0]) === 0
|| strlen($rangeArray[1]) === 0
|| !(strlen($rangeArray[0]) < strlen($rangeArray[1])
|| (strlen($rangeArray[0]) === strlen($rangeArray[1])
&& strcmp($rangeArray[0], $rangeArray[1]) <= 0))){
throw new MalformattedRangeStringException();
}
$columns = [];
for($column = $rangeArray[0]; $column !== $rangeArray[1]; ++$column) {
array_push($columns, $column);
}
array_push($columns, $column);
return $columns;
}
}

View File

@ -26,7 +26,21 @@ class HtmlConverter {
//ctor
private function __construct(Worksheet $worksheet, int $styleOption = 0, array|null $columns = null, float $scale = 1.0){
$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;
}
else{
$this->columns = ConverterHelpers::RangeToColumnArray('A-'.$worksheet->getHighestDataColumn());
}
$this->scale = $scale;
$this->worksheet = $worksheet;
$this->html = '';
@ -190,7 +204,8 @@ class HtmlConverter {
do {
++$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));
++$row;
return $row;

View File

@ -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);
}
}

View File

@ -0,0 +1,24 @@
<?php
use CSO\Excel2Html\ConverterHelpers;
use CSO\Excel2Html\Exceptions\MalformattedRangeStringException;
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);
}
public function testColumnRangeErrorOnWrongFormat(): void{
$this->expectException(MalformattedRangeStringException::class);
$columns = ConverterHelpers::RangeToColumnArray("-AC");
}
public function testColumnRangeErrorOnTwoLetterColumnFirst(): void{
$this->expectException(MalformattedRangeStringException::class);
$columns = ConverterHelpers::RangeToColumnArray("AC-Y");
}
public function testColumnRangeErrorOnHigherColumnFirst(): void{
$this->expectException(MalformattedRangeStringException::class);
$columns = ConverterHelpers::RangeToColumnArray("Z-B");
}
}

View File

@ -61,6 +61,27 @@ final class HtmlConverterTest extends TestCase{
$expected = file_get_contents('tests/assets/results/testProp.html');
$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-E', 'F'],
scale: 1.1);
$res = $conv->getHtml();
$expected = file_get_contents('tests/assets/results/testProp.html');
$this->assertSame($expected, $res);
}
public function testHtmlPropIsSameWithoutColumns(): void{
$conv = HtmlConverter::fromFilepath(
'tests/assets/test.xlsx',
styleOption: StyleOptions::WITH_COLUMN_WIDTH | StyleOptions::COLUMN_WIDTH_PROPORTIONAL,
worksheetName:'TestTable',
scale: 1.1);
$res = $conv->getHtml();
$expected = file_get_contents('tests/assets/results/testProp.html');
$this->assertSame($expected, $res);
}
//Exceptions
public function testCannotReadFromUnknownSheet(): void{
@ -72,29 +93,4 @@ final class HtmlConverterTest extends TestCase{
columns:['A', 'B', 'C', 'D', 'E', 'F'],
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);
}
}