Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrated PhpSpreadsheet to OpenSpout #1194

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .distignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ artifact
composer.json
composer.lock
package-lock.json
vendor/phpoffice/phpspreadsheet/samples
vendor/phpoffice/phpspreadsheet/docs
vendor/openspout/openspout/LICENSE-for-*
vendor/openspout/openspout/UPGRADE-3.0.md
vendor/openspout/openspout/README.md
cypress
cypress.json
.github
Expand Down
47 changes: 28 additions & 19 deletions classes/Visualizer/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ public function _getDataAs( $chart_id, $type ) {
}

$filename = $title;

switch ( $type ) {
case 'csv':
$final = $this->_getCSV( $rows, $filename, false );
Expand Down Expand Up @@ -324,9 +323,9 @@ private function _getCSV( $rows, $filename, $enclose ) {
* @param string $filename The name of the file to use.
*/
private function _getExcel( $rows, $filename ) {
// PHPExcel did not like sheet names longer than 31 characters and we will assume the same with PhpSpreadsheet
$chart = substr( $filename, 0, 30 );
$filename .= '.xlsx';
// OpenSpout allows for long sheet names, but let's keep the same limit for compatibility.
$chart = substr( $filename, 0, 30 );
$filename .= '.xlsx';
if ( ! apply_filters( 'vizualizer_export_include_series_type', true ) ) {
unset( $rows[1] );
$rows = array_values( $rows );
Expand All @@ -339,22 +338,32 @@ function( $r ) {
}
$vendor_file = VISUALIZER_ABSPATH . '/vendor/autoload.php';
if ( is_readable( $vendor_file ) ) {
include_once( $vendor_file );
}
$xlsData = '';
if ( class_exists( 'PhpOffice\PhpSpreadsheet\Spreadsheet' ) ) {
$doc = new PhpOffice\PhpSpreadsheet\Spreadsheet();
$doc->getActiveSheet()->fromArray( $rows, null, 'A1' );
$doc->getActiveSheet()->setTitle( sanitize_title( $chart ) );
$doc = apply_filters( 'visualizer_excel_doc', $doc );
$writer = PhpOffice\PhpSpreadsheet\IOFactory::createWriter( $doc, 'Xlsx' );
ob_start();
$writer->save( 'php://output' );
$xlsData = ob_get_contents();
ob_end_clean();
include_once $vendor_file;
}
$xlsData = '';
if ( class_exists( 'OpenSpout\Writer\Common\Creator\WriterEntityFactory' ) ) {
try {
// Use OpenSpout to create the XLSX file in memory.
$writer = \OpenSpout\Writer\Common\Creator\WriterEntityFactory::createXLSXWriter();
$writer->openToFile( 'php://output' ); // Open to output instead of a file.
$writer->getCurrentSheet()->setName( sanitize_title( $chart ) );

// Write rows.
foreach ( $rows as $row ) {
$rowFromValues = \OpenSpout\Writer\Common\Creator\WriterEntityFactory::createRowFromArray( $row );
$writer->addRow( $rowFromValues );
}

ob_start();
$writer->close(); // Saves and closes the file in the output buffer.
$xlsData = ob_get_clean();
} catch ( Exception $e ) {
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'OpenSpout writer error: ' . $e->getMessage(), 'error', __FILE__, __LINE__ );
error_log( 'OpenSpout writer error: ' . $e->getMessage() );
}
} else {
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'Class PhpOffice\PhpSpreadsheet\Spreadsheet does not exist!', 'error', __FILE__, __LINE__ );
error_log( 'Class PhpOffice\PhpSpreadsheet\Spreadsheet does not exist!' );
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'Class OpenSpout\Writer\Common\Creator\WriterEntityFactory does not exist!', 'error', __FILE__, __LINE__ );
error_log( 'Class OpenSpout\Writer\Common\Creator\WriterEntityFactory does not exist!' );
}
return array(
'csv' => 'data:application/vnd.ms-excel;base64,' . base64_encode( $xlsData ),
Expand Down
2 changes: 1 addition & 1 deletion classes/Visualizer/Render/Sidebar.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private static function is_excel_enabled() {
return false;
}

return class_exists( 'PhpOffice\PhpSpreadsheet\Spreadsheet' ) && extension_loaded( 'zip' ) && extension_loaded( 'xml' ) && extension_loaded( 'fileinfo' );
return class_exists( 'OpenSpout\Writer\Common\Creator\WriterEntityFactory' ) && extension_loaded( 'zip' ) && extension_loaded( 'xml' ) && extension_loaded( 'fileinfo' );
}

/**
Expand Down
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
},
"require": {
"codeinwp/themeisle-sdk": "^3.3",
"phpoffice/phpspreadsheet": "1.8.2",
"neitanod/forceutf8": "~2.0"
"neitanod/forceutf8": "~2.0",
"openspout/openspout": "^3.7"
},
"autoload": {
"files": [
"vendor/codeinwp/themeisle-sdk/load.php",
"vendor/phpoffice/phpspreadsheet/src/Bootstrap.php"
"vendor/codeinwp/themeisle-sdk/load.php"
]
},
"scripts": {
Expand All @@ -40,7 +39,7 @@
"config": {
"optimize-autoloader": true,
"platform": {
"php": "5.6"
"php": "7.4"
},
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
Expand Down
Loading
Loading