获取 PowerPoint 加载项上的当前选择

问题描述 投票:0回答:2

我正在开发一个用于 PowerPoint 的插件,用于在文档中插入图像。 我目前可以插入图像,但无法替换当前选定的图像。

例如,想象一下:

  1. 用户在空幻灯片内添加了加载项上可用的图像
  2. 然后,他选择了此图像,并希望将其替换为加载项上也提供的另一张图像。
  3. 他单击/选择幻灯片图像,然后单击加载项内他想要替换的图像。

问题是我无法通过 Office API 获取他的选择。 有人可以帮助我吗?

我当前的代码如下所示:

  const insertFile = (binaryStr: string) => {
    Office.context.document.setSelectedDataAsync(
      binaryStr,
      {
        coercionType: Office.CoercionType.Image,
      },
      (result) => {
        if (result.status === Office.AsyncResultStatus.Failed) {
          console.error(result.error.message);
        }
      }
    );
  };
powerpoint office-js office-addins powerpoint-addins
2个回答
0
投票

尝试使用 getSelectedDataAsync 方法来读取文档当前选择中包含的数据。


0
投票

另一种选择是以某种方式“标记”您的图像,以便您可以轻松地将它们取出以供以后管理。我正在使用标签。

使用

.setSelectedDataAsync(...)
插入图像后,新图像应该是选定的形状。因此,如果您获得选定的形状,您可以为其添加标签以便稍后管理。

const selectedShapes = context.presentation.getSelectedShapes();
  selectedShapes.load('items');
  context.sync().then(function () {
    if (selectedShapes.items.length > 0) {
      const shape = selectedShapes.items[0];
      shape.tags.add("tagName", "mytag");
    }
  })

然后您可以根据您添加的标签搜索形状:

await PowerPoint.run(async (context) => {
    const slides = context.presentation.slides.load("items");
    await context.sync();

    for (let i = 0; i < slides.items.length; i++) {
      const currentSlide = slides.items[i];
      const shapes = currentSlide.shapes.load("items");
      await context.sync();

      for (let shapeIndex = 0; shapeIndex < shapes.items.length; shapeIndex++) {
        const currentShape = shapes.items[shapeIndex];
        const tags = currentShape.tags.load("key, value");
        await context.sync();

        for (let tagIndex = 0; tagIndex < tags.items.length; tagIndex++) {
          const currentTag = tags.items[tagIndex];
          
          if (currentTag.key === "myTag") {
            ... do something here
          }
        }
      }
    }
© www.soinside.com 2019 - 2024. All rights reserved.