SheetJS / sheetjs

📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs

Home Page:https://sheetjs.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to set name for worksheet separately from workbook

jclayborn-averoinc opened this issue · comments

Currently using version xlsx-0.18.5. When I download the workbook, it is named properly, but the worksheet has the same name, regardless of how I set it.

I have the following code

`{The code from this point and above is just formatting the data into the json structure that creates the spreadsheet}
const packageData = (inData: any, props: any): WorkBook => {
const workBook: WorkBook = XLSX.utils.book_new();
workBook.Props = props;
const workSheet: WorkSheet = XLSX.utils.json_to_sheet(inData);

XLSX.utils.book_append_sheet(workBook, workSheet, workSheetTitle);
return workBook;
};

const releaseData = (workBook: WorkBook) => {
const buffer = XLSX.write(workBook, { bookType: 'csv', type: 'buffer' });
XLSX.write(workBook, { bookType: 'csv', type: 'binary' });
console.log('workbook: ', workBook);
XLSX.writeFile(workBook, ${filename}.csv, { sheet: workSheetTitle });
};`

The console log:
{ "SheetNames": [ "ISR" ], "Sheets": { "ISR": { {REDACTED} }, "Props": { "Author": "SheetJS", "Title": "Report 09_21_2022-09_21_2022" } }

The name of the downloaded file is "Report 09_21_2022-09_21_2022", as is the name of the actual worksheet.

I would expect the name of the file to remain the same as it is now, but the worksheet itself should be named ISR.

The CSV "worksheet name" is not actually stored in the sheet. Excel uses the name stem when opening a CSV file but that is an Excel convention. (note: SheetJS uses Sheet1 for consistency, but that can be overridden)

Ok, Thanks. Is there a way to name that sheet? Because it isn't coming across as Sheet1, either.

SheetJS cannot do anything to change how Excel parses CSV files. If you want Excel to show ISR, you have to write to ISR.csv

The Sheet1 comment refers to how SheetJS parses CSV files. There is no canonical file name since you can pass arbitrary strings or buffers, so the single-sheet parsers default to Sheet1 when reading