Skip to content

Commit

Permalink
暂存
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Jun 28, 2024
1 parent 3b88e8d commit 84f5a74
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 85 deletions.
2 changes: 1 addition & 1 deletion src/editorTable/EXCEL/excel2Json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { chineseTypeNameToEnglishTypeName } from '../../constants';
import { excelConverter } from './excelConverter';


export class excel2Json extends excelConverter{
export class excel2Json extends excelConverter {
constructor(rule: ImportRule, excelPath: vscode.Uri, targetPath: vscode.Uri) {
super(rule, excelPath, targetPath);
}
Expand Down
4 changes: 2 additions & 2 deletions src/editorTable/EXCEL/excelConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class excelConverter{
}
}
if(tmpID){
let exporter = excelExporter.getInstance();
let exporter = new excelExporter();
if(exporter){
output = await exporter.getTmpJsDict(this.rule.editorTableType, tmpID);
}
Expand Down Expand Up @@ -260,4 +260,4 @@ export class excelConverter{
}
}
}
}
}
82 changes: 26 additions & 56 deletions src/editorTable/EXCEL/excelExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as vscode from 'vscode';
import * as path from 'path';

import { EditorTableName, ObjectTypeNameCN, ObjectTypeNameEN, chineseTypeNameToEnglishTypeName, csvTypeToPath, englishPathToChinese } from '../../constants';
import { tableExpoter } from './tableExpoter';
import { excel2Json } from './excel2Json';
import { ImportRule } from './importRule';
import { env } from '../../env';
Expand All @@ -17,81 +16,52 @@ class editorTableDir{
}
}

export class excelExporter extends tableExpoter{
private static _instance: excelExporter;
export class excelExporter {
private excelPath: vscode.Uri;
private rulePath: vscode.Uri;
private editorTablePath: vscode.Uri;
private importRules: ImportRule[];
private editorTableDatas: {[key: string] : editorTableDir};
private editorTableDatas: {[key: string] : editorTableDir} = {};

constructor(excelUri: vscode.Uri, ruleUri: vscode.Uri, destinationUri: vscode.Uri){
super();
this.excelPath = excelUri;
this.rulePath = ruleUri;
this.editorTablePath = destinationUri;
this.importRules = [];
this.editorTableDatas = {};
}

public static getInstance(){
if(!excelExporter._instance){
if (!env.excelUri || !env.editorTableUri || !env.ruleUri) {
vscode.window.showErrorMessage("未找到excel表格的位置,请检查是否初始化Y3开发环境");
return null;
}
excelExporter._instance = new excelExporter(env.excelUri, env.ruleUri, env.editorTableUri);
constructor() {
if (!env.excelUri || !env.ruleUri || !env.editorTableUri) {
throw new Error('未找到地图目录');
}
return excelExporter._instance;
this.excelPath = env.excelUri;
this.rulePath = env.ruleUri;
this.editorTablePath = env.editorTableUri;
}

public async excelExport(){
await this.getEditorTableData();
await this.loadImportRules();
await this.convertByRules();
}

public async getEditorTableData() {
let editorTablePath = env.editorTablePath;
const files = await fs.promises.readdir(editorTablePath);
for (const file of files) {
const filePath = path.join(editorTablePath, file);
let editorTableType: string = file;
if (editorTableType in englishPathToChinese) {
editorTableType = englishPathToChinese[editorTableType as EditorTableName];
const jsFileList = await fs.promises.readdir(filePath);// 此目录下的编文件js文件目录
this.editorTableDatas[editorTableType] = new editorTableDir(jsFileList, filePath);
}
else {
continue;
}
const filePath = path.join(editorTablePath, file);
let editorTableType: string = file;
if (editorTableType in englishPathToChinese) {
editorTableType = englishPathToChinese[editorTableType as EditorTableName];
const jsFileList = await fs.promises.readdir(filePath);// 此目录下的编文件js文件目录
this.editorTableDatas[editorTableType] = new editorTableDir(jsFileList, filePath);
}
else {
continue;
}
}
}

private async convertByRules(){
// 处理 Excel 文件
try {
let importRules = this.importRules;
for(let rule of importRules){
if(!rule || !rule.excelRelativePath || !rule.editorTableType){
throw new Error("rule定义出错");
}
let excelPath = vscode.Uri.joinPath(this.excelPath, rule.excelRelativePath);
let editorTableType = chineseTypeNameToEnglishTypeName[rule.editorTableType];
let targetPath: vscode.Uri = vscode.Uri.joinPath(this.editorTablePath, csvTypeToPath[editorTableType]);
let converter = new excel2Json(rule, excelPath, targetPath);
await converter.convert();//TODO: 把excel数据通过converter转换为目标数据
}
return true;
}
catch (error){
vscode.window.showErrorMessage("处理导入规则importRule出错,错误类型为:" + error);
return false;
}
}
async runRule(rule: ImportRule) {
let excelPath = rule.excelRelativePath ? vscode.Uri.joinPath(this.excelPath, rule.excelRelativePath) : undefined;
let editorTableType = chineseTypeNameToEnglishTypeName[rule.editorTableType];
let targetPath: vscode.Uri = vscode.Uri.joinPath(this.editorTablePath, csvTypeToPath[editorTableType]);
//let converter = new excel2Json(rule, excelPath, targetPath);
//await converter.convert();//TODO: 把excel数据通过converter转换为目标数据
}

private async loadImportRules(){
this.importRules = [];
// 读取目录中的所有.mjs文件
const files = fs.readdirSync(this.rulePath.fsPath).filter(file => file.endsWith('.js') || file.endsWith('.mjs'));
for (const file of files) {
Expand All @@ -101,7 +71,7 @@ export class excelExporter extends tableExpoter{
let ImportRulesModule = require(src.fsPath);
for(let ruleName in ImportRulesModule){
if(ImportRulesModule[ruleName] instanceof ImportRule){
this.importRules.push(ImportRulesModule[ruleName]);
await this.runRule(ImportRulesModule[ruleName]);
}
}
} catch (error) {
Expand Down
59 changes: 39 additions & 20 deletions src/editorTable/EXCEL/importRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,59 @@ export class ImportRule {
public editorTableType: constants.ObjectTypeNameCN;
public excelRelativePath?: string;
public sheet?: string;
public fieldDefs: {[key: string] : [string | string[] | number[], any, any]} = {};
public fieldDefs: {
[key: string]: [string | string[] | number[], any, any];
} = {};
public filter: Function | undefined;
public jumpHeader: number = 1;
public dataRehandle: Function | undefined;

constructor(editorTableType: constants.ObjectTypeNameCN, excelRelativePath?: string, sheetName?: string){
constructor(
editorTableType: constants.ObjectTypeNameCN,
excelRelativePath?: string,
sheetName?: string
) {
this.editorTableType = editorTableType;
this.excelRelativePath = excelRelativePath;
this.sheet = sheetName;
}
public resetRule(editorTableType: constants.ObjectTypeNameCN, excelRelativePath?: string, sheetName?: string){
public resetRule(
editorTableType: constants.ObjectTypeNameCN,
excelRelativePath?: string,
sheetName?: string
) {
this.editorTableType = editorTableType;
this.excelRelativePath = excelRelativePath;
this.sheet = sheetName;
}

public def(fieldName: string, filedKey: string | string[] | number[], fieldType: any, attrType: any){
public def(
fieldName: string,
filedKey: string | string[] | number[],
fieldType: any,
attrType: any
) {
this.fieldDefs[fieldName] = [filedKey, fieldType, attrType];
}

public templateBy(fieldName: string){
this.fieldDefs[fieldName] = ['tmpid', Templete, null];
public templateBy(fieldName: string) {
this.fieldDefs[fieldName] = ["tmpid", Templete, null];
}

public indexBy(fieldName: string){
this.fieldDefs[fieldName] = ['uid', Str, INDEX];
public indexBy(fieldName: string) {
this.fieldDefs[fieldName] = ["uid", Str, INDEX];
}

public startBy(jumpHeader: number){
public startBy(jumpHeader: number) {
this.jumpHeader = jumpHeader;
}

public copyRule(): ImportRule{
let new_rule = new ImportRule(this.editorTableType, this.excelRelativePath, this.sheet);
public copyRule(): ImportRule {
let new_rule = new ImportRule(
this.editorTableType,
this.excelRelativePath,
this.sheet
);
new_rule.fieldDefs = this.fieldDefs;
new_rule.filter = this.filter;
new_rule.jumpHeader = this.jumpHeader;
Expand All @@ -48,21 +67,21 @@ export class ImportRule {
}

public deepCopy(obj: any): any {
if (typeof obj !== 'object' || obj === null) {
return obj; // 如果是基本类型或者null,则直接返回
if (typeof obj !== "object" || obj === null) {
return obj; // 如果是基本类型或者null,则直接返回
}

if (Array.isArray(obj)) {
return obj.map(item => this.deepCopy(item)); // 如果是数组,递归调用deepCopy对数组中的每个元素进行深复制
return obj.map((item) => this.deepCopy(item)); // 如果是数组,递归调用deepCopy对数组中的每个元素进行深复制
}

// 如果是对象,则遍历对象的属性,对每个属性递归调用deepCopy进行深复制
const newObj: { [key: string]: any } = {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
newObj[key] = this.deepCopy(obj[key]);
}
if (obj.hasOwnProperty(key)) {
newObj[key] = this.deepCopy(obj[key]);
}
}
return newObj;
}
}
}
1 change: 1 addition & 0 deletions src/editorTable/EXCEL/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './fieldTypes';
export * from './attrType';
export * from './importRule';
export * from './excelExporter';
5 changes: 0 additions & 5 deletions src/editorTable/EXCEL/tableExpoter.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/editorTable/EXCEL/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import * as y3 from 'y3-helper';

export function test() {
let rule = new y3.excel.ImportRule('单位');

}
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class Helper {
title: "Executing Task",
cancellable: false
}, async (progress, token) => {
let Expoter = excelExporter.getInstance();
let Expoter = new excelExporter();
await Expoter?.excelExport();

// this.editorTableDataProvider?.refresh();
Expand Down

0 comments on commit 84f5a74

Please sign in to comment.