woocommerce/packages/js/csv-export/zh-cn/README.md
2026-04-15 10:18:01 +08:00

60 lines
No EOL
1.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# CSV 导出
一组用于将数据转换为 CSV 值,并启用浏览器下载 CSV 数据的功能。
## 安装
安装模块
```bash
pnpm install @woocommerce/csv-export --save
```
## 用法
```js
onClick = () => {
// 基于标题和可选查询创建文件名。将返回带时间戳的名称,
// 例如revenue-2018-11-01-interval-month.csv
const name = generateCSVFileName( 'revenue', { interval: 'month' } );
// 创建 CSV 数据字符串,`headers` 是行标题数组,置于文件顶部。
// `rows` 是一个二维数组。每个数组是文件中的一行,由换行符分隔。
// 第二级数组是每行中的数据点。
const data = generateCSVDataFromTable( headers, rows );
// 触发浏览器 UI 保存文件,第一个参数为文件名,第二个参数为文件内容。
downloadCSVFile( name, data );
}
```
### generateCSVDataFromTable(headers, rows) ⇒ `字符串`
从表格内容生成 CSV 字符串
**返回**: `字符串` - CSV 格式的表格内容
| 参数 | 类型 | 描述 |
| --- | --- | --- |
| headers | `数组.<对象>` | 包含表格标题信息的对象 |
| rows | `数组.数组.<对象>` | 包含表格行信息的对象 |
### generateCSVFileName([name], [params]) ⇒ `字符串`
基于提供的名称、当前日期和提供的参数生成 CSV 文件的文件名,所有部分用连字符连接。
**返回**: `字符串` - 格式化后的文件名
| 参数 | 类型 | 默认值 | 描述 |
| --- | --- | --- | --- |
| [name] | `字符串` | `''` | 文件名 |
| [params] | `对象` | `{}` | 要附加到文件名的键值对对象 |
### downloadCSVFile(fileName, content)
下载具有给定文件名和内容的 CSV 文件
| 参数 | 类型 | 描述 |
| --- | --- | --- |
| fileName | `字符串` | 要下载的文件名 |
| content | `字符串` | 要下载的文件内容 |