我开始编写我的 Excel 工作表的验证代码。我实现了很多,所以我尽量保持代码简短。
我的源代码:
function main(workbook: ExcelScript.Workbook) {
console.log("starting...");
let cs = createVariantCategorySheet(workbook.getWorksheet("variant categories"));
cs.validateSheet();
for (let m of cs.logMessages) {
console.log(m);
}
console.log("finished...");
}
function createVariantCategorySheet(worksheet: ExcelScript.Worksheet): ImportSheet {
let sheet = new ImportSheetBuilder()
.name("variant categories")
.index(1)
.worksheet(worksheet)
.addColumnInfo(
new ColumnInfoBuilder()
.index(4)
.name("Sort Order")
// .addChecker(new NotEmptyChecker())
.addChecker(new UniqueSortOrderChecker(worksheet, worksheet.getRange("B:B")))
.build()
)
.build();
return sheet;
}
class ImportSheet {
public name: string;
public index: number;
public worksheet?: ExcelScript.Worksheet
columns: ColumnInfo[];
public logMessages: string[];
constructor() {
this.index = -1;
this.name = "";
this.columns = [];
this.logMessages = [];
}
// ... Some usefull private methods ...
validateSheet() {
let rowCount = this.getRowCount();
for (let rowIndex = 1; rowIndex < rowCount; rowIndex++) {
if (this.isRowEmpty(rowIndex)) {
this.logMessages.push("no more lines... ")
return;
}
this.validatingRow(rowIndex);
}
}
private validatingRow(rowIndex: number) {
this.logMessages.push("iterating over line [" + rowIndex + "]")
for (let columnItem of this.columns) {
this.validatingColumn(columnItem, this.rangeToValidate(rowIndex, columnItem.index));
}
}
private validatingColumn(columnItem: ColumnInfo, rangeToValidate: ExcelScript.Range){
for (let validator of columnItem.validator) {
let stringToValidate = rangeToValidate.getValue().toString();
this.logMessages.push("###### " + validator.isValid)
if (!validator.isValid(stringToValidate, rangeToValidate.getRowIndex())) {
this.logMessages.push("## ## ERROR: Failed Validation " + validator.getName() +
" in '" + rangeToValidate.getAddress() + "' for column '" +
columnItem.name + "'.");
} else {
this.logMessages.push("## ## INFO: Success Validation " + validator.getName() +
" in '" + rangeToValidate.getAddress() + "' for column '" +
columnItem.name + "'.");
}
}
}
}
class ColumnInfo {
name: string;
index: number;
validator: Checker[];
}
interface Checker {
isValid(value: string, rowIndex: number): boolean
getName(): string
}
class NotEmptyChecker implements Checker {
isValid(value: string): boolean {
if (value)
return true;
else
return false;
}
getName(): string {
return NotEmptyChecker.name;
}
}
class UniqueSortOrderChecker implements Checker {
sortOrderMap: Map<string, Set<string>>;
range: ExcelScript.Range
worksheet: ExcelScript.Worksheet
constructor(worksheet: ExcelScript.Worksheet, range: ExcelScript.Range) {
this.range = range;
this.worksheet = worksheet;
this.sortOrderMap = new Map<string, Set<string>>();
}
isValid(value: string, rowIndex: number): boolean {
return true;
}
getName(): string {
return UniqueSortOrderChecker.name;
}
}
我省略了 Builders,因为他们只是创建对象。
上面的代码可以正常工作并像我希望的那样验证文件。
该方法的生成代码UniqueSortOrderChecker.isValid
如下所示:
function (value, rowIndex) {
ExcelScript.engine.traceLine(undefined);
return true;
}
所以有趣的部分是,一旦我改变UniqueSortOrderChecker
成:
isValid(value: string, rowIndex: number): boolean {
console.log("test");
return true;
}
生成的代码现在看起来完全不同并返回一个承诺:
function (value, rowIndex) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
ExcelScript.engine.traceLine(264);
return [4 /*yield*/, console.log("test")];
case 1:
(_a.sent());
ExcelScript.engine.traceLine(undefined);
return [2 /*return*/, true];
}
});
});
}
当我尝试读取单元格值时也会发生同样的情况:
isValid(value: string, rowIndex: number): boolean {
let cellValue = this.range.getRow(rowIndex).getValue().toString();
return true;
}
把它变成一个承诺返回类型,如:
function (value, rowIndex) {
return __awaiter(this, void 0, void 0, function () {
var cellValue;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
ExcelScript.engine.traceLine(264);
return [4 /*yield*/, this.range.getRow(rowIndex).getValue()];
case 1:
cellValue = (_a.sent()).toString();
ExcelScript.engine.traceLine(undefined);
return [2 /*return*/, true];
}
});
});
}
有谁可以帮我离开这里吗?一旦它变成一个承诺,返回值总是真实的,我的验证不起作用。
希望这个问题不会太长,但我想给出上下文。