Arrays of Data数据的数组
Arrays of objects are a common data format in JavaScript database connectors and other general data sources.对象数组是JavaScript数据库连接器和其他通用数据源中的常见数据格式。
Numeric datasets commonly use arrays of arrays of numbers.数字数据集通常使用数字数组的数组。
The "Data Storage" section gives a general overview of common array formats in JavaScript.“数据存储”部分概述了JavaScript中常见的数组格式。
The "Functions" section describes the related functions."Functions"部分介绍了相关功能。
Data Storage数据存储
Array of Arrays数组的数组
The spiritual equivalent of the grid in JavaScript is an array of arrays:JavaScript中网格的精神等价物是数组的数组:
Spreadsheet | |
---|---|
|
|
Each array within the structure corresponds to one row. Individual data points can be read by indexing by row index and by column index:结构中的每个数组对应于一行。可以通过按行索引和按列索引进行索引来读取单个数据点:
var aoa = [
["Name", "Index"],
["Bill Clinton", 42],
["GeorgeW Bush", 43],
["Barack Obama", 44],
["Donald Trump", 45],
["Joseph Biden", 46]
];
var value_at_B4 = aoa[3][1]; // 44
var value_at_A2 = aoa[1][0]; // Bill Clinton
Arrays of Objects对象数组
Arrays of objects are commonly used to represent rows from a database:对象数组通常用于表示数据库中的行:
Spreadsheet | Array of Objects |
---|---|
|
|
Each object within the structure corresponds to one data row. The first row of the spreadsheet is interpreted as the header row.结构中的每个对象都对应于一个数据行。电子表格的第一行被解释为标题行。
Functions函数
The "Input" subsections describe functions that generate SheetJS worksheet objects from arrays of data.“输入”小节描述了从数据数组生成SheetJS工作表对象的函数。
The "Array Output" subsection defines functions that extract data from SheetJS worksheet objects.“数组输出”小节定义了从SheetJS工作表对象中提取数据的函数。
Example Sheet示例工作表
The live examples are based on the following worksheet:实际示例基于以下工作表:
S | h | e | e | t | J | S |
1 | 2 | 5 | 6 | 7 | ||
2 | 3 | 6 | 7 | 8 | ||
3 | 4 | 7 | 8 | 9 | ||
4 | 5 | 6 | 7 | 8 | 9 | 0 |
This table includes duplicate column labels ("e" and "S" appear twice in the first row) and gaps (three data rows have missing fields).该表包括重复的列标签(“e”和“S”在第一行出现两次)和间隙(三个数据行缺少字段)。
Array of Arrays Input数组输入
Create a worksheet from an array of arrays从数组创建工作表
var ws = XLSX.utils.aoa_to_sheet(aoa, opts);
XLSX.utils.aoa_to_sheet
takes an array of arrays of JS values and returns a worksheet resembling the input data. Values are interpreted as follows:获取一个JS值数组数组,并返回一个类似于输入数据的工作表。数值解释如下:
Numbers, Booleans and Strings are stored as the corresponding types.数字、布尔值和字符串存储为相应的类型。Date objects are stored as Date cells or date codes (see日期对象存储为日期单元格或日期代码(请参见cellDates
option)cellDates
选项)Array holes and explicit将跳过数组洞和显式undefined
values are skipped.undefined
值。null
values may be stubbed (see值可能会被截断(请参阅sheetStubs
andnullError
options)sheetStubs
和nullError
选项)Cell objects are used as-is.单元对象按原样使用。
The function takes an options argument:该函数采用一个选项参数:
dateNF | FMT 14 | |
cellDates | false | d (default is n )d (默认为n ) |
sheetStubs | false | z for null valuesnull 值创建z 类型的单元格对象 |
nullError | false | #NULL! error cells for null valuestrue ,则针对null 值发出#NULL! 错误单元格 |
UTC | false | true ,则使用UTC方法解释日期 ** |
dense | false |
UTC option is explained in "Dates"UTC选项在“日期”中有说明
The following live example reproduces the example worksheet:以下活动示例再现了示例工作表:
Add data from an array of arrays to an existing worksheet将数组中的数据添加到现有工作表
XLSX.utils.sheet_add_aoa(ws, aoa, opts);
XLSX.utils.sheet_add_aoa
takes an array of arrays of JS values and updates an existing worksheet object. 获取JS值的数组数组,并更新现有的工作表对象。It follows the same process as 它遵循与aoa_to_sheet
and accepts an options argument:aoa_to_sheet
相同的过程,并接受一个选项参数:
dateNF | FMT 14 | |
cellDates | false | d (default is n )d (默认为n ) |
sheetStubs | false | z for null valuesnull 值创建z 类型的单元格对象 |
nullError | false | #NULL! error cells for null valuestrue ,则针对null 值发出#NULL! 错误单元格 |
origin | ||
UTC | false | true ,则使用UTC方法解释日期 ** |
UTC option is explained in "Dates"UTC选项在“日期”中有说明
origin
is expected to be one of:预计是以下其中之一:
origin | |
---|---|
(cell object) | |
(string) | |
(number >= 0) | |
-1 | |
(default) | A1 A1 开始 |
The example worksheet can be built up in the following order:示例工作表可以按以下顺序构建:
|
0) 1) 2) 3) 4) |
/* Start from an empty worksheet从空工作表开始 */
var ws = XLSX.utils.aoa_to_sheet([[]]);
/* First row第一行 */
XLSX.utils.sheet_add_aoa(ws, [ "SheetJS".split("") ], {origin: "A1"});
/* Write data starting at A2从A2开始写入数据 */
XLSX.utils.sheet_add_aoa(ws, [[1,2], [2,3], [3,4]], {origin: "A2"});
/* Write data starting at E2从E2开始写入数据 */
XLSX.utils.sheet_add_aoa(ws, [[5,6,7], [6,7,8], [7,8,9]], {origin:{r:1, c:4}});
/* Append row */
XLSX.utils.sheet_add_aoa(ws, [[4,5,6,7,8,9,0]], {origin: -1});
Array of Objects Input对象的数组输入
Create a worksheet from an array of objects从对象数组创建工作表
var ws = XLSX.utils.json_to_sheet(aoo, opts);
XLSX.utils.json_to_sheet
takes an array of objects and returns a worksheet with automatically-generated "headers" based on the keys of the objects. 获取一个对象数组,并返回一个工作表,其中包含基于对象键自动生成的“标题”。The default column order is determined by the first appearance of the field using 默认的列顺序由使用Object.keys
. Object.keys
的字段的首次出现来确定。The function accepts an options argument:函数接受一个选项参数:
header | Object.keys )Object.keys ) ** |
|
dateNF | FMT 14 | |
cellDates | false | d (default is n )d (默认为n ) |
skipHeader | false | true ,则不在输出中包括标题行 |
nullError | false | #NULL! error cells for null valuestrue ,则针对null 值发出#NULL! 错误单元格 |
UTC | false | true ,则使用UTC方法解释日期 ** |
dense | false | Emit dense sheet object |
UTC option is explained in "Dates"UTC选项在“日期”中有说明
All fields from each row will be written!将写入每行中的所有字段! header
hints at a particular order but is not exclusive. 暗示了特定的顺序,但不是排他性的。To remove fields from the export, filter the data source.若要从导出中删除字段,请筛选数据源。
Some data sources have special options to filter properties. 某些数据源具有用于筛选属性的特殊选项。For example, MongoDB will add the 例如,MongoDB在从集合中查找数据时会添加_id
field when finding data from a collection:_id
字段:
const aoo_with_id = await coll.find({}).toArray();
const ws = XLSX.utils.json_to_sheet(aoo_with_id); // includes _id column
This can be filtered out through the 这可以通过projection
property:projection
属性筛选掉:
const aoo = await coll.find({}, {projection:{_id:0}}).toArray(); // no _id !
const ws = XLSX.utils.json_to_sheet(aoo);
If a data source does not provide a filter option, it can be filtered manually:如果数据源不提供筛选选项,则可以手动进行筛选:
const aoo = data.map(obj => Object.fromEntries(Object.entries(obj).filter(r => headers.indexOf(r[0]) > -1)));
If如果header
is an array, missing keys will be added in order of first use.header
是一个数组,则将按首次使用的顺序添加缺少的键。Cell types are deduced from the type of each value.单元类型是根据每个值的类型推导出来的。For example, a例如,Date
object will generate a Date cell, while a string will generate a Text cell.Date
对象将生成Date单元格,而字符串将生成Text单元格。Null values will be skipped by default.默认情况下将跳过空值。If如果nullError
is true, an error cell corresponding to#NULL!
will be written to the worksheet.nullError
为true
,则对应于#NULL!
的错误单元格将写入工作表。
The example sheet cannot be reproduced using plain objects since JS object keys must be unique.由于JS对象键必须是唯一的,因此无法使用普通对象复制示例表。
Typically the original data source will have different column names. 通常,原始数据源将具有不同的列名。After writing with 使用json_to_sheet
, the aoa_to_sheet
method can rewrite the headers:json_to_sheet
写入后,aoa_to_sheet
方法可以重写头:
/* original array of objects原始对象数组 */
var data = [
{ S:1, h:2, t:5, J:6, S_1:7 },
{ S:2, h:3, t:6, J:7, S_1:8 },
{ S:3, h:4, t:7, J:8, S_1:9 },
{ S:4, h:5, e:6, e_1:7, t:8, J:9, S_1:0 },
];
/* column order for the generated worksheet生成的工作表的列顺序 */
// | A | B | C | D | E | F | G |
var data_headers = [ "S", "h", "e", "e_1", "t", "J", "S_1" ];
/* new headers for the first row of the worksheet工作表第一行的新标题 */
// | A | B | C | D | E | F | G |
var new_headers = [ "S", "h", "e", "e", "t", "J", "S" ];
/* write data with using data headers使用数据头写入数据 */
var ws = XLSX.utils.json_to_sheet(data, { header: data_headers });
/* replace first row替换第一行 */
XLSX.utils.sheet_add_aoa(worksheet, [new_headers], { origin: "A1" });
Add data from an array of objects to an existing worksheet将对象数组中的数据添加到现有工作表中
XLSX.utils.sheet_add_json(ws, aoo, opts);
XLSX.utils.sheet_add_json
takes an array of objects and updates an existing worksheet object. 获取一个对象数组并更新现有的工作表对象。It follows the same process as 它遵循与json_to_sheet
and accepts an options argument:json_to_sheet
相同的过程,并接受一个options
参数:
header | Object.keys )Object.keys ) |
|
dateNF | FMT 14 | |
cellDates | false | d (default is n )d (默认为n ) |
skipHeader | false | true ,则不在输出中包括标题行 |
nullError | false | #NULL! error cells for null valuestrue ,则针对null 值发出#NULL! 错误单元格 |
origin | ||
UTC | false | true ,则使用UTC方法解释日期 ** |
UTC option is explained in "Dates"UTC选项在“日期”中有说明
origin
is expected to be one of:预计是以下其中之一:
origin | |
---|---|
(cell object) | |
(string) | |
(number >= 0) | |
-1 | |
(default) | A1 A1 开始 |
This example worksheet can be built up in the order 此示例工作表可以按A1:G1, A2:B4, E2:G4, A5:G5
:A1:G1, A2:B4, E2:G4, A5:G5
的顺序生成:
/* Start from an empty worksheet从空工作表开始 */
var ws = XLSX.utils.aoa_to_sheet([[]]);
/* Header order */
var header = ["A", "B", "C", "D", "E", "F", "G"];
/* First row第一行 */
XLSX.utils.sheet_add_json(ws, [
{ A: "S", B: "h", C: "e", D: "e", E: "t", F: "J", G: "S" }
], {header: header, skipHeader: true});
/* Write data starting at A2 */
XLSX.utils.sheet_add_json(ws, [
{ A: 1, B: 2 }, { A: 2, B: 3 }, { A: 3, B: 4 }
], {header: header, skipHeader: true, origin: "A2"});
/* Write data starting at E2 */
XLSX.utils.sheet_add_json(ws, [
{ A: 5, B: 6, C: 7 }, { A: 6, B: 7, C: 8 }, { A: 7, B: 8, C: 9 }
], {header: ["A", "B", "C"], skipHeader: true, origin: { r: 1, c: 4 }});
/* Append row追加行 */
XLSX.utils.sheet_add_json(ws, [
{ A: 4, B: 5, C: 6, D: 7, E: 8, F: 9, G: 0 }
], {header: header, skipHeader: true, origin: -1});
If the 如果header
option is an array, sheet_add_json
and sheet_to_json
will append missing elements.header
选项是一个数组,sheet_add_json
和sheet_to_json
将附加缺失的元素。
This design enables consistent header order across calls:这种设计实现了调用之间一致的头顺序:
Array Output数组输出
- JavaScript
- TypeScript
var arr = XLSX.utils.sheet_to_json(ws, opts);
var aoa = XLSX.utils.sheet_to_json(ws, {header: 1, ...other_opts});
TypeScript types are purely informational. They are not included at run time and do not influence the behavior of the TypeScript类型纯粹是信息性的。它们在运行时不包括在内,也不会影响sheet_to_json
function.sheet_to_json
函数的行为。
sheet_to_json
does not perform field validation!不执行字段验证!
The main type signature treats each row as 主类型签名将每一行视为any
:any
:
const data: any[] = XLSX.utils.sheet_to_json(ws, opts);
The any[][]
overload is designed for use with header: 1
(array of arrays):any[][]
重载是为与header: 1
(数组的数组)一起使用而设计的:
const aoa: any[][] = XLSX.utils.sheet_to_json(ws, { header: 1, ...other_opts });
An interface can be passed as a generic parameter. 接口可以作为泛型参数传递。sheet_to_json
will still return an array of plain objects (the types do not affect runtime behavior):仍将返回一个普通对象数组(这些类型不会影响运行时行为):
interface President {
Name: string;
Index: number;
}
const data: President[] = XLSX.utils.sheet_to_json<President>(ws);
XLSX.utils.sheet_to_json
generates an array of JS objects. The function takes an options argument:生成JS对象的数组。该函数采用一个选项参数:
raw | true | true )或格式化字符串(false ) |
range | ** | |
header | ||
dateNF | FMT 14 | |
defval | null 或undefined 的值 |
|
blankrows | ** | |
skipHidden | false | |
UTC | false | true ,则以UTC表示的日期将是正确的 ** |
raw
only affects cells which have a format code (.z
) field or a formatted text (.w
) field.raw
仅影响具有格式代码(.z
)字段或格式化文本(.w
)字段的单元格。If如果指定了header
is specified, the first row is considered a data row; ifheader
is not specified, the first row is the header row and not considered data.header
,则第一行被视为数据行;如果未指定header
,则第一行为头行,不视为数据。When如果未指定header
is not specified, the conversion will automatically disambiguate header entries by affixing_
and a count starting at1
.header
,则转换将通过附加_
和从1
开始的计数来自动消除头部条目的歧义。For example, if three columns have header例如,如果三列的标题为foo
the output fields arefoo
,foo_1
,foo_2
foo
,则输出字段为foo
、foo_1
、foo_2
当null
values are returned whenraw
is true but are skipped when false.raw
为true
时返回null
值,但当为false
时跳过null
值。If如果未指定defval
is not specified, null and undefined values are skipped normally.defval
,则通常会跳过null
和未定义的值。If specified, all null and undefined points will be filled with如果指定,所有空点和未定义点都将填充defval
defval
When当header
is1
, the default is to generate blank rows.header
为1
时,默认情况是生成空行。blankrows
must be set to必须设置为false
to skip blank rows.false
才能跳过空白行。When当header
is not1
, the default is to skip blank rows.header
不为1
时,默认情况是跳过空白行。blankrows
must be true to generate blank rows必须为true
才能生成空行UTC option is explained in "Dates"UTC选项在“日期”中有说明
range
is expected to be one of:预计是以下其中之一:
range | |
---|---|
(number) | |
(string) | |
(default) | ws['!ref'] )ws['!ref'] ) |
header
is expected to be one of:预计是以下其中之一:
header | |
---|---|
1 | |
"A" | |
array of strings | |
(default) |
If header is not如果1
, the row object will contain the non-enumerable property__rowNum__
that represents the row of the sheet corresponding to the entry.header
不为1
,则行对象将包含不可枚举的属性__rowNum__
,该属性表示与条目对应的工作表的行。If header is an array, the keys will not be disambiguated. This can lead to unexpected results if the array values are not unique!如果头是一个数组,键将不会被消除歧义。如果数组值不是唯一的,这可能会导致意外的结果!
For the example worksheet:对于示例工作表: