Skip to content

Commit

Permalink
修改MultiTable的结构
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Jul 25, 2024
1 parent 9828b12 commit b46443b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
"kind": "build",
"isDefault": true
}
},
{
"type": "shell",
"label": "🍟生成dts",
"command": "dts-bundle-generator src\\y3-helper.ts --o template\\plugin\\y3-helper.d.ts",
}
]
}
11 changes: 6 additions & 5 deletions src/editorTable/excel/excel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as y3 from 'y3-helper';
type Cells = Record<string, string>;
type TableKey = string | number;
export type Table = Record<TableKey, Record<string, string>>;
export type MultiTable = Record<TableKey, Record<string, string[]>>;
export type MultiTable = Record<TableKey, Record<string, string>[]>;

export class Sheet {
constructor(private sheet: exceljs.Worksheet) {
Expand Down Expand Up @@ -152,24 +152,25 @@ export class Sheet {
}

let table: MultiTable = {};
let current: Record<string, string[]> | undefined;
let current: Record<string, string>[] | undefined;

const mergeIntoCurrent = (row: exceljs.Row) => {
if (!current) {
return;
}
let record: Record<string, string> = {};
current.push(record);
for (let c = col; c <= this.sheet.columnCount; c++) {
const title = titles[c];
current[title] ??= [];
current[title].push(row.getCell(c).toString());
record[title] = row.getCell(c).toString();
}
};

for (let r = row + 1; r <= this.sheet.rowCount; r++) {
const row = this.sheet.getRow(r);
const key = row.getCell(col).toString();
if (key) {
current = {};
current = [];
table[key] = current;
}
mergeIntoCurrent(row);
Expand Down
2 changes: 1 addition & 1 deletion src/editorTable/excel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function loadFile(path: vscode.Uri | string, sheetName?: number | s
const exc = new excel.Excel();
const suc = await exc.loadFile(path);
if (!suc) {
throw new Error('加载文件失败:' + path.toString());
throw new Error('Excel打开失败:' + path.fsPath);
}
const sheet = exc.getSheet(sheetName ?? 1);
if (!sheet) {
Expand Down
10 changes: 5 additions & 5 deletions template/plugin/6/6.2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ let y3 = require('y3-helper')
export async function 多维表() {
y3.excel.setBaseDir(y3.env.pluginUri)

let excel = await y3.excel.loadFile('6-更多的演示/6.1-多维表')
let excel = await y3.excel.loadFile('6-更多的演示/6.2-多维表')
let table = excel.makeMultiTable()

y3.assert(table['风暴之锤']['字段1'][0] === '伤害')
y3.assert(table['风暴之锤']['字段1'][1] === '100|200|300')
y3.assert(table['风暴之锤'][0]['字段1'] === '伤害')
y3.assert(table['风暴之锤'][1]['字段1'] === '100|200|300')

y3.assert(table['雷霆一击']['编号'][0] === '1002')
y3.assert(table['雷霆一击'][0]['编号'] === '1002')

y3.assert(table['重击']['名字'][0] === '重击')
y3.assert(table['重击'][0]['名字'] === '重击')

y3.print('多维表读取成功!')
}
2 changes: 1 addition & 1 deletion template/plugin/y3-helper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as vscode from 'vscode';
export type Cells = Record<string, string>;
export type TableKey = string | number;
export type Table = Record<TableKey, Record<string, string>>;
export type MultiTable = Record<TableKey, Record<string, string[]>>;
export type MultiTable = Record<TableKey, Record<string, string>[]>;
declare class Sheet {
private sheet;
constructor(sheet: exceljs.Worksheet);
Expand Down

0 comments on commit b46443b

Please sign in to comment.