26

I need to create a xlsx with merged cells using sheetjs.

data:

[
  {
    "id": "nick",
    "items": [
      {
        "name": "ball"
      },
      {
        "name": "phone"
      }
    ]
  },
  {
    "id": "jack",
    "items": [
      {
        "name": "pen"
      },
      {
        "name": "doll"
      }
    ]
  }
]

My code:

var ws = XLSX.utils.json_to_sheet(data);
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "");
var wbout = XLSX.write(wb, {bookType:'xlsx', type:'array'});
saveAs(new Blob([wbout],{type:"application/octet-stream"}), filename + ".xlsx");

The result I want to get:

result

How do I get this result?

... Thank you

chacker
  • 315
  • 1
  • 4
  • 8
  • i am also expecting the lib will create the desired sheet with the given json object. but it seems the nested array is not treated well..will get back when find a solution. for now information i got from google., it seems client need explicitly specify the merge row/column. – RyanShao May 27 '19 at 03:28
  • Hi chacker did you find any solutions ? – Manoj Rejinthala Sep 17 '20 at 11:55

1 Answers1

45
const merge = [
  { s: { r: 1, c: 0 }, e: { r: 2, c: 0 } },{ s: { r: 3, c: 0 }, e: { r: 4, c: 0 } },
];
ws["!merges"] = merge;

Use this code for merge A2:A3 ({ s: { r: 1, c: 0 }, e: { r: 2, c: 0 } }) and A4:A5 ({ s: { r: 3, c: 0 }, e: { r: 4, c: 0 } })

Here s = start, r = row, c=col, e= end

Amjad Rehman A
  • 788
  • 10
  • 21