I am attempting to use an HTML form to display a "summary" section that contains an array of the headers (first row) of a spreadsheet file when the file is chosen and supplied to the File Input field of the form.
The AJAX request sends the file to the Wordpress backend where it is processed using wp_handle_upload, a PHP script attempts to identify the file type and create the appropriate Reader, then loads the file into the reader.
From here I would like to write some parsing script, unlink the file to get rid of PII for privacy compliance, then return the relevant information to the AJAX request.
public function tjg_csbs_ajax_get_spreadsheet_summary($file) {
// Pass file to wp_handle_upload
$upload = wp_handle_upload($file, array('test_form' => false));
// File type using IOFactory::identify()
$file_type = IOFactory::identify($upload['file']);
$reader = IOFactory::createReader($file_type);
// Pass upload to reader
try {
$spreadsheet = $reader->load($upload['file']);
unlink($upload['file']);
} catch (Exception $e) {
unlink($upload['file']);
wp_send_json_error('Error loading file: ' . $e->getMessage());
die();
}
}
I've verified that IOFactory::identify() and IOFactory::createReader() seem to be working, but when I call $reader->load() I am hitting a brick wall over and over.
- The file is successfully being uploaded through $_FILES via the AJAX request
- wp_handle_upload successfully handles the file and the file is visible in the wordpress uploads folder
- Different spreadsheet files other than the testing file are returning the same error
Here's the error output:
Fatal error: Uncaught Error: Call to undefined method PhpOffice\PhpSpreadsheet\Reader\Xlsx\Styles::setWorkbookPalette()
in /home1/wp-content/plugins/tjg-csbs/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 566
Call stack:
PhpOffice\P\R\Xlsx::loadSpreadsheetFromFile()
wp-content/plugins/tjg-csbs/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/BaseReader.php:166
PhpOffice\P\R\BaseReader::load()
wp-content/plugins/tjg-csbs/public/class-tjg-csbs-public.php:216
Tjg_Csbs_Public::tjg_csbs_ajax_get_spreadsheet_summary()
wp-content/plugins/tjg-csbs/public/class-tjg-csbs-public.php:186
Tjg_Csbs_Public::tjg_csbs_ajax_primary()
wp-includes/class-wp-hook.php:307
WP_Hook::apply_filters()
wp-includes/class-wp-hook.php:331
WP_Hook::do_action()
wp-includes/plugin.php:476
do_action()
wp-admin/admin-ajax.php:187
I've successfully used PHPSpreadsheet before with classic POST requests without encountering this error when not involving Javascript or AJAX in a different use case.
Any ideas where to check next to get this resolved? Huge thanks.