'receive Excel data and turn into objects to format a JSON

I have this solution that helps me creating a Wizard to fill some data and turn into JSON, the problem now is that I have to receive a xlsx and turn specific data from it into JSON, not all the data but only the ones I want which are documented in the last link.

In this link: https://stackblitz.com/edit/xlsx-to-json I can access the excel data and turn into object (when I print document.getElementById('output').innerHTML = JSON.parse(dataString); it shows [object Object])

I want to implement this solution and automatically get the specified fields in the config.ts but can't get to work. For now, I have these in my HTML and app-component.ts

https://stackblitz.com/edit/angular-xbsxd9 (It's probably not compiling but it's to show the code only)



Solution 1:[1]

It wasn't quite clear what you were asking, but based on the assumption that what you are trying to do is:

  • Given the data in the spreadsheet that is uploaded
  • Use a config that holds the list of column names you want returned in the JSON when the user clicks to download

based on this, I've created a fork of your sample here -> Forked Stackbliz

what I've done is:

  • use the map operator on the array returned from the sheet_to_json method
  • Within the map, the process is looping through each key of the record (each key being a column in this case).
  • If a column in the row is defined in the propertymap file (config), then return it.

This approach strips out all columns you don't care about up front. so that by the time the user clicks to download the file, only the columns you want are returned. If you need to maintain the original columns, then you can move this logic somewhere more convenient for you.

I also augmented the property map a little to give you more granular control over how to format the data in the returned JSON. i.e. don't treat numbers as strings in the final output. you can use this as a template if it suites your needs for any additional formatting.

hope it helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Edward