question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

merge cells from Array of Arrays

See original GitHub issue

Hi, sorry if this question have been answered somewhere else but i have an array of arrays that is perfectly exported to xlsx file. What I need is to merge the first 2 cells. to make a sort of title for the table Here is my code so far :

exportFile() {
    /* Format Raw Data reveived */
    let formatedData = this.formatData();
    /* convert state to workbook */
    const ws = XLSX.utils.aoa_to_sheet(formatedData);
    const wb = XLSX.utils.book_new();
      wb = {
        SheetNames:["sheet1"],
        sheet1:{
          "!merges":[
            { s:{r:0,c:0}, e:{r:0,c:1}}
          ]
        }
      }
    XLSX.utils.book_append_sheet(wb, ws, this.state.sheetName);
    /* generate XLSX file */
    const wbout = XLSX.write(wb, { type: "binary", bookType: "xlsx" });
    /* send to client */
    saveAs(
      new Blob([this.s2ab(wbout)], { type: "application/octet-stream" }),
      this.state.fileName
    );
  }

Im using React btw

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

55reactions
reviewhercommented, Jan 23, 2018

You need to build up the worksheet first, then add the merges to the worksheet, then add the worksheet to the workbook: https://jsfiddle.net/1ny97xrb/

/* notice the hole where cell "B1" would be */
var data = [
  ["Merged", "", "C", "D"],
  [1,2,3,4],
  ["a","b","c","d"]
];

/* merge cells A1:B1 */
var merge = { s: {r:0, c:0}, e: {r:0, c:1} };
//var merge = XLSX.utils.decode_range("A1:B1"); // this is equivalent

/* generate worksheet */
var ws = XLSX.utils.aoa_to_sheet(data);

/* add merges */
if(!ws['!merges']) ws['!merges'] = [];
ws['!merges'].push(merge);

/* generate workbook */
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "sheet1");

/* generate file and download */
const wbout = XLSX.write(wb, { type: "array", bookType: "xlsx" });
saveAs(new Blob([wbout], { type: "application/octet-stream" }), "issue964.xlsx");

And if this project has been helpful for you, a star would show support 😃

0reactions
afifalfianocommented, Aug 18, 2021

Thank you

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to enter array formulas in merged cells - Get Digital Help
Select the merged cell. Go to tab "Home" on the ribbon. Press with left mouse button on the "Merge & Center" button to...
Read more >
Combine ranges and arrays in Excel: VSTACK & HSTACK ...
The tutorial shows how to merge multiple arrays in Excel 365 vertically and horizontally using VSTACK and HSTACK functions.
Read more >
Merge/flatten an array of arrays - javascript - Stack Overflow
For older browsers, you can use Array.prototype.concat to merge arrays: ... work on huge (e.g. 200 000 elements) arrays, and even if they...
Read more >
5 ways to merge arrays in JavaScript and their differences
To merge elements from one array to another, we must first iterate(loop) through all the array elements. In the loop, we will retrieve...
Read more >
Combine Cell Arrays - MATLAB & Simulink - MathWorks
To combine cell arrays of character vectors into one character vector, use the strjoin function.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found