Data Processing数据处理
The "Common Spreadsheet Format" is a simple object representation of the core concepts of a workbook. “通用电子表格格式”是工作簿核心概念的简单对象表示。The utility functions work with the object representation and are intended to handle common use cases.实用工具函数与对象表示一起工作,用于处理常见用例。
Modifying Workbook Structure修改工作簿结构
API
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将工作表附加到工作簿并查找唯一名称
varnew_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
List the Worksheet names in tab order按选项卡顺序列出工作表名称
varwsnames=workbook.SheetNames;
The 工作簿对象的SheetNames
property of the workbook object is a list of the worksheet names in "tab order". SheetNames
属性是按“tab顺序”排列的工作表名称列表。 API functions will look at this array.API函数将查看此数组。
Replace a Worksheet in place将工作表替换到位
workbook.Sheets[sheet_name]=new_worksheet;
The 工作簿对象的Sheets
property of the workbook object is an object whose keys are names and whose values are worksheet objects. Sheets
属性是一个对象,其键为名称,值为工作表对象。By reassigning to a property of the 通过重新指定Sheets
object, the worksheet object can be changed without disrupting the rest of the worksheet structure.Sheets
对象的属性,可以更改工作表对象,而不会中断工作表结构的其余部分。
Examples示例
This example uses XLSX.utils.aoa_to_sheet.本例使用XLSX.utils.aoa_to_sheet
。
varws_name="SheetJS";
/* Create worksheet */
varws_data=[
["S","h","e","e","t","J","S"],
[1,2,3,4,5]
];
varws=XLSX.utils.aoa_to_sheet(ws_data);
/* Add the worksheet to the workbook */
XLSX.utils.book_append_sheet(wb,ws,ws_name);
Modifying Cell Values修改单元格值
API
Modify a single cell value in a worksheet修改工作表中的单个单元格值
XLSX.utils.sheet_add_aoa(worksheet,[[new_value]],{origin:address});
Modify multiple cell values in a worksheet修改工作表中的多个单元格值
XLSX.utils.sheet_add_aoa(worksheet,aoa,opts);
The sheet_add_aoa
utility function modifies cell values in a worksheet. sheet_add_aoa
实用程序函数修改工作表中的单元格值。The first argument is the worksheet object. 第一个参数是工作表对象。 The second argument is an array of arrays of values. 第二个参数是值数组的数组。 The 第三个参数的原点键控制单元格的写入位置。origin
key of the third argument controls where cells will be written. The following snippet sets 以下代码段设置B3=1
and E5="abc"
:B3=1
和E5="abc"
:
XLSX.utils.sheet_add_aoa(worksheet,[
[1],// <-- Write 1 to cell B3
,// <-- Do nothing in row 4
[/*B5*/,/*C5*/,/*D5*/,"abc"]// <-- Write "abc" to cell E5
],{origin:"B3"});
"Array of Arrays Input"“数组输入” describes the function and the optional 更详细地描述了函数和可选opts
argument in more detail.opts
参数。
Examples示例
The special origin value 特殊原点值-1
instructs sheet_add_aoa
to start in column A of the row after the last row in the range, appending the data:-1
指示sheet_add_aoa
从范围中最后一行之后的行A列开始,并附加数据:
XLSX.utils.sheet_add_aoa(worksheet,[
["first row after data",1],
["second row after data",2]
],{origin:-1});
Modifying Other Worksheet / Workbook / Cell Properties修改其他工作表/工作簿/单元格属性
The "Common Spreadsheet Format" section describes the object structures in greater detail.“通用电子表格格式”部分更详细地描述了对象结构。