环境:win10,Visual Studio 2019,javascript
我想自定义一个类,以确定数据对于给定数据类型和给定数据是否合法,例如,给定数据类型列表,给定数据[1,2]是否满足要求。
此类的文件使用VScode实现,并使用Node.js进行调试。它成功运行
我使用excel api实现了一些基本功能,例如文档数据读取,现在我想将该类导入到我的项目中。
我在Internet上找到的方法是,首先将文件添加到项目的脚本文件夹,然后添加标记以指示Home.html中的路径。然后在Home.js中调用该类。 JavaScript.js是类文件
在Home.js中调用
这是我对ParserFactory类的实现
// JavaScript source code
//列规则默认分隔符
RULE_SEP = ",";
RULE_KV_SEP = ":";
//内容默认分隔符
DEFAULT_SEP = ",";
DEFAULT_KV_SEP = ":";
function get_most(str) {
let result = Array.prototype.reduce.call(str, function (all_words, cur_word) {
all_words[cur_word] ? all_words[cur_word]++ : all_words[cur_word] = 1;
return all_words;
}, {});
return result;
}
function isEmpty(object) {
for (var i in object) {
return true;
}
return false;
}
function get_cnt(cnt, ch) {
let result = 0;
if (ch in cnt) {
result += cnt[ch];
}
return result;
}
function int_check(s) {
let val = Number(s);
return Number.isInteger(val);
}
class ParserFactory {
static get_parser(rule, parent) {
let after_extract_brackets = Parser.extract_brackets(rule, "{}");
let p;
rule = after_extract_brackets[0];
let enum_info = after_extract_brackets[1];
if (rule.startsWith(ListParser.PREFIX)) {
p = new ListParser(rule, parent);
}
else {
p = new EvalParser(rule, parent);
}
if (enum_info) {
let items = Parser.split(enum_info, RULE_SEP);
for (let item in items) {
let items1 = Parser.split(item, RULE_KV_SEP);
for (let item1 in items1) {
p.enum = [item1[0], item1[1]];
}
}
}
return p;
}
}
class Parser {
constructor(rule, parent) {
this.rule = rule;
this.parent = parent;
this.enum = {};
}
call(s) {
this.parse(s);
}
parse(s) {
if (isEmpty(this.enum) === 0) {
return this._parse(this.enum[s]);
}
else {
return this._parse(s);
}
}
get_separators() {
return ""; //使用字符串初始化,方便后续indexof操作
}
static split(s, sep, is_rule = false) {
let parts = s.split(sep);
let new_parts = new Array();
let part = "";
parts.forEach(element => {
if (part === "") {
part += element;
}
else {
part += sep + element;
}
let cnt = get_most(part);
let l_cnt = get_cnt(cnt, '{') + get_cnt(cnt, '[') + get_cnt(cnt, '(');
let r_cnt = get_cnt(cnt, '}') + get_cnt(cnt, ']') + get_cnt(cnt, ')');
if (is_rule) {
l_cnt += get_cnt(cnt, '<');
r_cnt += get_cnt(cnt, '>');
}
if (l_cnt == r_cnt) {
new_parts.push(part);
part = "";
}
});
return new_parts;
}
static is_match(s, lr) {
let l = lr[0];
let r = lr[1];
let cnt = { l: 0, r: 0 };
for (let letter of s) {
if (letter == l) {
cnt.l++;
}
else if (letter == r) {
cnt.r++;
if (cnt.r > cnt.l) {
return false;
}
}
}
if (l === r) {
return cnt.l % 2 === 0;
}
else {
return cnt.l === cnt.r;
}
}
static extract_brackets(rule, brackets = "<>") {
let result = new Array();
if (rule.endsWith(brackets[1])) {
let idx = rule.indexOf(brackets[0]);
if (idx >= 0) {
result.push(rule.substr(0, idx));
result.push(rule.substr(idx + 1, rule.length - idx - 2));
return result;
}
}
else {
result.push(rule);
return result;
}
}
}
class EvalParser extends Parser {
constructor(rule, parent) {
super(rule, parent);
if (rule === "bool") {
this.func;
}
else if (rule === "str_strip") {
}
else if (rule) {
this.func = int_check;
}
else {
this.func = str_check;
}
}
_parse(s) {
return this.func(s);
}
}
class ListParser extends Parser {
static PREFIX = "list";
constructor(rule, parent) {
super(rule, parent);
let after_extract_brackets = Parser.extract_brackets(rule);
let pre = after_extract_brackets[0];
rule = after_extract_brackets[1];
if (pre.length == ListParser.PREFIX.length) {
this.separator = DEFAULT_SEP;
}
else {
this.separator = pre.substr(pre.length + 1, 1);
}
this.func = ParserFactory.get_parser(rule, this);
this.need_brackets = this.parent != null && this.parent.get_separators().indexOf(this.separator) >= 0;
}
_parse(s) {
let result = true;
let with_brackets = s.startsWith('[') && s.endsWith(']') && Parser.is_match(s.substr(1, s.length - 2), '[]');
if (this.need_brackets) {
}
if (with_brackets) {
s = s.substr(1, s.length - 2);
}
if (s === "") {
return false;
}
Parser.split(s, this.separator).forEach(element => {
result = result && this.func.parse(element);
});
return result;
}
get_separators() {
return this.separator;
}
}
//parser = ParserFactory.get_parser("list<list<int>>");
//console.log(parser.parse("[[[1]]]"));
//parser = ParserFactory.get_parser("int");
//console.log(parser.parse("ssd"));
//console.log(parser.parse("1234.5"));
//console.log(parser.parse("-5"));
//console.log(parser.parse("0"));
//console.log(parser.parse("[1]"));
Home.js代码
(function () {
"use strict";
var cellToHighlight;
var messageBanner;
// The initialize function must be run each time a new page is loaded.
Office.initialize = function (reason) {
$(document).ready(function () {
// Initialize the notification mechanism and hide it
var element = document.querySelector('.MessageBanner');
messageBanner = new components.MessageBanner(element);
messageBanner.hideBanner();
// If not using Excel 2016, use fallback logic.
if (!Office.context.requirements.isSetSupported('ExcelApi', '1.1')) {
$("#template-description").text("This sample will display the value of the cells that you have selected in the spreadsheet.");
$('#button-text').text("Display!");
$('#button-desc').text("Display the selection");
$('#highlight-button').click(displaySelectedCells);
return;
}
$("#template-description").text("This sample highlights the highest value from the cells you have selected in the spreadsheet.");
$('#button-text').text("Highlight!");
$('#button-desc').text("Highlights the largest number.");
loadSampleData();
// Add a click event handler for the highlight button.
$('#highlight-button').click(hightlightHighestValue);
Excel.run(async context => {
var mysheet = context.workbook.worksheets.getActiveWorksheet();
mysheet.onChanged.add(valueChanged);
//mysheet.onSelectionChanged.add(selectionChanged);
await context.sync();
});
});
};
async function valueChanged(event) {
await Excel.run(async (context) => {
let range_to_edit = event.getRange(context);
range_to_edit.format.fill.clear();
range_to_edit.load("columnIndex,rowIndex,values");
await context.sync();
if (range_to_edit.rowIndex == 1) return;
let cur_sheet = context.workbook.worksheets.getActiveWorksheet();
let range_for_check_rules = cur_sheet.getCell(1, range_to_edit.columnIndex);
range_for_check_rules.load("text");
await context.sync();
var a1 = format_check(range_for_check_rules.text[0][0], range_to_edit.values[0][0]);
if (!a1) {
range_to_edit.format.fill.color = "yellow";
}
});
}
function format_check(setted_format, cell_value) {
parse = ParserFactory.get_parser(setted_format);
return parse.parse(cell_value);
}
})();
错误如下
error for code below,PREFIX,dont know why
class ListParser extends Parser {
static PREFIX = "list";