Sheets
添加对电子表格的支持以存储数据。
配置参数
- extensions string[]
Extensions processed by this plugin
Default:[ ".xlsx", ".numbers", ".csv" ]
- sheets first auto
Return the first sheet only or all sheets if the document have more
Default:"auto"
- options object
Options passed to Sheetjs
- type base64 binary buffer file array string
Input data encoding
- codepage number
Default codepage for legacy files
This requires encoding support to be loaded. It is automatically loaded in
xlsx.full.min.js
and in CommonJS / Extendscript, but an extra step is required in React / Angular / Webpack ESM deployments.Check the relevant guide https://docs.sheetjs.com/docs/getting-started/
- cellFormula boolean
Save formulae to the .f field
Default:true
- cellHTML boolean
Parse rich text and save HTML to the .h field
Default:true
- cellNF boolean
Save number format string to the .z field
Default:false
- cellText boolean
Generate formatted text to the .w field
Default:true
- dateNF string
Override default date format (code 14)
- FS string
Field Separator ("Delimiter" override)
- sheetRows number
If >0, read the first sheetRows rows
Default:0
- bookDeps boolean
If true, parse calculation chains
Default:false
- bookFiles boolean
If true, add raw files to book object
Default:false
- bookProps boolean
If true, only parse enough to get book metadata
Default:false
- bookSheets boolean
If true, only parse enough to get the sheet names
Default:false
- sheets number string object
If specified, only parse the specified sheets or sheet names
- raw boolean
If true, plaintext parsing will not parse values
- nodim boolean
If true, ignore "dimensions" records and guess range using every cell
- xlfn boolean
If true, preserve _xlfn. prefixes in formula function names
- sheet string
For single-sheet formats (including CSV), override the worksheet name
Default:"Sheet1"
- PRN boolean
描述
此插件使用 SheetJS 读取任何电子表格文档作为 数据文件,因此你可以使用这些数据来渲染你的页面。
安装
在你的 _config.ts
文件中导入此插件以使用它:
import lume from "lume/mod.ts";
import sheets from "lume/plugins/sheets.ts";
const site = lume();
site.use(sheets());
export default site;
格式
默认情况下,它加载 .xlsx
、.numbers
和 .csv
文件。 你可以使用 SheetJS 支持的任何扩展名。 有关更多信息,请参阅文件格式文档。
import lume from "lume/mod.ts";
import sheets from "lume/plugins/sheets.ts";
const site = lume();
site.use(sheets({
extensions: [".ods", "rtf", "xls"],
}));
export default site;
表格模式
此插件可以在两种表格模式下工作:first
和 auto
。
First
在 first
表格模式下,即使文档包含多个表格,也只返回在文档中找到的第一个表格。 例如,存储在 _data/people.xlsx
中的文件包含两个表格,则只会返回第一个表格:
<table>
<tr>
{{ for key, column of people[0] }}
<th>{{ key }}</th>
{{ /for }}
</tr>
{{ for row of people }}
<tr>
{{ for key, column of row }}
<td>{{ column }}</td>
{{ /for }}
</tr>
{{ /for }}
</table>
Auto
如果 sheets
值设置为 auto
,它将返回文档中找到的所有表格,并且你必须使用表格名称来访问其内容。
例如,如果文件 _data/people.xlsx
包含名为 "Women" 和 "Men" 的表格,你可以通过以下方式访问数据:
<h1>Women</h1>
<ul>
{{ for person of people["Women"] }}
<li>
{{ person.name }} - {{ person.surname }}
</li>
{{ /for }}
</ul>
<h1>Men</h1>
<ul>
{{ for person of people["Men"] }}
<li>
{{ person.name }} - {{ person.surname }}
</li>
{{ /for }}
</ul>
如果电子表格**仅包含一个表格,**则你无需通过名称访问数据。 在这种情况下,它的工作方式与 "first" 模式完全相同。
默认模式是 auto
,要将其更改为 first
:
import lume from "lume/mod.ts";
import sheets from "lume/plugins/sheets.ts";
const site = lume();
site.use(sheets({
sheets: "first", // 返回在每个文档中找到的第一个表格
}));
export default site;