PowerPoint: add base64 image in shape

PowerPoint: add base64 image in shape
3

Upvotes

Upvote

 Jun 22 2022
1 Comments (1 New)
New

As in Excel Js, I need to add a base64 image into shape and set the name, alternate text, and size. I tried to use setSelectedDataAsync, but it is very limited and it is not possible to get the selected shape. 

Comments
Brass Contributor

It would be helpful if the setSelectedDataAsync method would return the id of the new inserted image.  As a work around, I've created this helper method for PowerPoint that compares arrays of slide ids before and after inserting the drawing, the difference is the id of the new shape...

    async #insertDrawing(slideId, svgString, rectangle) {
        const asyncContext = await SlideHelper.getShapes(slideId, "id");
        return new Promise((resolve, reject) => {
            Office.context.document.setSelectedDataAsync(
                svgString,
                {
                    coercionType: Office.CoercionType.XmlSvg,
                    imageLeft: rectangle.left,
                    imageTop: rectangle.top,
                    imageWidth: rectangle.width,
                    imageHeight: rectangle.height,
                    asyncContext: asyncContext,
                },
                async (asyncResult) => {
                    if (asyncResult.status === Office.AsyncResultStatus.Failed) {
                        reject(asyncResult.error);
                    }
                    else {
                        // Our Async content contains an array of shapes before inserting the Drawing
                        const shapesOriginal = asyncResult.asyncContext;

                        // Let's get the list of shapes in our drawing after the insert
                        const shapesNew = await SlideHelper.getShapes(slideId, "id");

                        // Find the "difference", ie the new drawing shape!
                        let difference = shapesNew.filter(x => !shapesOriginal.some(y => y.id === x.id));

                        var toResolve = undefined;

                        if (difference !== undefined && difference.length === 1) {
                            // We found it, let's return our new shape id...
                            toResolve = difference[0].id;
                        }

                        resolve(toResolve);
                    }
                }
            );
        });
    }

Ideally, I would prefer some kind of identifier returned of the new inserted image.

 

Cheers!

 

Troy