Skip to content

Commit

Permalink
add various checks
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Jan 5, 2024
1 parent 985aae2 commit d90b4e6
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions src/ExcelImportExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ public static function getDefaultExtension()
*/
public static function getDefaultWriter(Spreadsheet $spreadsheet): IWriter
{
if (!self::isPhpSpreadsheetAvailable()) {
throw new Exception("PHPSpreadsheet is not installed");
}
$writer = ucfirst(self::getDefaultExtension());
return IOFactory::createWriter($spreadsheet, $writer);
}
Expand Down Expand Up @@ -380,6 +383,11 @@ public static function getReaderForExtension($ext)
}
}

public static function isPhpSpreadsheetAvailable()
{
return class_exists(\PhpOffice\PhpSpreadsheet\Spreadsheet::class);
}

/**
* If you exported separated files, you can merge them in one big file
* Requires PHPSpreadsheet
Expand All @@ -388,6 +396,9 @@ public static function getReaderForExtension($ext)
*/
public static function mergeExcelFiles($files)
{
if (!self::isPhpSpreadsheetAvailable()) {
throw new Exception("PHPSpreadsheet is not installed");
}
$merged = new Spreadsheet;
$merged->removeSheetByIndex(0);
foreach ($files as $filename) {
Expand Down Expand Up @@ -444,16 +455,6 @@ public static function arrayToCsv($data, $filepath, $delimiter = ',', $enclosure
SpreadCompat::write($data, $filepath, $options);
}


/**
* @param IReader $reader
* @return Csv
*/
protected static function getCsvReader(IReader $reader)
{
return $reader;
}

public static function excelColumnRange(string $lower = 'A', string $upper = 'ZZ'): Generator
{
++$upper;
Expand All @@ -479,7 +480,6 @@ public static function getLetter($index)
}
}


/**
* Convert a file to an array
*
Expand All @@ -494,24 +494,15 @@ public static function fileToArray($filepath, $delimiter = ';', $enclosure = '',
if ($ext === null) {
$ext = pathinfo($filepath, PATHINFO_EXTENSION);
}
$readerType = self::getReaderForExtension($ext);
$reader = IOFactory::createReader($readerType);
if ($readerType == 'Csv') {
// @link https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-and-writing-to-file/#setting-csv-options_1
$reader = self::getCsvReader($reader);
$reader->setDelimiter($delimiter);
$reader->setEnclosure($enclosure);
} else {
// Does not apply to CSV
$reader->setReadDataOnly(true);
}
$data = [];
if ($reader->canRead($filepath)) {
$excel = $reader->load($filepath);
$data = $excel->getActiveSheet()->toArray(null, true, false, false);
} else {
throw new Exception("Cannot read $filepath");
}

$data = iterator_to_array(
SpreadCompat::read(
$filepath,
extension: $ext,
separator: $delimiter,
enclosure: $enclosure
)
);
return $data;
}

Expand All @@ -526,6 +517,9 @@ public static function fileToArray($filepath, $delimiter = ';', $enclosure = '',
*/
public static function excelToArray($filepath, $sheetname = null, $onlyExisting = true, $ext = null)
{
if (!self::isPhpSpreadsheetAvailable()) {
throw new Exception("PHPSpreadsheet is not installed");
}
if ($ext === null) {
$ext = pathinfo($filepath, PATHINFO_EXTENSION);
}
Expand Down Expand Up @@ -575,6 +569,9 @@ public static function convertExcelDate($v)
if (!is_numeric($v)) {
return '';
}
if (!self::isPhpSpreadsheetAvailable()) {
throw new Exception("PHPSpreadsheet is not installed");
}
return date('Y-m-d', Date::excelToTimestamp($v));
}

Expand All @@ -591,6 +588,9 @@ public static function convertExcelDate($v)
*/
public static function excelToAssocArray($filepath, $sheetname = null, $ext = null)
{
if (!self::isPhpSpreadsheetAvailable()) {
throw new Exception("PHPSpreadsheet is not installed");
}
if ($ext === null) {
$ext = pathinfo($filepath, PATHINFO_EXTENSION);
}
Expand Down

0 comments on commit d90b4e6

Please sign in to comment.