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

Issue with reading a single CSV file #29

Open
jayashraj opened this issue Nov 20, 2021 · 2 comments
Open

Issue with reading a single CSV file #29

jayashraj opened this issue Nov 20, 2021 · 2 comments

Comments

@jayashraj
Copy link

Issue

In the parser.ts file's loadFromCsv, you have directly invoked .split(",") method. This causes an issue when there is only a single CSV file.

Why is it an issue for me?

In my use case I like to have a csv file for a chart in a separate file rather than having them in the working .md file. The benefit of that is that I can use iOS shortcuts to edit the CSV instead of the YAML code in the main .md file. Plus it makes the file look cleaner.

Solution

I have created a solution that works for me, I am just posting it for you to maybe implement it in a cleaner way for everyone else to use, Thanks.

Here is the code:




async function loadFromCsv(data: string, plugin: ChartsViewPlugin): Promise<DataType> {
    var csvFileNames;
    const value = [];
    if (data.includes(",")){
        const csvFileNames = data.split(",");

    for (let name of csvFileNames.values()) {
        const file = plugin.app.vault.getAbstractFileByPath(plugin.settings.dataPath + "/" + name.trim());
        if (file instanceof TFile) {
            value.push(parseCsv(await plugin.app.vault.read(file)));
        } else {
            value.push({});
        }
    }
    } else {
        csvFileNames = data;
        const file = plugin.app.vault.getAbstractFileByPath(csvFileNames.trim());
        console.log(file)
        if (file instanceof TFile) {
            console.log(data, csvFileNames, parseCsv(await plugin.app.vault.read(file)))
            value.push(parseCsv(await plugin.app.vault.read(file)));
        } else {
            value.push({});
        }
    }
    if (value.length == 0) {
        return {};
    } 
    if (value.length == 1) {
        return value[0];
    }
    return value;
}

Thanks a lot for the great plugin.

@caronchen
Copy link
Owner

Many thanks. I tried with a single CSV file, no issues found.
Here are my screenshots.

IMG_0019
IMG_0020

@jayashraj
Copy link
Author

jayashraj commented Nov 23, 2021

For me here is a brand new vault with Chartsview 1.1.12 installed.

data.csv

image

MD

image

Preview

image

Preview With my fix

image

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

2 participants