convert-excel-to-json icon indicating copy to clipboard operation
convert-excel-to-json copied to clipboard

How to convert empty space cell's to null values

Open kgrvamsi opened this issue 6 years ago • 15 comments

How can i keep the empty spaces with cells to match the header data.(is there a way to define empty space as null or none value when converted to json)

Example: header cells :14 Cells: 11(3 cells are empty space cells)

kgrvamsi avatar Jul 06 '18 02:07 kgrvamsi

Can you post here the full config (header, columnToKey, etc.) you are passing in to the lib?

DiegoZoracKy avatar Jul 07 '18 14:07 DiegoZoracKy

Hi @kgrvamsi,

Did you solve your issue? Can we close this?

DiegoZoracKy avatar Aug 28 '18 00:08 DiegoZoracKy

@DiegoZoracKy i'm really sorry i didn't looked into this issue ...my bad but here is the config i use as part of the conversion.

return excelToJson({
        sourceFile: data.file,
        outputJSON: false,
      });

I'm expecting something like this

return excelToJson({
        sourceFile: data.file,
        outputJSON: false,
        blankSpaceToNull: true, \\ kind of add the empty value to a Null String value
      });

kgrvamsi avatar May 22 '19 20:05 kgrvamsi

No problem @kgrvamsi. Would mind explaining your use case? Why do you need the property with a null value?

DiegoZoracKy avatar May 22 '19 21:05 DiegoZoracKy

the excel sheet's i convert at times i see that even if there is a space it would result me with a undefined value and if there is no value in the cell that that value won't populate in the json array which is good and simplifies my work more on to defect for the right key(A,B,C,D....)

kgrvamsi avatar May 22 '19 21:05 kgrvamsi

Thanks for the awesome library @DiegoZoracKy . I believe I have a similar use case to @kgrvamsi

Given the following spreadsheet

image

Current result:

{
  "11111": {
    "col1": "x",
    "col2": "x"
  },
  "22222": {
    "col2": "x",
    "col3": "x"
  }
}

Desired result:

{
  "11111": {
    "col1": "x",
    "col2": "x",
    "col3": null
  },
  "22222": {
    "col1": null,
    "col2": "x",
    "col3": "x"
  }
}

It would be nice to have an option to choose whether to return "property: null" or "no property" for empty cells.

The use case is for creating objects that need to have values or nulls for all the properties in order to be complete.

miekademarco avatar Feb 26 '20 06:02 miekademarco

Hi @micdemarco and @kgrvamsi (again), I get the idea and I believe that it will be good for us to move forward with it. We will need:

  • New config option named sheetStubs to tell when those null properties should appear on the result data.

  • A new condition capable of preserving those properties when sheetStubs config is set to true

The following line is where a new choice has to be made. The ".v" can be undefined however ".z" will have a value on these cases:

https://github.com/DiegoZoracKy/convert-excel-to-json/blob/a716b606426e7a67317b3ef5f92e1ddf4eb2f686/lib/convert-excel-to-json.js#L46

Tell me if some of you would like to work on this issue, so I can provide further guidance on it.

DiegoZoracKy avatar Feb 27 '20 22:02 DiegoZoracKy

@DiegoZoracKy sure, I'll be happy to make the changes

miekademarco avatar Mar 04 '20 08:03 miekademarco

Great! If you need more info on this specific case or just about the internals of this lib you can keep posting here and I'll help you.

DiegoZoracKy avatar Mar 04 '20 23:03 DiegoZoracKy

Hi @DiegoZoracKy ! Thanks for the awesome lib. Is this issue closed ? What new key do we have to add for enabling this ? Had another issue though. Dummy rows are getting converted with all fields as blank string ? How to tackle this issue?

harsh07bharvada avatar May 26 '20 10:05 harsh07bharvada

@harsh07bharvada some work was done in this pr https://github.com/DiegoZoracKy/convert-excel-to-json/pull/39 awaiting feedback from @DiegoZoracKy

miekademarco avatar Jun 05 '20 01:06 miekademarco

Hi @micdemarco and @harsh07bharvada, I'm sorry for the delay. I'm really facing a huge lack of time currently. @micdemarco the PR is merged now and thanks for working on it!

@harsh07bharvada can you try to use this new version, see if that works well for your case and return here with a feedback?

DiegoZoracKy avatar Jun 05 '20 14:06 DiegoZoracKy

Hi @DiegoZoracKy i also facing same issue. My case is i want to insert excel data into database. in this case, excelToJson({ source: fs.readFileSync(filePath), header:{ rows: 1 }, columnToKey: { A: 'type', B: 'name', C: 'company', D: 'orgID', E: 'isParent', F: 'parentCompany', G: 'primaryContact', H: 'taxIdNumber', I: 'phoneNumber', J: 'tollFree' } })

Above configuration does not returns null values. But i need that null columns also. suggest me how to handle this.

intermx-krishnan avatar Jul 09 '20 12:07 intermx-krishnan

Hi @intermx-krishnan, the README.md hasn't been updated yet with the new feature brought by @micdemarco and released on version 1.7.0. You just have to define sheetStubs on the config object passed in to excelToJson (right @micdemarco?) . You can check the example on the test file pushed on this PR:

https://github.com/DiegoZoracKy/convert-excel-to-json/pull/39/files

DiegoZoracKy avatar Jul 10 '20 02:07 DiegoZoracKy

@intermx-krishnan @DiegoZoracKy

Yes, correct, i've added a pr to update the README

miekademarco avatar Jul 15 '20 03:07 miekademarco