DiegoZoracKy / convert-excel-to-json

Convert Excel to JSON, mapping sheet columns to object keys.
MIT License
287 stars 91 forks source link

How to retreive empty columns #24

Closed danny12321 closed 5 years ago

danny12321 commented 5 years ago

Hello,

Thanks for the great module! It works great but I can not see an option to get empty columns in a row. Isn't there any or am I missing something?

My config:

const result = excelToJson({
    sourceFile: file.path
});
DiegoZoracKy commented 5 years ago

Hi @danny12321, can you tell me a bit more about your case?

Let's suppose that you have 3 columns named A, B and C. When there is no content for one of these columns, the object representing the row won't have this property. e.g. !rowResult.A

danny12321 commented 5 years ago

Hi @DiegoZoracKy,

Here is my excel file and the result I got.

image

image

I expected to get an empty string in result[2].B. This is my bad, but is it possible to get an empty string instead of removing result[2].B? This will help me because I am trying to recreate the table.

Thanks in advance.

DiegoZoracKy commented 5 years ago

I got your point. We could have here a config like preserveEmptyColumns to retrieve the column containing a null (not undefined) value when the cell at the source is empty. Currently this feature doesn't exist, and it would be easy to be implemented. Would like to try to do it and push a PR? Here is the first line that should be affected by this new config:

https://github.com/DiegoZoracKy/convert-excel-to-json/blob/44de1e05199baf2ea6551e5189037b974f2a88d9/lib/convert-excel-to-json.js#L91

But apart from that, a suggestion for you to move forward with your case when you know beforehand the headers, is to make a post processing of the data filling the missing keys as you wish.

danny12321 commented 5 years ago

Unfortunately I don't know the headers because users could import their own excel sheet. I will do my best to create the feature and make a PR soon.

Thank you very much for the great module!

DiegoZoracKy commented 5 years ago

Great, and thanks!

danny12321 commented 5 years ago

Unfortunately I can not find a proper solution because the xlsx module does not return the column. I have searched in the config from the xlsx module but I couldn't find any property that will return an empty field.

Do you have any solution? Else I will have to loop all the columns and all the rows and search if there is any data.

Thanks in advance.

DiegoZoracKy commented 5 years ago

"Do you have any solution? Else I will have to loop all the columns and all the rows and search if there is any data."

This is one example of a post processing method that I suggested you before, but to be implemented on your application. I believe the module shouldn't be responsible for handle it in this way because of the performance penalty involved.

Also I believe xlsx has something in this sense already. Try to search for sheetStubs here and see if this can help:

https://github.com/SheetJS/js-xlsx/blob/master/README.md