我正在开发一个用于 PowerPoint 的插件,用于在文档中插入图像。 我目前可以插入图像,但无法替换当前选定的图像。
例如,想象一下:
问题是我无法通过 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);
}
}
);
};
尝试使用 getSelectedDataAsync 方法来读取文档当前选择中包含的数据。
另一种选择是以某种方式“标记”您的图像,以便您可以轻松地将它们取出以供以后管理。我正在使用标签。
使用
.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
}
}
}
}