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

Limit on how many rows should be inserted. #240

Open
p1xel007 opened this issue Oct 18, 2021 · 3 comments
Open

Limit on how many rows should be inserted. #240

p1xel007 opened this issue Oct 18, 2021 · 3 comments

Comments

@p1xel007
Copy link

Is it possible to set a limit on how many rows can be imported?

@mcolominas
Copy link

mcolominas commented Oct 19, 2021

It would not make sense to limit imports, if you want to import something, the normal thing is to import the whole sheet, also, you can limit the maximum size of the file that is uploaded to the server..

If you really want to limit imports, you can have a global variable that increases and when it reaches a certain amount call an exception, in this case, I advise you to create your own exception:

$current_imports= 0;
$limit_imports = 1;
try {
    (new FastExcel())->import($file_name, function ($row) use (&$current_imports, $limit_imports) {
        if ($current_imports++ >= $limit_imports) throw new \Exception("Maximum imports reached");

        //Import
    });
} catch (\Throwable $th) {
    //Do something
}

Another alternative you have, is that the import returns a collection of all the data, you can perform a count to know how many records there are:

$results = (new FastExcel())->import($file_name, function($row){
    //parse row but don't insert
    return $row;
});
//Or
$results = (new FastExcel())->import($file_name);

$count_results = count($results);

EDIT:
I advise you to use the second option, since using the first, it will not call:

$reader->close();

EDIT2:
In the first example, the following could also apply:

(new FastExcel())->import($file_name, function ($row) use (&$current_imports, $limit_imports) {
    if ($current_imports++ >= $limit_imports) return null;

    //Import
});

@atymic
Copy link
Contributor

atymic commented Dec 27, 2021

This should be a feature, would you accept a PR? Use case, preview first x lines of file for the purpose of choosing data mapping

@atymic
Copy link
Contributor

atymic commented Dec 27, 2021

.#248

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

No branches or pull requests

3 participants