DiegoZoracKy / convert-excel-to-json

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

Range from the same sheet? #58

Open dotnetshadow opened 3 years ago

dotnetshadow commented 3 years ago

Hi there,

A great library. I was wondering how I could read various ranges from the same sheet:

At the moment I have the following:

image

Season Autumn
Year 2021
Comp Comp1
Round 1
Ranking Team Total
1 Team1 0
2 Team2 0
3 Team3 0
4 Team4 0
5 Team5 0
6 Team6 0
7 Team7 0
8 Team8 0
9 Team9 0
const excelToJson = require('convert-excel-to-json');

const result = excelToJson({
    sourceFile: 'standings.xlsx',
    sheets:[{
        name: 'Ladder',
        //range: 'A1:B4',
        // header:{
        //     rows: 0
        // },
        //'*': '{{columnHeader}}',
        columnToKey: {
            A: 'key',
            B: 'value'
        }
    },{
        name: 'Ladder',
        //range: 'A5:C20',
        // header:{
        //     rows: 5
        // },
        columnToKey: {
            A: 'rank',
            B: 'team',        
            C: 'total'
        }
        //'*': '{{columnHeader}}',
    }]

Result

{
  Ladder: [
    { rank: 'Season', team: 'Autumn' },
    { rank: 'Year', team: 2021 },
    { rank: 'Comp', team: 'Comp1' },
    { rank: 'Round', team: 1 },
    { rank: 'Ranking', team: 'Team', total: 'Total' },
    { rank: 1, team: 'Team1', total: 0 },
    { rank: 2, team: 'Team2', total: 0 },
    { rank: 3, team: 'Team3', total: 0 },
    { rank: 4, team: 'Team4', total: 0 },
    { rank: 5, team: 'Team5', total: 0 },
    { rank: 6, team: 'Team6', total: 0 },
    { rank: 7, team: 'Team7', total: 0 },
    { rank: 8, team: 'Team8', total: 0 },
    { rank: 9, team: 'Team9', total: 0 }
  ]
}

I thought at a minimum it could output:

{
  Ladder: [
    { key: 'Season', value: 'Autumn' },
    { key: 'Year', value: 2021 },
    { key: 'Comp', value: 'Comp1' },
    { key: 'Round', value: 1 },
    { rank: 'Ranking', team: 'Team', total: 'Total' },
    { rank: 1, team: 'Team1', total: 0 },
    { rank: 2, team: 'Team2', total: 0 },
    { rank: 3, team: 'Team3', total: 0 },
    { rank: 4, team: 'Team4', total: 0 },
    { rank: 5, team: 'Team5', total: 0 },
    { rank: 6, team: 'Team6', total: 0 },
    { rank: 7, team: 'Team7', total: 0 },
    { rank: 8, team: 'Team8', total: 0 },
    { rank: 9, team: 'Team9', total: 0 }
  ]
}

Ideally I would like to specify a region for a sheet and it would give me a different array for each section perhaps I could define a section name

heading: [],
ladder[]

I guess the second way is that I would have to have separate excelToJson calls for each section and then merge the results?

Any suggestions would be welcomed?