let yourElement = document.getElementById("yourElement");
let wsheet = new websheet('HTML', yourElement,0,0,800,400);
// 定义 UDF 类
class UDF {
// 定义静态私有变量 instance
static instance = null;
// 定义类的属性
name;
returnValueType;
// 构造函数
constructor() {
if (!UDF.instance) {
UDF.instance = this;
this.name = 'udf'; // 与表格内使用的公式名称一致
this.returnValueType = 'S'; // 文本
}
return UDF.instance;
}
// 静态方法,用于获取单例实例
static getInstance() {
if (!this.instance) {
this.instance = new UDF();
}
return this.instance;
}
// 定义 UDF 方法 传入参数和返回值见第五小节
UDF(pram, sheetname, workbook, formula) {
console.log(pram.toString());
return '函数UDF返回值';
}
}
/**
* 第一步 获取workbook
*/
let workbook = wsheet.Workbook();
/**
* 第二步 注册自定义函数
*/
workbook.AddUserDefineFunction(UDF);
let activeSheet = wsheet.ActiveSheet();
/**
* 使用udf
*/
activeSheet.SetCellValue('A1', '=udf()');
/**
* 第三步 重新绘制表格
*/
activeSheet.setColWidth(1, 160);
activeSheet.WorkFormula(); //重建公式
activeSheet.cacl();//公式计算
wsheet.BuildSheet();
wsheet.Draw();