[PowerPoint javascript API] Add event listener for objects removed from slide

[PowerPoint javascript API] Add event listener for objects removed from slide
8

Upvotes

Upvote

 Sep 21 2023
1 Comments (1 New)
New

With the current version of Office js API it is not possible to track whether an object was removed from the slide.

Would it be possible to have an event listener triggered when object is removed from the slide or when slide itself is removed? Ideally this event should return data about removed object.

use case: User removes image directly from the slide, currently corresponding object in the add-in will still be present as we are not able to detect removed objects

Comments
Copper Contributor

Although there is nothing in office.js that supports this directly, you should be able to achieve what you need by using 

Office.context.document.addHandlerAsync(
Office.EventType.DocumentSelectionChanged
 
This allows you to run a function every time a selection change happens. If your add-in keeps track of all shapes (using 
const slide = context.presentation.slides.getItemAt(<currently selected slide index);
const shapes = slide.shapes;
// Load all the shapes in the collection without loading their properties.
shapes.load("items/Name");
) you can detect a removal by comparing to the previous set of shapes. As part of that comparison you can then get the data of the removed shape (for example its Name in the example above)