executeMethod一次性插入多条数据的问题?

可以帮我看看吗?

我正在尝试使用 executeMethod 方法向文档中插入多条数据。executeMethod 方法的第二个参数是 Array 类型。以下是我的代码:

const arr = images.map(item => {
      return {
        guid: _info.guid,
        widthPix: (_info.mmToPx * _info.width) >> 0,
        heightPix: (_info.mmToPx * _info.height) >> 0,
        width: _info.width ? _info.width : 100,
        height: _info.height ? _info.height : 70,
        imgSrc: item.imageUrl,
        data: item.imageUrl,
        objectId: _info.objectId,
        resize: _info.resize,
      }
    });

window.Asc.plugin.executeMethod(_method, arr, function () {
    window.Asc.plugin.executeCommand("close", "");
});

复现步骤

  1. 准备一个包含 10 条数据的 images 数组。
  2. 定义包含 guidmmToPxwidthheightobjectIdresize 等相关属性的 _info 对象。
  3. 运行上述代码。

预期结果

我期望 arr 数组中的所有 10 条数据能一次性插入到文档中。

实际结果

文档中仅插入了一条数据。

文档服务器版本

Version: 7.3.3

补充信息

我不确定是 executeMethod 方法本身存在限制,还是我使用方法有误。我想知道如何修改代码以实现一次性插入多条数据。目前该问题没有相关的图片或视频文件。
executeMethod | ONLYOFFICE

I tried the following method and got an error, as shown in the picture

images.forEach((item) => {
      // Create a parameter object for each image
      var _param = {
        guid: _info.guid, 
        widthPix: (_info.mmToPx * _info.width) >> 0,
        heightPix: (_info.mmToPx * _info.height) >> 0,
        width: _info.width ? _info.width : 100,
        height: _info.height ? _info.height : 70,
        imgSrc: item.imageUrl, 
        data: item.imageUrl, 
        objectId: _info.objectId, 
        resize: _info.resize, 
      };

      // Execute the method for each image with its corresponding parameter
      window.Asc.plugin.executeMethod(_method, [_param], function () {
        // If it is the last image, then close the plugin
        if (images.indexOf(item) === images.length - 1) {
          window.Asc.plugin.executeCommand("close", "");
        }
      });
    });

您好,
请解释以下你传递为 _method 的实际方法是什么?此外,请更新到最新的 8.3 版本,并在该版本上进行所有测试,因为我们不再为旧版本提供支持。

另外,请查看此指南,以了解更多关于 executeMethod 的信息:

以下是编辑器 executeMethod 支持的方法:

最后,请更详细地描述您的场景——您只是想批量插入图片,还是除了图片之外还要执行其他操作?

您好:我已经更新到最新8.3版本了
1: _method 的实际方法:

  function insertImage(images) {
    var _info = window.Asc.plugin.info;
    images.forEach((item)  => {
      var _param = {
        guid: _info.guid, 
        widthPix: (_info.mmToPx  * _info.width)  >> 0,
        heightPix: (_info.mmToPx  * _info.height)  >> 0,
        width: _info.width?  _info.width  : 100,
        height: _info.height?  _info.height  : 70,
        imgSrc: item.imageUrl, 
        data: item.imageUrl, 
        objectId: _info.objectId, 
        resize: _info.resize, 
      };

      window.Asc.plugin.executeMethod( "AddOleObject",  [_param], function () {
        // 如果是最后一张图片,再关闭插件
        if (images.indexOf(item)  === images.length  - 1) {
          window.Asc.plugin.executeCommand("close",  "");
        }
      });
    });
  }

2:请更详细地描述您的场景
当编辑word的时候,需要点击工具栏的按钮(插件里),弹出一个modal框,是一个资质列表(图片+图片名称),当我选中多个图片的时候,能把图片和文字插入到光标所在的位置,保存文档。
如果我按照以下代码写(不知道对不对),是可以插入一张图的,但是当我循环,也是只能插入一张图

  function insertImage(imageUrl) {
    var _info = window.Asc.plugin.info;
    var _param = {
      guid: _info.guid,
      widthPix: (_info.mmToPx * _info.width) >> 0,
      heightPix: (_info.mmToPx * _info.height) >> 0,
      width: _info.width ? _info.width : 100,
      height: _info.height ? _info.height : 70,
      imgSrc: imageUrl,
      data: imageUrl,
      objectId: _info.objectId,
      resize: _info.resize,
    };

    window.Asc.plugin.executeMethod( "AddOleObject", [_param], function () {
      window.Asc.plugin.executeCommand("close", "");
    });
  }

谢谢

对于这种方法,您不应该使用 executeMethod,而应该使用 callCommand:

(function (window, undefined) {
  window.Asc.plugin.init = function () {
    const array1 = [
      "https://link-to-image-1",
      "https://link-to-image-2",
    ];

    Asc.scope.imgSrc = array1;

    Asc.plugin.callCommand(() => {
      Asc.scope.imgSrc.forEach(
        (element) => {
          let doc = Api.GetDocument();
          let paragraph = doc.GetElement(0);
          let image = Api.CreateImage(element, 60 * 36000, 60 * 36000);
          paragraph.AddDrawing(image);
        }, true, true, console.log("Images inserted"));
    });
  };
})(window, undefined);

请查阅以下指南:

  1. CreateImage | ONLYOFFICE
  2. How to call commands | ONLYOFFICE
1 Like

万分感谢您的帮助,拯救了我

1 Like

不客气。如果还有其他任何问题,请随时向我们提问