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

feat: Add sheet context to importSheets callback #369

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

StarkOne
Copy link

Add sheet name to importSheets callback function

Description

This PR enhances the importSheets method by allowing callback functions to receive the current sheet name as the first parameter. This is useful for data validation, especially when the same field names exist across different sheets.

The implementation adds a new withSheetContext() method that, when called, will make importSheets pass the sheet name as the first parameter to the callback function.

@rap2hpoutre This PR solves that issue #366

Changes

  • Added $with_sheet_context property to the Importable trait
  • Added withSheetContext() method to enable the feature
  • Modified importSheet() method to conditionally pass sheet name to callback
  • Updated facade documentation with the new method

Backwards Compatibility

This change maintains full backwards compatibility:

  • Existing code continues to work without modification
  • New functionality is opt-in via the withSheetContext() method

Usage Example

$collections = (new FastExcel)
    ->withSheetContext()
    ->importSheets('file.xlsx', function ($sheetName, $row) {
        // Perform sheet-specific validation
        if ($sheetName === 'Users' && empty($row['email'])) {
            return null; // Skip rows without email in Users sheet
        }
        
        // Add sheet information to result
        $row['_sheet'] = $sheetName;
        
        return $row;
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant