Compare commits
8 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
b989de20d3 | |
|
|
397966af7b | |
|
|
6f4fffc305 | |
|
|
777ed5c746 | |
|
|
1be11f6567 | |
|
|
b0e11f931b | |
|
|
3422ac1bac | |
|
|
29ace8a0c3 |
|
|
@ -1 +1,3 @@
|
|||
vendor/
|
||||
vendor/
|
||||
*.cache
|
||||
composer.lock
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@
|
|||
],
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
"phpoffice/phpspreadsheet": "^1.4"
|
||||
"phpoffice/phpspreadsheet": "^1.0 || ^2.0 || ^3.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^11"
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -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>
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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 = '';
|
||||
|
|
@ -130,7 +144,7 @@ class HtmlConverter {
|
|||
?>
|
||||
<td colspan="<?php echo $colspan ?>" excel-col="<?php echo $cell->getColumn() ?>" excel-cell-range="<?php echo $cell->getMergeRange() ?>" style="background: <?php echo $this->getBackground($cell) ?>; <?php echo $this->getBorder($cell) ?>; white-space: <?php echo !$cell->getStyle()->getAlignment()->getWrapText() ? 'nowrap' : ''; ?>;">
|
||||
<?php if ($cell->hasHyperlink()): ?>
|
||||
<a href="<?php echo $cell->getHyperlink()->getUrl() ?>" <?php echo (isset($_GET['linksInNewTabs']) && ($_GET['linksInNewTabs'] == 1)) ? 'target="_blank" rel="noopener noreferrer"' : '' ?> style="text-decoration: <?php echo $this->getTextDecoration($cell) ?>; color: #<?php echo $font->getColor()->getRGB()?>; font-size: <?php echo $this->getFontSizePt($cell) ?>pt; text-align: <?php echo $this->getAlignment($cell) ?>;">
|
||||
<a href="<?php echo $cell->getHyperlink()->getUrl() ?>" style="text-decoration: <?php echo $this->getTextDecoration($cell) ?>; color: #<?php echo $font->getColor()->getRGB()?>; font-size: <?php echo $this->getFontSizePt($cell) ?>pt; text-align: <?php echo $this->getAlignment($cell) ?>;">
|
||||
<text>
|
||||
<?php echo $tag ?>
|
||||
<?php echo $value?>
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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,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");
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue