跳到主要内容

如何调用命令

要调用命令并将数据发送回编辑器,需要定义调用命令方法。该方法允许插件发送结构化数据,这些数据可以插入到最终的文档中(如格式化段落、表格、文本片段或单词等)。

调用命令

参数

名称类型描述
funcfunction用 JavaScript 编写的命令,其作用是构造可插入至最终文档文件中的结构化数据(如格式化段落、表格、文本片段或单词等),随后将该数据发送给编辑器。该命令必须符合 Office JavaScript API 的语法。
isCloseboolean指定在执行完 func 参数中的代码后是否关闭插件窗口。值为 true 时将在执行后关闭插件窗口;值为 false 时将在执行后保持窗口开启,以便继续接收其他命令或操作。默认值为 false。
isCalcboolean指定在执行 func 参数中的代码后是否重新计算文档内容。值为 true 时将在执行后重新计算文档;值为 false 时不重新计算(仅当您确认所做修改不影响文档计算时使用)。默认值为 true。
callbackfunction方法返回的结果。仅支持 JavaScript 标准类型(如传入对象将被替换为 undefined)。

返回值

此方法不返回任何值。

示例

Asc.plugin.callCommand(() => {
const oDocument = Api.GetDocument()
const oParagraph = Api.CreateParagraph()
oParagraph.AddText("Hello world")
oDocument.InsertContent([oParagraph])
}, true, true, (returnValue) => {})

Asc.scope 对象

该方法在其独立上下文中执行,与其他 JavaScript 数据相互隔离。如果需要向该方法传递某些参数或其他附加数据(如对象、参数、变量等),可以使用 Asc.scope 对象。

备注

不能通过 Asc.scope 对象将函数传递给调用命令方法

示例

Asc.scope.text = text
Asc.plugin.callCommand(() => {
const oDocument = Api.GetDocument()
const oParagraph = Api.CreateParagraph()
oParagraph.AddText(Asc.scope.text)
oDocument.InsertContent([oParagraph])
}, true, true, (returnValue) => {})

info 对象

Asc.plugin.info 对象是 Asc.plugin 对象的属性。有关完整的属性列表和示例,请参阅 Asc.plugin > info 对象

调试

要在浏览器控制台中记录所有 callCommandexecuteMethod 调用,请在浏览器本地存储中设置 asc_plugin_commands_log 键:

localStorage.setItem("asc_plugin_commands_log", "true");

要禁用日志,请删除该键:

localStorage.removeItem("asc_plugin_commands_log");

该设置在页面重新加载后仍然有效。