Plugin Directory

Changeset 3227745


Ignore:
Timestamp:
01/23/2025 10:34:29 PM (14 months ago)
Author:
wibergsweb
Message:

Bugfix Reading xlsx-files

Location:
csv-to-html
Files:
890 added
2 edited

Legend:

Unmodified
Added
Removed
  • csv-to-html/trunk/csvtohtml.php

    r3226738 r3227745  
    44Plugin URI: http://www.wibergsweb.se/plugins/csvtohtml
    55Description:Display/edit/synchronize csv-file(s) dynamically into a html-table
    6 Version: 3.32
     6Version: 3.33
    77Author: Wibergs Web
    88Author URI: http://www.wibergsweb.se/
     
    1414require_once __DIR__ . '/vendor/autoload.php';
    1515use PhpOffice\PhpSpreadsheet\IOFactory; //Used for handling Excel-files
     16use PhpOffice\PhpSpreadsheet\Writer\Csv;
     17use PhpOffice\PhpSpreadsheet\Spreadsheet;
    1618
    1719if( !class_exists('csvtohtmlwp') )
     
    31503152     *
    31513153     */
    3152     private function convert_from_excel( $source, $selected_sheets ) 
     3154    private function convert_from_excel( $source, $selected_sheets )
    31533155    {
    3154         $spreadsheet = IOFactory::load($source);
    3155         if ( !empty( $selected_sheets ) )
    3156         {
    3157             foreach ( $spreadsheet->getAllSheets() as $sheetIndex => $sheet )
    3158             {
    3159                 if ( !in_array($sheet->getTitle(), $selected_sheets) )
    3160                 {
    3161                     $spreadsheet->removeSheetByIndex($sheetIndex);
    3162                 }
    3163             }
    3164         }       
    3165         $excel_csv = IOFactory::createWriter($spreadsheet, 'csv');
    3166         $new_source = preg_replace('/\.(xls|xlsx)$/i', '.csv', $source); //Handle both xls and xlsx
    3167         $excel_csv->save($new_source);
    3168         return $new_source;
     3156        try {
     3157            $reader = IOFactory::createReaderForFile( $source );
     3158            $reader->setReadDataOnly( true ); //No formatting, only data
     3159            $spreadsheet = $reader->load( $source );
     3160   
     3161            //If specific sheets are selected, combine them into one sheet
     3162            if ( !empty( $selected_sheets ) )
     3163            {
     3164                $selected_sheets = explode( ",", $selected_sheets );
     3165                $combinedSpreadsheet = new Spreadsheet();
     3166                $combinedSheet = $combinedSpreadsheet->getActiveSheet();
     3167               
     3168                $rowOffset = 1;   
     3169                foreach ( $selected_sheets as $sheetName )
     3170                {
     3171                    $sheetName = trim( $sheetName );
     3172                    $sheet = $spreadsheet->getSheetByName( $sheetName );
     3173   
     3174                    if ( $sheet !== null )
     3175                    {
     3176                        $highestRow = $sheet->getHighestRow();
     3177                        $highestColumn = $sheet->getHighestColumn();
     3178                        $sheetData = $sheet->rangeToArray( "A1:$highestColumn$highestRow", null, true, true, true );
     3179   
     3180                        foreach ( $sheetData as $rowIndex => $rowData )
     3181                        {
     3182                            $combinedSheet->fromArray($rowData, null, "A" . $rowOffset);
     3183                            $rowOffset++;
     3184                        }
     3185                        //End of the sheet(tab)
     3186                    }
     3187                }
     3188   
     3189                //Save the combined data to a single CSV file
     3190                $new_source = preg_replace( '/\.(xls|xlsx)$/i', '.csv', $source );
     3191                $writer = new Csv( $combinedSpreadsheet );
     3192                $writer->setUseBOM( true ); //UTF-8 BOM
     3193                $writer->save( $new_source );
     3194   
     3195                return $new_source; //Return the path of the generated CSV
     3196            }
     3197   
     3198            // If no specific sheets are selected, save the entire workbook as a single CSV
     3199            $new_source = preg_replace('/\.(xls|xlsx)$/i', '.csv', $source);
     3200            $writer = new Csv($spreadsheet);
     3201            $writer->setUseBOM(true); // Ensure UTF-8 BOM
     3202            $writer->save($new_source);
     3203   
     3204            return $new_source; // Return the path of the generated CSV
     3205        } catch (\PhpOffice\PhpSpreadsheet\Reader\Exception $e) {
     3206            return false;
     3207        } catch (\PhpOffice\PhpSpreadsheet\Writer\Exception $e) {
     3208            return false;
     3209        }
     3210   
     3211        return false; // Return false in case of failure
    31693212    }
    3170 
    3171 
     3213   
     3214   
    31723215    /**
    31733216     *   source_to_table
  • csv-to-html/trunk/readme.txt

    r3226738 r3227745  
    66Requires at least: 3.0.1
    77Tested up to: 6.7
    8 Stable Tag: 3.32
     8Stable Tag: 3.33
    99License: GPLv2
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    140140
    141141== Changelog ==
     142= 3.33 = (2025-01-23)
     143Bugfix Reading xlsx-files
     144
    142145= 3.32 = (2025-01-22)
    143146Readability in code improved aligning to Wordpress standards
     
    662665
    663666== Upgrade notice ==
    664 When upgrading from 3.0.6 security has been improved drastically, so please use
    665 at least version 3.30. If you're using fetch_interval you might have to remove all current files in
    666 your wordpress upload/csvtohtml-arrs folder (if it doesn't exist, no need to do anything).
    667 Please make a little donation and encourage me to continue my development of this plugin.
    668 
     667If you're using Excel-files, you need at least version 3.33.
    669668Please tell me if you're missing something (please mail info@wibergsweb.se for fastest reply) ! I will do my best to add the feature.
Note: See TracChangeset for help on using the changeset viewer.