OfficeDev / office-js

A repo and NPM package for Office.js, corresponding to a copy of what gets published to the official "evergreen" Office.js CDN, at https://appsforoffice.microsoft.com/lib/1/hosted/office.js.
https://learn.microsoft.com/javascript/api/overview
Other
679 stars 95 forks source link

How to make slide active Or How to insert image using index #4755

Open Jayshiv1408Dev opened 2 months ago

Jayshiv1408Dev commented 2 months ago

My Environment Platform : PC desktop Host: Power Point Office version number: Microsoft® PowerPoint® for Microsoft 365 MSO (Version 2404 Build 16.0.17531.20140) 64-bit Operating System: Windows 11 pro

I want to redirect to slide base on index number. Currently I'm on slide 10(index: 9) and want to jump on slide 1(index: 0) Is there way I can achieve it? Base on this Other Problem is also solved. Here is my use case for redirect issue.

Inserting Image using this code snippet.

Office.context.document.setSelectedDataAsync(base64string, {
      coercionType: Office.CoercionType.Image
  }, function (asyncResult) {
      if (asyncResult.status === Office.AsyncResultStatus.Failed) {
          console.log("Error", asyncResult.error.message);
          showNotification("Error", asyncResult.error.message);
      }
  });

This Code inserting image on Active slide.I want to Insert image on other slide base on slide index. Is there way I can insert image on slide using Index number Or I can make that slide active(Selected) and Then insert image on it.

EsterBergen commented 2 months ago

HI @Jayshiv1408Dev - have you tried this? https://learn.microsoft.com/en-us/office/dev/add-ins/powerpoint/powerpoint-add-ins#navigate-to-a-particular-slide-in-the-presentation

Jayshiv1408Dev commented 2 months ago

Thanks @EsterBergen I'll try this. https://learn.microsoft.com/en-us/javascript/api/office/office.document?view=common-js-preview#office-office-document-getselecteddataasync-member(1)

image

I check that earlier it's showing in preview stage.

Jayshiv1408Dev commented 2 months ago

HI @Jayshiv1408Dev - have you tried this? https://learn.microsoft.com/en-us/office/dev/add-ins/powerpoint/powerpoint-add-ins#navigate-to-a-particular-slide-in-the-presentation

I tried but it's not Redirecting. Here is my code snippet. If I'm making some mistake please let me know.

Here is my CDN

async function makeSlideActive1(slideIndex) {
    var Globals = { activeViewHandler: 0, firstSlideId: 0 }
    PowerPoint.run(async context => {
        const presentation = context.presentation;
        const slides = presentation.slides;

        slides.load("items");
        await context.sync();

        if (slideIndex < 0 || slideIndex >= slides.items.length) {
            console.error("Slide index out of range.");
            return;
        }
        const slideId = slides.items[slideIndex].id;
        Globals.firstSlideId = slideId;
        Office.context.document.getSelectedDataAsync(Office.CoercionType.SlideRange, function (asyncResult) {
            if (asyncResult.status == "failed") {
                app.showNotification("Action failed with error: " + asyncResult.error.message);
            }
            else {
                Globals.firstSlideId = slideId;
                app.showNotification(JSON.stringify(asyncResult.value));
            }
        });
    });
}
EsterBergen commented 2 months ago

It should work if you follow this guidance @Jayshiv1408Dev .

https://learn.microsoft.com/en-us/office/dev/add-ins/powerpoint/powerpoint-add-ins#navigate-to-a-particular-slide-in-the-presentation