Workbook Helpers工作簿帮助程序
Many utility functions return worksheet objects. Worksheets cannot be written to workbook file formats directly. They must be added to a workbook object.许多实用程序函数返回工作表对象。工作表不能直接写入工作簿文件格式。必须将它们添加到工作簿对象中。
Create a new workbook创建新工作簿
var wb_sans_sheets = XLSX.utils.book_new();
With no arguments, the 如果没有参数,book_new
utility function creates an empty workbook.book_new
实用程序函数将创建一个空工作簿。
Spreadsheet software generally require at least one worksheet and enforce the requirement in the user interface. For example, if the last worksheet is deleted in the program, Apple Numbers will automatically create a new blank sheet.电子表格软件通常需要至少一个工作表,并在用户界面中强制执行该要求。例如,如果程序中删除了最后一张工作表,Apple Numbers将自动创建一张新的空白工作表。
The SheetJS write functions enforce the requirement. They will throw errors when trying to export empty workbooks.SheetJS写入函数强制执行该要求。当尝试导出空工作簿时,它们将引发错误。
Single Worksheet单个工作表
var wb_with_sheet_named_Sheet1 = XLSX.utils.book_new(worksheet);
var wb_with_sheet_named_Blatte = XLSX.utils.book_new(worksheet, "Blatte");
book_new
can accept one or two arguments.可以接受一个或两个论点。
If provided, the first argument is expected to be a worksheet object. It will be added to the new workbook.如果提供,则第一个参数应为工作表对象。它将被添加到新工作簿中。
If provided, the second argument is the name of the worksheet. If omitted, the default name "Sheet1" will be used.如果提供,第二个参数是工作表的名称。如果省略,将使用默认名称“Sheet1”。
Append a Worksheet to a Workbook将工作表附加到工作簿
XLSX.utils.book_append_sheet(workbook, worksheet, sheet_name);
The book_append_sheet
utility function appends a worksheet to the workbook. book_append_sheet
实用程序函数将工作表附加到工作簿中。The third argument specifies the desired worksheet name. Multiple worksheets can be added to a workbook by calling the function multiple times. 第三个参数指定所需的工作表名称。通过多次调用函数,可以将多个工作表添加到工作簿中。If the worksheet name is already used in the workbook, it will throw an error.如果工作表名称已在工作簿中使用,则会引发错误。
Append a Worksheet to a Workbook and find a unique name将工作表附加到工作簿并查找唯一名称
var new_name = XLSX.utils.book_append_sheet(workbook, worksheet, name, true);
If the fourth argument is 如果第四个参数为true
, the function will start with the specified worksheet name. true
,则函数将以指定的工作表名称开始。 If the sheet name exists in the workbook, a new worksheet name will be chosen by finding the name stem and incrementing the counter.如果工作簿中存在工作表名称,将通过查找名称词干并递增计数器来选择新的工作表名称。
XLSX.utils.book_append_sheet(workbook, sheetA, "Sheet2", true); // Sheet2
XLSX.utils.book_append_sheet(workbook, sheetB, "Sheet2", true); // Sheet3
XLSX.utils.book_append_sheet(workbook, sheetC, "Sheet2", true); // Sheet4
XLSX.utils.book_append_sheet(workbook, sheetD, "Sheet2", true); // Sheet5