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
670 stars 96 forks source link

Different behavior between PowerPoint desktop and Online when creating a new slide and adding content to it #2903

Closed emiliodangelo closed 5 months ago

emiliodangelo commented 1 year ago

Message from office-js bot: We’re closing this issue because it has been inactive for a long time. We’re doing this to keep the issues list manageable and useful for everyone. If this issue is still relevant for you, please create a new issue. Thank you for your understanding and continued feedback.

Created a task pane PowerPoint Add-in which creates a new slide and add some text and images on it. The add-in is working as expected in PowerPoint desktop, but fails to render the same content when executed on PowerPoint Online. The add-in is a task pane project using react and TypeScript. The code used to create the slide and add the content is:

return PowerPoint.run(async (context) => {
  try {
    const presentation = context.presentation;

    // Add new slide
    presentation.slides.add();
    await context.sync();

    // If we wait a couple seconds here we avoid an error in PowerPoint Online
    // await sleep(2000);

    // Get the number of slides in presentation
    const slideCount = presentation.slides.getCount();
    await context.sync();

    // Get a reference to the last slide
    const slide = presentation.slides.getItemAt(slideCount.value - 1);
    slide.shapes.load("items/$none");
    await context.sync();

    // Delete all shapes in the slide
    slide.shapes.items.forEach((shape) => {
      shape.delete();
    });
    await context.sync();

    // Add title to slide
    const titleOptions: PowerPoint.ShapeAddOptions = {
      top: 25,
      left: 25,
      height: 50,
      width: 915,
    };
    const title = slide.shapes.addTextBox("Lorem ipsum dolor sit amet", titleOptions);
    title.textFrame.textRange.font.name = "Calibri Light";
    title.textFrame.textRange.font.size = 28;
    title.textFrame.autoSizeSetting = PowerPoint.ShapeAutoSize.autoSizeTextToFitShape;

    // Add description to slide
    const descriptionOptions: PowerPoint.ShapeAddOptions = {
      top: 80,
      left: 330,
      height: 415,
      width: 610,
    };
    const description = slide.shapes.addTextBox("Nam tristique ... tempor vestibulum.", descriptionOptions);
    description.textFrame.textRange.font.name = "Calibri Light";
    description.textFrame.textRange.font.size = 16;
    description.textFrame.autoSizeSetting = PowerPoint.ShapeAutoSize.autoSizeTextToFitShape;

    // Go to the last slide
    Office.context.document.goToByIdAsync(Office.Index.Last, Office.GoToType.Slide, async () => {
      const pictureOptions = {
        coercionType: Office.CoercionType.Image,
        imageTop: 80,
        imageLeft: 25,
        imageWidth: 300,
      };

      // Add a picture in the selected slide
      Office.context.document.setSelectedDataAsync(
        "PD94bWwgdm ... 3ZnPgo=",
        pictureOptions
      );
    });
  } catch (error) {
    console.error(error);
  }
});

Your Environment

Expected behavior

When the add- in is executed in PowerPoint desktop, the slide is created, the content is added to the new slide, and the slide is selected:

PowerPoint desktop expected result

Current behavior

When the add-in is executed in PowerPoint Online, the slide is created, no text is added, and the image is added to the first slide:

PowerPoint Online current result without wait

Also, the following error is captured in the console:

RichApi.Error: InvalidParam passed to GetItem(id)
    at new n (powerpoint-web-16.00.js:26:304803)
    at r.processRequestExecutorResponseMessage (powerpoint-web-16.00.js:26:369137)
    at powerpoint-web-16.00.js:26:367199

If a wait is performed right after the slide creation the text is correctly rendered, but the image is still being added to the first slide:

PowerPoint Online current result with wait

Steps to reproduce

  1. Clone the powerpoint-addin-example repository.
  2. Execute the server part of the add-in with npm run dev-server
  3. Execute the add-in in PowerPoint Online with npm run start:web -- --document <URL of a PowerPoint document in OneDrive>
  4. Show the task pane and click on > Run

Link to live example(s)

An example is available at emiliodangelo/powerpoint-addin-example repo.

RuizhiSunMS commented 1 year ago

@ononder Could you give a hand?