diff --git a/app/Helpers/ExcelMergeHelper.php b/app/Helpers/ExcelMergeHelper.php new file mode 100644 index 00000000..06f68c66 --- /dev/null +++ b/app/Helpers/ExcelMergeHelper.php @@ -0,0 +1,93 @@ + $sheetsToMerge) { + // Load the current file + $currentSpreadsheet = IOFactory::load($filePath); + + // Debugging: Log the number of sheets in the current file + error_log("File: $filePath has " . $currentSpreadsheet->getSheetCount() . " sheets."); + + // If this is the first file, initialize the base spreadsheet + if ($spreadsheet === null) { + $spreadsheet = $currentSpreadsheet; + + // If specific sheets are provided for the first file, remove the others + if (!is_null($sheetsToMerge) && !empty($sheetsToMerge)) { + $sheetCount = $spreadsheet->getSheetCount(); + for ($i = $sheetCount - 1; $i >= 0; $i--) { + if (!in_array($i, $sheetsToMerge)) { + $spreadsheet->removeSheetByIndex($i); + } + } + } + } else { + // If we are not working with the first file, we need to merge specified sheets + if (is_null($sheetsToMerge) || empty($sheetsToMerge)) { + // If no specific sheets, merge all sheets + $sheetsToMerge = range(0, $currentSpreadsheet->getSheetCount() - 1); + } + + // Merge the specified sheets from the current file + foreach ($sheetsToMerge as $sheetIndex) { + // Check if sheet index is within bounds + if ($sheetIndex < 0 || $sheetIndex >= $currentSpreadsheet->getSheetCount()) { + throw new Exception("Invalid sheet index $sheetIndex in file $filePath"); + } + + // Retrieve the sheet to copy + $sheetToCopy = $currentSpreadsheet->getSheet($sheetIndex); + + // Handle sheet name collision by renaming the sheet with a counter + $baseName = $sheetToCopy->getTitle(); + $sheetName = $baseName; + $counter = 1; + while ($spreadsheet->sheetNameExists($sheetName)) { + $sheetName = $baseName . '_merged_' . $counter; + $counter++; + } + $sheetToCopy->setTitle($sheetName); + + // Clone and add the sheet to the base spreadsheet + $spreadsheet->addSheet(clone $sheetToCopy); + } + } + } + + // Save the merged file + $writer = new Xlsx($spreadsheet); + $writer->save($outputPath); + + return true; // Return true if merge was successful + } catch (Exception $e) { + // Log the error (optional) and return false on failure + error_log("Excel Merge Error: " . $e->getMessage()); + return false; + } + } +} diff --git a/composer.json b/composer.json index da1c83ff..73453d9c 100755 --- a/composer.json +++ b/composer.json @@ -33,6 +33,7 @@ "friendsofphp/php-cs-fixer": "^3.47.1", "kint-php/kint": "^5.0.4", "mikey179/vfsstream": "^1.6", + "mockery/mockery": "^1.6", "nexusphp/cs-config": "^3.6", "phpunit/phpunit": "^9.1", "predis/predis": "^1.1 || ^2.0" diff --git a/tests/unit/ExcelMergeHelperTest.php b/tests/unit/ExcelMergeHelperTest.php new file mode 100755 index 00000000..e648f0b6 --- /dev/null +++ b/tests/unit/ExcelMergeHelperTest.php @@ -0,0 +1,25 @@ + [1,3,2], + '/home/venba/Downloads/enrollment.xlsx' => [] + ]; + $outputPath = '/home/venba/Documents/merged_file.xlsx'; // Output path for the merged file + + // Call the mergeExcelFiles function + $result = ExcelMergeHelper::mergeExcelFiles($filePaths, $outputPath); + var_dump($result); + // Check if the result is true (indicating success) + $this->assertTrue($result, 'Expected true, but got false'); + } + +} \ No newline at end of file