7

(I used this page https://github.com/SheetJS/js-xlsx for my chrome extension.)

I have a problem with adding a worksheet to a local workbook.

I want to create a worksheet with data from a page, parse and save the data to a worksheet. This worksheet should be stored in a local workbook, which already has worksheets.

Currently I am creating a new workbook every time and saving only on worksheet. This is a sample working code part for create a worksheet, add it to the workbook and downloading the workbook.

var title = "test";
var url = "xyz";

var data = [
    {"A": "Title", "B": title},
    {"A": "URL", "B": url},
  ];

var ws = XLSX.utils.json_to_sheet(data, {skipHeader:true});

var wb = XLSX.utils.book_new();

//Only 31 available characters at the name of the worksheet
while(title.length >= 32){
  title = title.substr(0, title.length-1);
}
var ws_name = title;

// Add the worksheet to the workbook 
XLSX.utils.book_append_sheet(wb, ws, ws_name);

//create and downloading workbook
XLSX.writeFile(wb, 'test.xlsx');

I would be glad if anyone has any idea how it works to add the created worksheet to an existing local xlsx file.

Akriti
  • 773
  • 1
  • 10
  • 19
nise
  • 71
  • 1
  • 2
  • 3
    What you have above should work just fine. I've tested it out myself. Above you create the workbook as wb but you mentioned using an "existing local xlsx" fine. Did you mean to read in the local file first? – Justin Dalrymple Apr 14 '20 at 17:10

0 Answers0