//代码写在:你的加载项文件夹\js\functions.js 中
//加载项自定义函数在单元格、JSA中调用
//1.单元格中调用函数,例如:HelloEt.btoa();
//2.JSA中调用函数,例如:
// const addin = Application.WPSAddIns.Item("HelloWps"); // 加载项名称
// const btoa = addin.Functions.Item("btoa"); // 加载项中注册的函数名
// let result = btoa.Call("ABCabc123"); // 调用函数
//第一种方法:注册自定义函数和元数据,现主要用于包装内置对象的方法
if (wps.AddCustomFunction !== undefined) {
{
//1.让 单元格中 可以调用函数
wps.AddCustomFunction("HelloEt", "btoa", btoa, {
"description": "以 base-64 编码字符串。",
"result": {
"type": "string"
},
"parameters": [
{
"name": "data",
"type": "string",
"description": "必需。要编码的字符串。"
}
]
});
//2.让 js宏中 可以调用函数
let func = Application.CurrentWPSAddIn.Functions.Register("btoa", btoa);
func.AllowJsIdeCall = true;
}
{
//1.让 单元格中 可以调用函数
wps.AddCustomFunction("HelloEt", "atob", atob, {
"description": "对 base-64 编码的字符串进行解码。",
"result": {
"type": "string"
},
"parameters": [
{
"name": "data",
"type": "string",
"description": "必需。要解码的字符串。"
}
]
});
//2.让 js宏中 可以调用函数
let func = Application.CurrentWPSAddIn.Functions.Register("atob", atob);
func.AllowJsIdeCall = true;
}
}
//第二种方法:创建自定义函数 + jsdoc注释 + vite插件
//1.让 单元格中 可以调用函数
/**
* 这是一个加载项自定义函数
* @customfunction
* @param {string} arg0 - 支持字符串参数
* @param {number} arg1 - 支持数值参数
* @param {boolean} arg2 - 支持bool参数
* @returns {number} - 可以设置返回值类型
*/
function custom_function(arg0, arg1, arg2, arg3, arg4) {
let argAndType = (arg) => `${arg}: ${typeof arg}`
let argAndTypeList = [arg0, arg1, arg2, arg3, arg4].map(argAndType)
let message = `这是一个加载项自定义函数(${argAndTypeList.join(', ')})`
console.log(message)
return message
}
//2.让 js宏中 可以调用该函数
{
let func = Application.CurrentWPSAddIn.Functions.Register("custom_function", custom_function);
func.AllowJsIdeCall = true;
}