如何与插件交互?怎么把我的图片和文字传入到插件里,从而插入到word里

我的需求是,在点击我页面的按钮后,在word中的光标位置插入指定的文本和图片到word中

我现在是这么做的:
vue项目代码:

 const insertTextAtCursor = () => {
    onlyOfficeEditor.value.serviceCommand('123', '321')
  }

onlyoffice插件:

  /**
   * 接收外部数据
   * @param {string} data - 外部数据
   */
  window.parent.Common.Gateway.on('internalcommand', function(data){
    console.log(data.command); // 接受到123参数
    console.log(data.data); // 接受到321参数
    Asc.plugin.callCommand(() => {
      let doc = Api.GetDocument();
      let paragraph = doc.GetElement(0);
      // 插入文本, https://api.onlyoffice.com/docs/office-api/usage-api/text-document-api/Api/Methods/CreateWordArt/
      let textPr = Api.CreateTextPr();
      let textArt = Api.CreateWordArt(textPr, `这是接受到的文本: ${data.command} ${data.data} `);
      paragraph.AddDrawing(textArt);
    });
  });

当我调用**insertTextAtCursor() **方法的时候,页面显示如下:
image

复现步骤

  1. 在调用onlyoffice的页面我增加了一个插入按钮
  2. 点击按钮后
  3. 显示如上图

预期结果

能正常插入我传入的文字或图片

实际结果

报错

文档服务器版本

企业版试用版:onlyoffice-documentserver-ee 8.3.2-19

补充信息

之前您回答过我executeMethod一次性插入多条数据的问题?,现在我不知道怎么把这些数据传入到插件里从而再插入到word中?

谢谢

您好,
我不太了解您insertTextAtCursor方法的实现。如何想要使用此方法?
没办法获取光标位置,但所有将某种类型的内容添加到文档中的Office API方法都是在光标位置触发的。
为了插入文本需要使用以下方法:AddText | ONLYOFFICE

为了插入图片需要使用我们在其他论坛话题所讨论的这个方法:CreateImage | ONLYOFFICE
首先需要通过Asc.scope从插件到编辑器传递您所需要的图片 (参考这里的例子)

image

参数为js数组对象,谢谢

为了插件方法有效,您需要将插件添加至编辑器,请查看以下指南:

青还参考以下两个指南:

插件方法不能在实际插件之外使用,不能直接向编辑器添加 callCommand 方法,该方法仅用于插件内部。

如果要在编辑器中使用插件方法但不在插件内,则需要 Automation API ONLYOFFICE

我们提供了可用于文本和图像插入的方法。您可以在这里查看插件示例:

1 Like

Hello, have you solved this problem.

@hulianzaixian123 Are you implementing the same scenario? What specific issues are you encountering?