button

定义与插件一起使用的按钮以及单击它们时的插件行为。 单击任何插件按钮时,将调用此方法。

模态框形式的插件才能定义 buttons 属性, isVisual == true && isInsideMode == false 详情关联

相关参数

属性名 描述 类型
id 在插件配置 config.json 文件的 button 数组中定义按钮索引。 如果 id == -1 ,则插件认为关闭窗口按钮已被单击,或者其操作已被中断。 number

示例代码

  1. window.Asc.plugin.button = function (id) {
  2. console.log('当前按钮索引', id)
  3. if (id === -1) {
  4. this.executeCommand("close", '')
  5. }
  6. }

callCommand (window.Asc.plugin.callCommand(fCommand, isClose))

定义用于将数据发送回编辑器的方法。 在处理文本时,它替换了executeCommand方法,以简化使用ONLYOFFICE Document Builder API传递给编辑器所必需的脚本语法。 它允许插件发送可以插入到结果文档文件中的结构化数据(格式化的段落,表格,文本部分和单独的单词等)。

注意:

ONLYOFFICE Document Builder 命令只能用于创建内容并将其插入到文档编辑器中(使用 Api.GetDocument().InsertContent(...) )。 由于在线编辑器中具有共同编辑功能,因此存在此限制。 如果需要为桌面编辑器创建一个插件以使用本地文件,则不会应用此类限制。

相关参数

属性名 描述 类型
fCommand 定义用JavaScript编写的命令,该命令的目的是形成结构化的数据,该数据可以插入到生成的文档文件中(格式化的段落,表格,文本部分和单独的单词等)。 然后将数据发送到编辑器。 该命令必须与ONLYOFFICE Document Builder语法兼容。 function
isClose 定义是在执行代码后必须关闭插件窗口还是必须打开以等待其他命令或操作。 true值用于在执行fCommand参数中的功能后关闭插件窗口。 false值用于执行命令并使窗口保持打开状态,等待下一条命令。 boolean

fCommand 里面使用数据

fCommand与其他 JavaScript 代码隔离,不能直接取到数据,需要使用 Asc.scope 对象。
注意:

定义 window.Asc.plugin.callCommand 方法,该方法在与其他JavaScript数据隔离的自身上下文中执行。 如果需要将某些参数或其他数据传递给此方法,请使用 Asc.scope 对象。

示例代码

  1. handleClick(item, index) {
  2. window.Asc.scope.text = item // export variable to plugin scope
  3. this.ctx.callCommand(function () {
  4. const scopeText = Asc.scope.text
  5. const oDocument = Api.GetDocument()
  6. const oParagraph = Api.CreateParagraph()
  7. oParagraph.AddText(scopeText)
  8. oDocument.InsertContent([oParagraph])
  9. }, false)
  10. }

executeCommand (window.Asc.plugin.executeCommand(type, command))

用于将数据发送回编辑器。 此方法主要用于OLE对象,并保留用于文本,以使插件的早期版本保持兼容。

第二个参数是符合 ONLYOFFICE Document Builder API 的 JavaScript 代码,允许插件将结构化数据(格式化的段落,表格,文本部分和单独的单词等)插入到结果文档文件中的代码。

注意:

定义 window.Asc.plugin.callCommand 方法,该方法在与其他JavaScript数据隔离的自身上下文中执行。 如果需要将某些参数或其他数据传递给此方法,请使用 Asc.scope 对象。


相关参数

属性名 描述 类型
type 可选,定义命令的类型。 执行命令参数中的功能后,close 用于关闭插件窗口。command 时用于执行命令并使窗口保持打开状态,等待下一个命令。 string
command 定义用 JavaScript 代码编写的命令,该命令的目的是形成可以插入到结果文档文件中的结构化数据(格式化的段落,表格,文本部分和单独的单词等), 然后将数据发送到编辑器。 该命令必须与ONLYOFFICE Document Builder 语法兼容。 string

创建/编辑OLE对象时,使用两个扩展名进行处理:

  • Api.asc_addOleObject(window.Asc.plugin.info)-用于在文档中创建OLE对象;
  • Api.asc_editOleObject(window.Asc.plugin.info)-用于编辑创建的OLE对象。

创建/编辑OLE对象时,可以将其属性传递到 window.Asc.plugin.info 对象,该对象定义对象的外观。

示例代码

  1. // OLE对象的示例
  2. // 此示例目前还有问题 请使用executeMethod
  3. window.Asc.plugin.button = function (id) {
  4. var _info = window.Asc.plugin.info;
  5. var _method = (_info.objectId === undefined) ? "asc_addOleObject" : "asc_editOleObject";
  6. _info.width = _info.width ? _info.width : 70;
  7. _info.height = _info.height ? _info.height : 70;
  8. _info.widthPix = (_info.mmToPx * _info.width) >> 0;
  9. _info.heightPix = (_info.mmToPx * _info.height) >> 0;
  10. _info.imgSrc = 'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png';
  11. _info.data = window.g_board.getData();
  12. var _code = "Api." + _method + "(" + JSON.stringify(_info) + ");";
  13. this.executeCommand("close", _code);
  14. };
  15. // 文本示例(出于兼容性原因未使用但保留)
  16. window.Asc.plugin.init = function () {
  17. var sScript = 'var oDocument = Api.GetDocument();';
  18. sScript += 'oDocument.CreateNewHistoryPoint();';
  19. sScript += 'oParagraph = Api.CreateParagraph();';
  20. sScript += 'oParagraph.AddText(\'Hello word!\');';
  21. sScript += 'oDocument.InsertContent([oParagraph]);';
  22. window.Asc.plugin.info.recalculate = true;
  23. this.executeCommand("close", sScript);
  24. };

executeMethod (window.Asc.plugin.executeMethod(Name, [args], callback))

window.Asc.plugin.executeMethod(Name, [args], callback)

用来执行在定义 window.Asc.plugin 对象的某些方法,这些方法是允许使用插件执行编辑器的某些方法。 Name 是执行的特定方法的名称,[args] 是使用中的方法具有的参数(如果有),而 callback 是该方法返回的结果。 后者是可选参数。 如果缺少它,则将使用 window.Asc.plugin.onMethodReturn 方法返回该方法执行的结果。

方法和描述

Name 描述 Name 描述
InsertAndReplaceContentControls 此方法插入一个包含数据的内容控件。 PasteHtml 此方法用来将html格式的文本粘贴到文档中。
RemoveContentControls 此方法用来删除几个内容控件。 PasteText 此方法用来将文本粘贴到文档中。
GetAllContentControls 此方法允许获取有关已添加到页面的所有内容控件的信息。 GetMacros 此方法用来获取文档宏。
AddContentControl 此方法允许向文档中添加一个空的内容控件。 SetMacros 此方法用来为文档设置宏。
RemoveContentControl 此方法允许删除内容控件,但保留其所有内容。 StartAction 此方法用来为长时间操作指定启动操作。
GetCurrentContentControl 此方法用来获取所选内容控件的标识符。 EndAction 此方法用来为长时间操作指定结束动作。
AddOleObject 此方法允许将OLE对象添加到文档中。 OnEncryption 此方法用来加密插件。
EditOleObject 此方法用来编辑文档中的OLE对象。 SetProperties 此方法用来为文档设置属性。
GetFontList 此方法用来获取字体列表。 ShowInputHelper 此方法用来显示输入助手。
InputText 此方法用来将文本插入文档中。 UnShowInputHelper 此方法用来取消显示输入助手。

为了使插件正常工作,必须等到当前方法执行后再执行下一个方法。

InsertAndReplaceContentControls

RemoveContentControls

GetAllContentControls

AddContentControl

RemoveContentControl

GetCurrentContentControl

AddOleObject

EditOleObject

GetFontList

参数:无
返回值:

  1. [
  2. {
  3. "m_wsFontName": "string",
  4. "m_wsFontPath": "string",
  5. "m_lIndex": number,
  6. "m_bBold":boolean,
  7. "m_bItalic": boolean,
  8. "m_bIsFixed": boolean,
  9. "m_aPanose": number[],
  10. "m_ulUnicodeRange1": number,
  11. "m_ulUnicodeRange2": number,
  12. "m_ulUnicodeRange3": number,
  13. "m_ulUnicodeRange4":number,
  14. "m_ulCodePageRange1": number,
  15. "m_ulCodePageRange2": number,
  16. "m_usWeigth": number,
  17. "m_usWidth": number,
  18. "m_sFamilyClass": "string",
  19. "m_eFontFormat": "string",
  20. "m_shAvgCharWidth":number,
  21. "m_shAscent": number,
  22. "m_shDescent": number,
  23. "m_shLineGap": number,
  24. "m_shXHeight": number,
  25. "m_shCapHeight": number
  26. }
  27. ]

使用方法及示例:

  1. window.Asc.plugin.executeMethod("GetFontList");
  2. window.Asc.plugin.executeMethod("PasteHtml", ["<p><b>Plugin methods for OLE objects</b></p><ul><li>AddOleObject</li><li>EditOleObject</li></ul>"]);

InputText

参数:text:string, textReplaced:string
返回值: undefined

textReplaced 参数源文档为 textReplace,此处根据含义调整

使用方法及示例:

  1. window.Asc.plugin.executeMethod("InputText", [text, textReplaced]);
  2. window.Asc.plugin.executeMethod("InputText", ["text", "textReplaced"]);

PasteHtml

参数:htmlText:string
返回值: undefined

使用方法及示例:

  1. window.Asc.plugin.executeMethod("PasteHtml", [htmlText]);
  2. window.Asc.plugin.executeMethod("PasteHtml", ["<p><b>Plugin methods for OLE objects</b></p><ul><li>AddOleObject</li><li>EditOleObject</li></ul>"]);

PasteText

参数:text:string
返回值: undefined

使用方法及示例:

  1. window.Asc.plugin.executeMethod("PasteText", [text]);
  2. window.Asc.plugin.executeMethod("PasteText", ["PasteText"]);

GetMacros

SetMacros

StartAction

EndAction

OnEncryption

SetProperties

ShowInputHelper

UnShowInputHelper

info object (window.Asc.plugin.info)

定义辅助window.Asc.plugin.info对象,该对象在插件工作时可用。 它存储有关使用插件的编辑器的所有信息(被使用在 editorType -文本文档,电子表格,演示文稿)以及OLE对象的其他设置(宽度,高度,毫米与像素之比和其他 OLE 对象参数)。

执行window.Asc.plugin.executeCommand时,此对象用于更改对象数据并发送其他参数。 例如,如果文档内容已更改且需要重新计算,则必须将参数window.Asc.plugin.info.recalculate设置为true。 因为重新计算过程是异步的,所以此操作是必需的。 此外,可能还需要上传一些其他数据(例如,字体或其他内容)。

属性及方法

Name Description Type Access
data The OLE object data which need to be sent to the window.Asc.plugin.info object and used by the plugin. OLE object data write
editorType Returns the type of the editor where the plugin is currently running. string read-only
guid The OLE object GUID in the current document. string read-only
height The OLE object height measured in millimeters. number read-only
imgSrc The link to the image (its visual representation) stored in the OLE object and used by the plugin. string write
mmToPx Millimeter to pixel conversion ratio for the OLE object vector draw. number read-only
objectId The OLE object identifier in the current document. string read-only
recalculate Force the document to recalculate its content data. boolean write
resize Checks if the OLE object size has been changed. boolean read-only
width The OLE object width measured in millimeters. number read-only

init (window.Asc.plugin.init(data))

定义发送到插件的数据,描述要执行的操作以及必须执行的操作。 启动插件时将调用此方法。

Name 描述 Type
data 定义依赖于插件配置config.json文件中指定的initDataType设置的数据参数。 数据的类型如下:none-空字符串,text-所选文档的文本,html-所选文档的片段,ole-OLE对象的数据。 string

window.Asc.plugin.init.onExternalMouseUp(fMouseUp)

定义在插件iframe外部释放鼠标按钮时要执行的操作。

Name 描述 Type
fMouseUp 定义在插件iframe外部释放鼠标按钮时将执行的功能。 function

示例代码

  1. window.Asc.plugin.onExternalMouseUp = function () {
  2. var evt = document.createEvent("MouseEvents");
  3. evt.initMouseEvent("mouseup", true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
  4. document.dispatchEvent(evt);
  5. };

window.Asc.plugin.init.onMethodReturn(fMouseUp)

定义window.Asc.plugin对象方法,该方法允许返回先前执行的方法的结果。 执行 window.Asc.plugin.executeMethod(…) 方法后,可用于返回数据。

示例代码

  1. window.Asc.plugin.executeMethod("InsertAndReplaceContentControls", [_obj]);
  2. window.Asc.plugin.onMethodReturn = function(returnValue) {
  3. if (window.Asc.plugin.info.methodName == "InsertAndReplaceContentControls") {
  4. window.Asc.plugin.executeMethod("GetAllContentControls");
  5. } else if ("GetAllContentControls") {
  6. window.Asc.plugin.executeCommand("close", console.log(JSON.stringify(returnValue)));
  7. }
  8. };

Asc.scope object

该对象用于将任何其他数据(对象,参数,变量等)传递到 window.Asc.plugin.callCommand 方法,该方法在其自己的隔离上下文中执行。

示例代码

  1. handleClick(item, index) {
  2. window.Asc.scope.text = item // export variable to plugin scope
  3. this.ctx.callCommand(function () {
  4. const scopeText = Asc.scope.text
  5. const oDocument = Api.GetDocument()
  6. const oParagraph = Api.CreateParagraph()
  7. oParagraph.AddText(scopeText)
  8. oDocument.InsertContent([oParagraph])
  9. }, false)
  10. }