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

Some CSVs aren't being parsed. #279

Open
barbinbrad opened this issue Oct 15, 2024 · 5 comments
Open

Some CSVs aren't being parsed. #279

barbinbrad opened this issue Oct 15, 2024 · 5 comments

Comments

@barbinbrad
Copy link

barbinbrad commented Oct 15, 2024

I used your code as the basis of a CSV import tool and ran into a tricky bug.

Here's an example of a file that was not parsed by readLines:
random_contacts.csv

Will post a solution when I have one.

@barbinbrad
Copy link
Author

As a quick workaround, I ended up just using the passing the file to Papa.parse instead of using readLines

@pontusab
Copy link
Contributor

thank you! I will have a look once Invoice is done 🙌

@anteriovieira
Copy link

Hi @barbinbrad , can you show us the changes you made?

I think the problem may be related to the count parameter

export const readLines = async (file: File, count = 2): Promise<string> => {
   // ...
   
   if (lines.length >= count) {
        reader.cancel();
        return lines.slice(0, count).join("\n");
   }
   
   // ...
}

@barbinbrad
Copy link
Author

@anteriovieira just something like this:

const processFile = async (file: File) => {
    if (!file) {
      setFileColumns(null);
      return;
    }

    flushSync(() => {
      setLoading(true);
    });

    Papa.parse(file, {
      header: true,
      skipEmptyLines: true,
      error: (error) => {
        setError(error.message);
        setFileColumns(null);
        setFirstRows(null);
        setLoading(false);
      },
      complete: (results) => {
        const { data, meta } = results;

        if (!data || data.length < 2) {
          setError("CSV file must have at least 2 rows.");
          setFileColumns(null);
          setFirstRows(null);
          setLoading(false);
          return;
        }

        if (!meta || !meta.fields || meta.fields.length <= 1) {
          setError("Failed to retrieve CSV column data.");
          setFileColumns(null);
          setFirstRows(null);
          setLoading(false);
          return;
        }

        setFileColumns(meta.fields);
        setFirstRows(data as Record<string, string>[]);
        setLoading(false);
      },
    });
  };

@anteriovieira
Copy link

Thank you very much @barbinbrad

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