0

I have some data on my front-end that is just an array of arrays with numbers, i.e.:

data: number[][]

data = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]

What I would like to do is to click a button, and then these three arrays are copied into my clipboard, so that I can paste it into e.g. Excel.

For that I do the following:

type Props = {
  exportData: number[][];
};

const ExportBtn = ({ exportData }: Props) => {

  const copy = () => {
    let data = exportData.map((lines) => lines.join("\n")).join("\n");

    navigator.clipboard.writeText(data);

  };

  return (
    <>
      <button type="button" onClick={copy}> Copy </button>
    </>
  );
};

export default ExportBtn;

If I console log the data from the copy function, the result is three rows with the above content, i.e.:

1  2  3  4
2  3  4  5
3  4  5  6

And since this is going to be pasted into Excel I would like this to be transposed, so the result looks like:

1  2  3
2  3  4
3  4  5
4  5  6

But I am not sure what I need to do in order to make this happen ?

Denver Dang
  • 2,069
  • 1
  • 29
  • 56

0 Answers0