修改excel导出文件格式

This commit is contained in:
wenjiarui 2023-01-30 12:56:38 +08:00
parent 4348b641b0
commit 15d4a11505
5 changed files with 80 additions and 14 deletions

5
lib/AspectDict.d.ts vendored
View File

@ -1,4 +1,3 @@
/// <reference types="node" />
import { EntityDict, OperateOption, SelectOption, OperationResult, AggregationResult } from "oak-domain/lib/types";
import { AmapInstance } from "oak-external-sdk";
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
@ -47,8 +46,8 @@ export declare type CommonAspectDict<ED extends EntityDict & BaseEntityDict, Cxt
entity: T;
id: string;
filter?: ED[T]['Selection']['filter'];
}, context: Cxt) => Promise<NodeJS.ReadableStream>;
}, context: Cxt) => Promise<ArrayBuffer>;
getImportationTemplate: (params: {
id: string;
}, context: Cxt) => Promise<NodeJS.ReadableStream>;
}, context: Cxt) => Promise<ArrayBuffer>;
};

5
lib/port.d.ts vendored
View File

@ -1,4 +1,3 @@
/// <reference types="node" />
import { EntityDict } from 'oak-domain/lib/types/Entity';
import { Importation, Exportation } from 'oak-domain/lib/types/Port';
import { AsyncContext } from 'oak-domain/lib/store/AsyncRowStore';
@ -9,7 +8,7 @@ export declare function exportEntity<ED extends EntityDict, T extends keyof ED,
entity: T;
id: string;
filter?: ED[T]['Selection']['filter'];
}, context: Cxt): Promise<NodeJS.ReadableStream>;
}, context: Cxt): Promise<ArrayBuffer>;
export declare function getImportationTemplate<ED extends EntityDict, Cxt extends AsyncContext<ED>>(params: {
id: string;
}, context: Cxt): Promise<NodeJS.ReadableStream>;
}, context: Cxt): Promise<ArrayBuffer>;

View File

@ -45,6 +45,9 @@ async function importEntity(params, context) {
const id = params.get('id');
const option = JSON.parse(params.get('option'));
const importation = getImportation(id);
if (!importation) {
throw new Error('尚不支持此数据的导入');
}
const { fn } = importation;
const arrayBuffer = await file.arrayBuffer();
const workbook = (0, xlsx_1.read)(arrayBuffer);
@ -72,10 +75,39 @@ async function importEntity(params, context) {
}
exports.importEntity = importEntity;
async function exportEntity(params, context) {
throw new Error('export not implement yet');
const id = params.id;
const filter = params.filter;
const exportation = getExportation(id);
if (!exportation) {
throw new Error('尚不支持此数据的导出');
}
const { projection, headers, fn, entity } = exportation;
const dataList = await context.select(entity, {
filter,
data: projection,
}, {});
const fittedDatalist = [];
for (const data of dataList) {
fittedDatalist.push(fn(data));
}
const exportSheet = xlsx_1.utils.json_to_sheet(fittedDatalist, { header: headers });
const exportBook = xlsx_1.utils.book_new();
xlsx_1.utils.book_append_sheet(exportBook, exportSheet);
return await (0, xlsx_1.write)(exportBook, { type: 'buffer' });
// throw new Error('export not implement yet');
}
exports.exportEntity = exportEntity;
async function getImportationTemplate(params, context) {
throw new Error('not implement yet');
const id = params.id;
const importation = getImportation(id);
const { headers } = importation;
if (!importation) {
throw new Error('未找到对应的模板');
}
const exportSheet = xlsx_1.utils.json_to_sheet([], { header: headers });
const exportBook = xlsx_1.utils.book_new();
xlsx_1.utils.book_append_sheet(exportBook, exportSheet);
return await (0, xlsx_1.write)(exportBook, { type: 'buffer' });
// throw new Error('not implement yet');
}
exports.getImportationTemplate = getImportationTemplate;

View File

@ -66,6 +66,6 @@ export type CommonAspectDict<ED extends EntityDict & BaseEntityDict, Cxt extends
entity: T;
id: string;
filter?: ED[T]['Selection']['filter'];
}, context: Cxt) => Promise<NodeJS.ReadableStream>;
getImportationTemplate: (params: { id: string }, context: Cxt) => Promise<NodeJS.ReadableStream>;
}, context: Cxt) => Promise<ArrayBuffer>;
getImportationTemplate: (params: { id: string }, context: Cxt) => Promise<ArrayBuffer>;
};

View File

@ -53,6 +53,9 @@ export async function importEntity<
const id = params.get('id') as string;
const option = JSON.parse(params.get('option') as string);
const importation = getImportation<ED, keyof ED>(id);
if (!importation) {
throw new Error('尚不支持此数据的导入');
}
const { fn } = importation;
const arrayBuffer = await file.arrayBuffer();
const workbook = read(arrayBuffer);
@ -92,13 +95,46 @@ export async function exportEntity<
entity: T;
id: string;
filter?: ED[T]['Selection']['filter'];
}, context: Cxt): Promise<NodeJS.ReadableStream> {
throw new Error('export not implement yet');
}, context: Cxt): Promise<ArrayBuffer> {
const id = params.id;
const filter = params.filter;
const exportation = getExportation<ED, keyof ED>(id);
if (!exportation) {
throw new Error('尚不支持此数据的导出');
}
const { projection, headers, fn, entity } = exportation;
const dataList = await context.select(
entity,
{
filter,
data: projection,
},
{}
);
const fittedDatalist = []
for (const data of dataList) {
fittedDatalist.push(fn(data as ED[keyof ED]['Schema']));
}
const exportSheet = utils.json_to_sheet(fittedDatalist, { header: headers });
const exportBook = utils.book_new();
utils.book_append_sheet(exportBook, exportSheet);
return await write(exportBook, { type: 'buffer' });
// throw new Error('export not implement yet');
}
export async function getImportationTemplate<
ED extends EntityDict,
Cxt extends AsyncContext<ED>
>(params: { id: string }, context: Cxt): Promise<NodeJS.ReadableStream> {
throw new Error('not implement yet');
>(params: { id: string }, context: Cxt): Promise<ArrayBuffer> {
const id = params.id;
const importation = getImportation<ED, keyof ED>(id);
const { headers } = importation;
if (!importation) {
throw new Error('未找到对应的模板');
}
const exportSheet = utils.json_to_sheet([], { header: headers });
const exportBook = utils.book_new();
utils.book_append_sheet(exportBook, exportSheet);
return await write(exportBook, { type: 'buffer' });
// throw new Error('not implement yet');
}