Skip to content

Commit

Permalink
简化 tsconfig.json 和 types 类型,修改 setCellStyle 逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
qiang.bai committed Dec 31, 2024
1 parent 2432241 commit d9ce3a9
Show file tree
Hide file tree
Showing 16 changed files with 249 additions and 275 deletions.
1 change: 0 additions & 1 deletion example-vite2/src/example/Basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export default function App() {
const onCountBtnClick = () => {
const sheets = [
{
name: 'sheet',
columns: columns,
dataSource
}
Expand Down
5 changes: 4 additions & 1 deletion example-vite2/src/example/CustomStyle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,17 @@ export default function App() {
bold: false,
backgroundColor: 'ff0000',
textAlign: 'left'
},
body: {
textAlign: 'center'
}
}
}
]

style({
sheets,
filename: 'output.xlsx'
filename: 'custom style.xlsx'
})
}

Expand Down
3 changes: 1 addition & 2 deletions example-vite2/src/example/HeaderGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,14 @@ export default function App() {
const onCountBtnClick = () => {
const sheets: ISheet[] = [
{
name: 'sheet',
columns,
dataSource
}
]

style({
sheets,
filename: 'moreSheet.xlsx',
filename: 'header group.xlsx',
})
}

Expand Down
2 changes: 1 addition & 1 deletion example-vite2/src/example/HiddenHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export default function App() {

nostyle({
sheets,
filename: 'output.xlsx',
filename: 'hidden header.xlsx',
hiddenHeader: true,
})
}
Expand Down
4 changes: 1 addition & 3 deletions example-vite2/src/example/MoreSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,18 @@ export default function App() {
const onCountBtnClick = () => {
const sheets = [
{
name: 'sheet1',
columns: columns,
dataSource
},
{
name: 'sheet2',
columns: columns,
dataSource
}
]

style({
sheets,
filename: 'moreSheet.xlsx',
filename: 'more sheet.xlsx',
})
}

Expand Down
3 changes: 1 addition & 2 deletions example-vite2/src/example/NotExportColumn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@ export default function App() {
const onCountBtnClick = () => {
const sheets = [
{
name: 'sheet',
columns: columns,
dataSource,
}
]

style({
sheets,
filename: 'basic.xlsx',
filename: 'not export column.xlsx',
})
}

Expand Down
3 changes: 1 addition & 2 deletions example-vite2/src/example/Span.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,14 @@ export default function App() {
const onCountBtnClick = () => {
const sheets = [
{
name: '普通表格',
columns: columns,
dataSource,
}
]

style({
sheets,
filename: 'output.xlsx'
filename: '单元格合并.xlsx'
})
}

Expand Down
4 changes: 2 additions & 2 deletions example-vite2/src/example/UseWorker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const columns = [
// in Worker 不能传函数
render: `
(val, row, i) => {
console.log(val, row, i)
// console.log(val, row, i)
return 'formatted Name: ' + val
}
`
Expand Down Expand Up @@ -53,7 +53,7 @@ const columns = [
},
]

const filename = 'basic.xlsx'
const filename = 'work in Worker.xlsx'

export default function App() {
const workerRef = useRef<Worker>()
Expand Down
4 changes: 2 additions & 2 deletions src/style.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

import { write as styleWriteFn } from 'xlsx-style-vite'
import getCellStyle from './utils/cellStyle'
import setCellStyle from './utils/setCellStyle'
import download from './utils'
import { IExcel } from './type'

export default function style(excelProps: IExcel) {
return download(
Object.assign(excelProps, {
write: styleWriteFn,
getCellStyle,
setCellStyle
})
)
}
165 changes: 90 additions & 75 deletions src/type.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
/**
* 主 Excel 配置接口
*/
export interface IExcel {
sheets: ISheet[]; // 表格中的工作表列表
filename?: string; // 导出的文件名
hiddenHeader?: boolean; // 是否隐藏所有工作表的表头
worker?: boolean; // 是否启用 Worker 多线程
}

/**
* 工作表配置接口
*/
export interface ISheet {
name?: string; // 工作表名称
columns: IColumn[]; // 列配置
dataSource: Record<string, any>[]; // 数据源
hiddenHeader?: boolean; // 是否隐藏当前表的表头(优先级高于全局配置)
style?: ISheetStyle; // 样式配置
}

/**
* 样式配置接口(表头和正文)
*/
export interface ISheetStyle {
header?: IStyleConfig; // 表头样式配置
body?: IStyleConfig; // 正文样式配置
}

/**
* 列配置接口
*/
export type IColumn = {
title: string;
width?: number;
notExport?: boolean;
children?: IColumn[];
render?: (value: any, row: Record<string, any>, rowIndex: number) => any | string;
onCell?: Function | string;
colSpan?: number;
title: string; // 列标题
width?: number; // 列宽度
notExport?: boolean; // 是否不导出该列
children?: IColumn[]; // 子列(支持分组表头)
render?: (value: any, row: Record<string, any>, rowIndex: number) => any | string; // 自定义单元格渲染
onCell?: Function | string; // 单元格事件
colSpan?: number; // 合并列数
[key: string]: any; // 额外字段支持

[key: string]: any;
// dataIndex?: string; 表头分组 父 column 没 dataIndex
// rootHeight?: number;
// leafCount?: number;
Expand All @@ -16,85 +48,68 @@ export type IColumn = {
// merge?: IMerge;
}

export interface ICellStyle {
fontSize?: number;
color?: string;
bold?: boolean;
background?: string;
backgroundColor?: string;
textAlign?: 'left' | 'center' | 'right';
borderColor?: string;
}

export interface ISheet {
name?: string;
columns: IColumn[];
dataSource: Record<string, any>[];
hiddenHeader?: boolean; // 导出表格时,是否隐藏表头。默认显示表头
style?: {
header?: ICellStyle;
body?: ICellStyle;
}
}

export interface IExcel {
sheets: ISheet[];
filename?: string;
hiddenHeader?: boolean; // 导出表格时,是否隐藏所有 sheet 表头
worker?: boolean;
}

// cell 左上角
export type Point = {
r: number;
c: number;
/**
* 单元格左上角位置坐标
*/
type Point = {
r: number; // 行索引
c: number; // 列索引
}

/**
* 单元格合并信息
*/
export type IMerge = {
title?: string;
s: Point;
e: Point;
title?: string; // 合并后的标题
s: Point; // 合并起始位置
e: Point; // 合并结束位置
}

export type getCellStyleType = (
{
isTitle,
style
}: {
isTitle: boolean,
style?: ISheet['style']
}
) => styleType

/**
* 用户配置的样式接口
*/
export interface IStyleConfig {
fontSize?: number; // 字体大小
color?: string; // 字体颜色
bold?: boolean; // 是否加粗
background?: string; // 背景色(优先级低于 backgroundColor)
backgroundColor?: string; // 背景颜色
textAlign?: 'left' | 'center' | 'right'; // 文本对齐方式
borderColor?: string; // 边框颜色
}

export type Side = 'top' | 'bottom' | 'left' | 'right'
/**
* 边框的方向
*/
export type Side = 'top' | 'bottom' | 'left' | 'right';

export type borderType = Record<
Side,
{
style: string,
/**
* 单个单元格样式
*/
export type ICellStyle = {
font?: {
sz?: number; // 字体大小
color?: {
rgb?: string
rgb: string; // 字体颜色(RGB 格式)
}
bold?: boolean; // 是否加粗
}
>

export type styleType = {
font?: {
sz?: number,
color: {
rgb?: string
},
bold?: Boolean
},
alignment: {
horizontal?: ICellStyle['textAlign'],
wrapText: Boolean
},
border: borderType,
fill?: {
horizontal: IStyleConfig['textAlign']; // 水平对齐方式
wrapText: boolean; // 是否自动换行
}
border: Record<
Side,
{
style: string; // 边框样式
color?: {
rgb?: string; // 边框颜色(RGB 格式)
}
}
>;
fill: {
fgColor: {
rgb: string
rgb?: string; // 填充颜色(RGB 格式)
}
}
}
Loading

0 comments on commit d9ce3a9

Please sign in to comment.