数据处理
"通用电子表格格式" 是工作簿核心概念的简单对象表示。实用函数与对象表示一起使用,旨在处理常见用例。
¥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.
数据输入 和 数据输出 部分介绍了如何从数据源读取和写入数据源。
¥The Data Input and Data Output sections cover how to read from data sources and write to data sources.
工作簿
¥Workbook
工作表
¥Worksheets
工作表名称区分大小写。
¥Worksheet names are case-sensitive.
按 Tab 键顺序列出工作表名称
¥List the Worksheet names in tab order
var wsnames = workbook.SheetNames;
工作簿对象的 SheetNames
属性是 "选项卡顺序" 中工作表名称的列表。API 函数将查看该数组。
¥The SheetNames
property of the workbook object is a list of the worksheet
names in "tab order". API functions will look at this array.
按名称访问工作表
¥Access a Worksheet by name
var worksheet = workbook.Sheets[sheet_name];
工作簿对象的 Sheets
属性是一个对象,其键是工作表名称,其值是工作表对象。
¥The workbook object's Sheets
property is an object whose keys are sheet names
and whose values are worksheet objects.
访问第一个工作表
¥Access the first Worksheet
var first_ws = workbook.Sheets[workbook.SheetNames[0]];
结合前面的示例,workbook.Sheets[workbook.SheetNames[0]]
是第一个工作表(如果工作簿中存在)。
¥Combining the previous examples, workbook.Sheets[workbook.SheetNames[0]]
is
the first worksheet if it exists in the workbook.
就地替换工作表
¥Replace a Worksheet in place
workbook.Sheets[sheet_name] = new_worksheet;
工作簿对象的 Sheets
属性是一个以名称为键、以工作表对象为值的对象。通过重新分配给 Sheets
对象的属性,可以更改工作表对象,而不会破坏工作表结构的其余部分。
¥The Sheets
property of the workbook object is an object whose keys are names
and whose values are worksheet objects. By reassigning to a property of the
Sheets
object, the worksheet object can be changed without disrupting the
rest of the worksheet structure.
将工作表附加到工作簿
¥Append a Worksheet to a Workbook
XLSX.utils.book_append_sheet(workbook, worksheet, sheet_name);
book_append_sheet
实用程序函数将工作表附加到工作簿。
¥The book_append_sheet
utility function appends a
worksheet to the workbook.
将工作表附加到工作簿并找到唯一的名称
¥Append a Worksheet to a Workbook and find a unique name
var new_name = XLSX.utils.book_append_sheet(workbook, worksheet, name, true);
如果第四个参数是 true
,该函数将尝试查找新的工作表名称,以防发生冲突。
¥If the fourth argument is true
, the function will try to find a new worksheet
name in case of a collision.
示例
¥Examples
var ws_name = "SheetJS";
/* Create worksheet */
var ws_data = [
[ "S", "h", "e", "e", "t", "J", "S" ],
[ 1 , 2 , 3 , 4 , 5 ]
];
var ws = XLSX.utils.aoa_to_sheet(ws_data);
/* Create workbook */
var wb = XLSX.utils.book_new();
/* Add the worksheet to the workbook */
XLSX.utils.book_append_sheet(wb, ws, ws_name);
/* Write to file */
XLSX.writeFile(wb, "SheetJS.xlsx");
其他属性
¥Other Properties
添加定义的名称
¥Add a Defined Name
if(!workbook.Workbook) workbook.Workbook = {};
if(!workbook.Workbook.Names) workbook.Workbook.Names = [];
workbook.Workbook.Names.push({
Name: "SourceData",
Ref: "Sheet1!$A$1:$D$12"
});
这在 "定义名称" 中有更详细的描述。
¥This is described in more detail in "Defined Names".
设置工作簿属性
¥Set Workbook Properties
if(!wb.Props) wb.Props = {};
wb.Props["Company"] = "SheetJS LLC";
完整的属性名称集及其到 Excel UI 的映射包含在 "文件属性" 中
¥The full set of property names, and their mapping to the Excel UI, is included in "File Properties"
工作表
¥Worksheet
单元格
¥Cells
修改工作表中的单个单元格值
¥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);
sheet_add_aoa
实用函数修改工作表中的单元格值。第一个参数是工作表对象。第二个参数是值数组的数组。第三个参数的 origin
键控制单元格的写入位置。以下代码片段设置 B3=1
和 E5="abc"
:
¥The sheet_add_aoa
utility function modifies cell values in a worksheet. 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"
:
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" });
"数组的数组输入" 更详细地描述了该函数和可选的 opts
参数。
¥"Array of Arrays Input" describes the
function and the optional opts
argument in more detail.
示例
¥Examples
特殊原始值 -1
指示 sheet_add_aoa
从范围内最后一行之后的行的 A 列开始,附加数据:
¥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:
XLSX.utils.sheet_add_aoa(worksheet, [
["first row after data", 1],
["second row after data", 2]
], { origin: -1 });
其他属性
¥Other Properties
合并一组单元格
¥Merge a group of cells