torbenrudgaard
torbenrudgaard

Reputation: 2541

Excel or SheetJS - how do add multiple lines to an excel sheet from JS?

I am trying to add multiple lines to an excel sheet.

Adding one line works great:

var wb = {
    SheetNames: ["mySheet"],
    Sheets: {
        mySheet: {
            "!ref":"A1:C1",
            A1: { t:"d", v:"30-Jan-1967" },
            B1: { t:"s", v:"This is some text" },
            C1: { t:"n", v:10000 }
        }
    }
}
XLSX.writeFile(wb, "sheetjs.xlsx");

But if I make an object and try to add multiple lines, it wont work cause the lines keep overwriting each other.

var myObject = [];
for (let i = 0; i<10; i++) {
    myObject.push(
        {   
            "!ref":"A1:C1",
            A1: { t:"d", v:"30-Jan-1967" },
            B1: { t:"s", v:"This is no" + i },
            C1: { t:"n", v: (i * 1000) }
        }
    )
}

var wb = {
    SheetNames: ["mySheet"],
    Sheets: {
        mySheet: myObject
    }
}
XLSX.writeFile(wb, "sheetjs.xlsx");

Does anyone have an ides how this could be done? (VBA has special functions to do it, but I cant figure out how to do in JS)

Upvotes: 1

Views: 1798

Answers (1)

satendra
satendra

Reputation: 113

let myObject= { "!ref":"A1:C10" }

for (let col=1; col< 3; col++) {
    for(let row=1; row< 10; row++ ) {
        myObject['A' + row] = {t: "s", v: 'hi A' + row};
        myObject['B' + row] = {t: "s", v: 'hi B' + row};
        myObject['C' + row] = {t: "s", v: 'hi C' + row};
    }
}

You can try this one, this would work!!!

Upvotes: 1

Related Questions