mirror of
https://gitee.com/x-extends/vxe-table-plugin-export-xlsx.git
synced 2026-01-21 05:21:33 +08:00
重构 4.0 next 版本
This commit is contained in:
21
gulpfile.js
21
gulpfile.js
@@ -7,19 +7,14 @@ const replace = require('gulp-replace')
|
||||
const sourcemaps = require('gulp-sourcemaps')
|
||||
const ts = require('gulp-typescript')
|
||||
const pack = require('./package.json')
|
||||
const tsconfig = require('./tsconfig.json')
|
||||
|
||||
const exportModuleName = 'VXETablePluginExportXLSX'
|
||||
|
||||
gulp.task('build_commonjs', function () {
|
||||
return gulp.src(['index.d.ts', 'depend.ts', 'index.ts'])
|
||||
.pipe(sourcemaps.init())
|
||||
.pipe(ts({
|
||||
strict: true,
|
||||
moduleResolution: 'node',
|
||||
noImplicitAny: true,
|
||||
target: 'es6',
|
||||
lib: ['dom', 'es6']
|
||||
}))
|
||||
.pipe(ts(tsconfig.compilerOptions))
|
||||
.pipe(babel({
|
||||
presets: ['@babel/env']
|
||||
}))
|
||||
@@ -33,14 +28,8 @@ gulp.task('build_commonjs', function () {
|
||||
|
||||
gulp.task('build_umd', function () {
|
||||
return gulp.src(['index.d.ts', 'depend.ts', 'index.ts'])
|
||||
.pipe(ts({
|
||||
strict: true,
|
||||
moduleResolution: 'node',
|
||||
noImplicitAny: true,
|
||||
target: 'es6',
|
||||
lib: ['dom', 'es6']
|
||||
}))
|
||||
.pipe(replace(`import XEUtils from 'xe-utils/ctor';`, `import XEUtils from 'xe-utils';`))
|
||||
.pipe(ts(tsconfig.compilerOptions))
|
||||
.pipe(replace('import XEUtils from \'xe-utils/ctor\';', 'import XEUtils from \'xe-utils\';'))
|
||||
.pipe(babel({
|
||||
moduleId: pack.name,
|
||||
presets: [
|
||||
@@ -51,7 +40,7 @@ gulp.task('build_umd', function () {
|
||||
globals: {
|
||||
[pack.name]: exportModuleName,
|
||||
'xe-utils': 'XEUtils',
|
||||
'xlsx': 'XLSX'
|
||||
xlsx: 'XLSX'
|
||||
},
|
||||
exactGlobals: true
|
||||
}]
|
||||
|
||||
80
index.ts
80
index.ts
@@ -1,22 +1,33 @@
|
||||
/* eslint-disable no-unused-vars */
|
||||
import XEUtils from 'xe-utils/ctor'
|
||||
import {
|
||||
VXETable,
|
||||
Table,
|
||||
InterceptorExportParams,
|
||||
InterceptorImportParams,
|
||||
ColumnConfig,
|
||||
ExportOptons
|
||||
VXETableByVueProperty,
|
||||
VXETableInstance,
|
||||
VxeTableConstructor,
|
||||
VxeTablePropTypes,
|
||||
VxeTableDefines,
|
||||
VxeGlobalInterceptorHandles
|
||||
} from 'vxe-table/lib/vxe-table'
|
||||
import XLSX from 'xlsx'
|
||||
|
||||
declare module 'vxe-table/lib/vxe-table' {
|
||||
namespace VxeTableDefines {
|
||||
interface ColumnInfo {
|
||||
_row: any;
|
||||
_colSpan: number;
|
||||
_rowSpan: number;
|
||||
childNodes: VxeTableDefines.ColumnInfo[];
|
||||
}
|
||||
}
|
||||
}
|
||||
/* eslint-enable no-unused-vars */
|
||||
|
||||
function getFooterCellValue ($table: Table, opts: ExportOptons, rows: any[], column: ColumnConfig) {
|
||||
const cellValue = rows[$table.$getColumnIndex(column)]
|
||||
function getFooterCellValue ($table: VxeTableConstructor, opts: VxeTablePropTypes.ExportConfig, rows: any[], column: VxeTableDefines.ColumnInfo) {
|
||||
const cellValue = rows[$table.getVTColumnIndex(column)]
|
||||
return cellValue
|
||||
}
|
||||
|
||||
function getFooterData (opts: ExportOptons, footerData: any[][]) {
|
||||
function getFooterData (opts: VxeTablePropTypes.ExportConfig, footerData: any[][]) {
|
||||
const { footerFilterMethod } = opts
|
||||
return footerFilterMethod ? footerData.filter((items, index) => footerFilterMethod({ items, $rowIndex: index })) : footerData
|
||||
}
|
||||
@@ -28,7 +39,7 @@ function toBuffer (wbout: any) {
|
||||
return buf
|
||||
}
|
||||
|
||||
function getCellLabel (column: ColumnConfig, cellValue: any) {
|
||||
function getCellLabel (column: VxeTableDefines.ColumnInfo, cellValue: any) {
|
||||
if (cellValue) {
|
||||
switch (column.cellType) {
|
||||
case 'string':
|
||||
@@ -48,16 +59,7 @@ function getCellLabel (column: ColumnConfig, cellValue: any) {
|
||||
return cellValue
|
||||
}
|
||||
|
||||
declare module 'vxe-table/lib/vxe-table' {
|
||||
interface ColumnInfo {
|
||||
_row: any;
|
||||
_colSpan: number;
|
||||
_rowSpan: number;
|
||||
childNodes: ColumnConfig[];
|
||||
}
|
||||
}
|
||||
|
||||
function getValidColumn (column: ColumnConfig): ColumnConfig {
|
||||
function getValidColumn (column: VxeTableDefines.ColumnInfo): VxeTableDefines.ColumnInfo {
|
||||
const { childNodes } = column
|
||||
const isColGroup = childNodes && childNodes.length
|
||||
if (isColGroup) {
|
||||
@@ -66,11 +68,11 @@ function getValidColumn (column: ColumnConfig): ColumnConfig {
|
||||
return column
|
||||
}
|
||||
|
||||
function exportXLSX (params: InterceptorExportParams) {
|
||||
function exportXLSX (params: VxeGlobalInterceptorHandles.InterceptorExportParams) {
|
||||
const msgKey = 'xlsx'
|
||||
const { $table, options, columns, colgroups, datas } = params
|
||||
const { $vxe } = $table
|
||||
const { modal, t } = $vxe
|
||||
const { instance } = $table
|
||||
const { modal, t } = instance.appContext.config.globalProperties.$vxe as VXETableByVueProperty
|
||||
const { message, sheetName, isHeader, isFooter, isMerge, isColgroup, original } = options
|
||||
const showMsg = message !== false
|
||||
const mergeCells = $table.getMergeCells()
|
||||
@@ -89,7 +91,7 @@ function exportXLSX (params: InterceptorExportParams) {
|
||||
})
|
||||
if (isColgroup && !original && colgroups) {
|
||||
colgroups.forEach((cols, rIndex) => {
|
||||
let groupHead: any = {}
|
||||
const groupHead: any = {}
|
||||
columns.forEach((column) => {
|
||||
groupHead[column.id] = null
|
||||
})
|
||||
@@ -114,9 +116,9 @@ function exportXLSX (params: InterceptorExportParams) {
|
||||
// 处理合并
|
||||
if (isMerge && !original) {
|
||||
mergeCells.forEach(mergeItem => {
|
||||
let { row: mergeRowIndex, rowspan: mergeRowspan, col: mergeColIndex, colspan: mergeColspan } = mergeItem
|
||||
const { row: mergeRowIndex, rowspan: mergeRowspan, col: mergeColIndex, colspan: mergeColspan } = mergeItem
|
||||
for (let rIndex = 0; rIndex < datas.length; rIndex++) {
|
||||
let rowIndex = $table._getRowIndex(datas[rIndex]._row)
|
||||
let rowIndex = $table.getVTRowIndex(datas[rIndex]._row)
|
||||
if (rowIndex === mergeRowIndex) {
|
||||
if (isHeader && colgroups) {
|
||||
rowIndex = rIndex + colgroups.length
|
||||
@@ -174,10 +176,10 @@ function exportXLSX (params: InterceptorExportParams) {
|
||||
}
|
||||
}
|
||||
|
||||
function downloadFile (params: InterceptorExportParams, blob: Blob, options: ExportOptons) {
|
||||
function downloadFile (params: VxeGlobalInterceptorHandles.InterceptorExportParams, blob: Blob, options: VxeTablePropTypes.ExportConfig) {
|
||||
const { $table } = params
|
||||
const { $vxe } = $table
|
||||
const { modal, t } = $vxe
|
||||
const { instance } = $table
|
||||
const { modal, t } = instance.appContext.config.globalProperties.$vxe as VXETableByVueProperty
|
||||
const { message, filename, type } = options
|
||||
const showMsg = message !== false
|
||||
if (window.Blob) {
|
||||
@@ -203,17 +205,11 @@ function checkImportData (tableFields: string[], fields: string[]) {
|
||||
return fields.some(field => tableFields.indexOf(field) > -1)
|
||||
}
|
||||
|
||||
declare module 'vxe-table/lib/vxe-table' {
|
||||
interface Table {
|
||||
_importResolve?: Function | null;
|
||||
_importReject?: Function | null;
|
||||
}
|
||||
}
|
||||
|
||||
function importXLSX (params: InterceptorImportParams) {
|
||||
function importXLSX (params: VxeGlobalInterceptorHandles.InterceptorImportParams) {
|
||||
const { $table, columns, options, file } = params
|
||||
const { $vxe, _importResolve, _importReject } = $table
|
||||
const { modal, t } = $vxe
|
||||
const { instance, internalData } = $table
|
||||
const { _importResolve, _importReject } = internalData
|
||||
const { modal, t } = instance.appContext.config.globalProperties.$vxe as VXETableByVueProperty
|
||||
const showMsg = options.message !== false
|
||||
const fileReader = new FileReader()
|
||||
fileReader.onload = (e: any) => {
|
||||
@@ -266,14 +262,14 @@ function importXLSX (params: InterceptorImportParams) {
|
||||
fileReader.readAsBinaryString(file)
|
||||
}
|
||||
|
||||
function handleImportEvent (params: InterceptorImportParams) {
|
||||
function handleImportEvent (params: VxeGlobalInterceptorHandles.InterceptorImportParams) {
|
||||
if (params.options.type === 'xlsx') {
|
||||
importXLSX(params)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
function handleExportEvent (params: InterceptorExportParams) {
|
||||
function handleExportEvent (params: VxeGlobalInterceptorHandles.InterceptorExportParams) {
|
||||
if (params.options.type === 'xlsx') {
|
||||
exportXLSX(params)
|
||||
return false
|
||||
@@ -284,7 +280,7 @@ function handleExportEvent (params: InterceptorExportParams) {
|
||||
* 基于 vxe-table 表格的增强插件,支持导出 xlsx 格式
|
||||
*/
|
||||
export const VXETablePluginExportXLSX = {
|
||||
install (vxetable: typeof VXETable) {
|
||||
install (vxetable: VXETableInstance) {
|
||||
const { interceptor } = vxetable
|
||||
vxetable.setup({
|
||||
export: {
|
||||
|
||||
48
package.json
48
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "vxe-table-plugin-export-xlsx",
|
||||
"version": "2.0.5",
|
||||
"version": "3.0.0-alpha.0",
|
||||
"description": "基于 vxe-table 的表格插件,支持导出 xlsx 格式",
|
||||
"scripts": {
|
||||
"lib": "gulp build"
|
||||
@@ -16,22 +16,22 @@
|
||||
"style": "dist/style.css",
|
||||
"typings": "index.ts",
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.4.4",
|
||||
"@babel/plugin-transform-runtime": "^7.4.4",
|
||||
"@babel/preset-env": "^7.4.4",
|
||||
"@babel/runtime": "^7.4.4",
|
||||
"@typescript-eslint/eslint-plugin": "^2.3.1",
|
||||
"@typescript-eslint/parser": "^2.3.1",
|
||||
"del": "^5.1.0",
|
||||
"eslint": "^5.15.1",
|
||||
"eslint-config-prettier": "^6.3.0",
|
||||
"eslint-config-standard": "^12.0.0",
|
||||
"@babel/core": "^7.12.3",
|
||||
"@babel/plugin-transform-runtime": "^7.12.1",
|
||||
"@babel/preset-env": "^7.12.1",
|
||||
"@babel/runtime": "^7.12.5",
|
||||
"@typescript-eslint/eslint-plugin": "^4.6.1",
|
||||
"@typescript-eslint/parser": "^4.6.1",
|
||||
"del": "^6.0.0",
|
||||
"eslint": "^7.13.0",
|
||||
"eslint-config-prettier": "^6.15.0",
|
||||
"eslint-config-standard": "^16.0.1",
|
||||
"eslint-friendly-formatter": "^4.0.1",
|
||||
"eslint-plugin-import": "^2.16.0",
|
||||
"eslint-plugin-node": "^8.0.1",
|
||||
"eslint-plugin-prettier": "^3.1.1",
|
||||
"eslint-plugin-promise": "^4.0.1",
|
||||
"eslint-plugin-standard": "^4.0.0",
|
||||
"eslint-plugin-import": "^2.22.1",
|
||||
"eslint-plugin-node": "^11.1.0",
|
||||
"eslint-plugin-prettier": "^3.1.4",
|
||||
"eslint-plugin-promise": "^4.2.1",
|
||||
"eslint-plugin-standard": "^4.0.2",
|
||||
"eslint-plugin-typescript": "^0.14.0",
|
||||
"gulp": "^4.0.2",
|
||||
"gulp-autoprefixer": "^7.0.1",
|
||||
@@ -44,16 +44,16 @@
|
||||
"gulp-sourcemaps": "^2.6.5",
|
||||
"gulp-typescript": "^5.0.1",
|
||||
"gulp-uglify": "^3.0.2",
|
||||
"markdown-doctest": "^0.9.1",
|
||||
"prettier": "^1.18.2",
|
||||
"typescript": "^3.9.3",
|
||||
"vue": "^2.6.12",
|
||||
"vxe-table": "^2.9.25",
|
||||
"xe-utils": "^2.7.13",
|
||||
"xlsx": "^0.16.7"
|
||||
"markdown-doctest": "^1.1.0",
|
||||
"prettier": "^2.1.2",
|
||||
"typescript": "^4.0.5",
|
||||
"vue": "^3.0.2",
|
||||
"vxe-table": "^4.0.0-alpha.1",
|
||||
"xe-utils": "^3.0.1",
|
||||
"xlsx": "^0.16.8"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"vxe-table": ">= 2.9.25",
|
||||
"vxe-table": ">= 4",
|
||||
"xlsx": ">= 0.16.0"
|
||||
},
|
||||
"repository": {
|
||||
|
||||
@@ -8,10 +8,11 @@
|
||||
"strict": true,
|
||||
"moduleResolution": "node",
|
||||
"noImplicitAny": true,
|
||||
"target": "es6",
|
||||
"target": "esnext",
|
||||
"lib": [
|
||||
"esnext",
|
||||
"dom",
|
||||
"es6"
|
||||
"dom.iterable"
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user