Search code examples
javascriptpowerpointoffice-jsslidebeta

MS PowerPoint JavaScript API BETA - How can I insert a new slide with insertSlidesFromBase64(base64File, options)?


I am experimenting with the JavaScript APIs in beta/preview for a MS PowerPoint Add-in. What I want to achieve is to insert a new slide from a base64-encoded .pttx file into the current document.

I would expect that this is possible with the insertSlidesFromBase64(base64File, options) method, which is documented here: PowerPoint API doc

I have included https://appsforoffice.microsoft.com/lib/beta/hosted/office.js in the Add-in

I am working on Mac OS 10.15.7

I have updated PowerPoint to the newest version in the Beta Channel. The PowerPoint Version is 16.44 (20111100).

Now, I am not quite sure if the beta API Methods are actually available in my environment. The bigger issue I am facing though is, that I don't know on which object I can call this method. I think the method should be available somewhere in the context of the current document/presentation?!?

I think a very simple example of how I can insert a "base64EncodedPptx" with insertSlidesFromBase64("base64EncodedPptx") would solve the problem.


Solution

  • Your Mac PowerPoint version should have the implementation of this API.

    In terms of very simple usage, here are some code snipplets:

        await PowerPoint.run(async function(context) {
          context.presentation.insertSlidesFromBase64( base64EncodedPptxFileAsString );
          context.sync();
        });
    
        await PowerPoint.run(async function (context) {
          context.presentation.insertSlidesFromBase64( base64EncodedPptxFileAsString,
            {
              formatting: "UseDestinationTheme",
              targetSlideId: "257#",
              sourceSlideIds: ["257#3396654126", "258#"]
            });
          context.sync();
        });
    

    From javascript side, you can use a file picker for example to get the base64 string: If you have this in HTML

        <form>
            <input type="file" id="file" />
        </form>
    

    and this in the script:

    $("#file").change(() => tryCatch(useInsertSlidesApi));
    
    async function useInsertSlidesApi() {
      const myFile = <HTMLInputElement>document.getElementById("file");
      const reader = new FileReader();
    
      reader.onload = async (event) => {
        // strip off the metadata before the base64-encoded string
        const startIndex = reader.result.toString().indexOf("base64,");
        const copyBase64 = reader.result.toString().substr(startIndex + 7);
    
        await PowerPoint.run(async function(context) {
          context.presentation.insertSlidesFromBase64(copyBase64);
          context.sync();
        });
      };
    
      // read in the file as a data URL so we can parse the base64-encoded string
      reader.readAsDataURL(myFile.files[0]);
    }
    
    /** Default helper for invoking an action and handling errors. */
    async function tryCatch(callback) {
      try {
        await callback();
      } catch (error) {
        // Note: In a production add-in, you'd want to notify the user through your add-in's UI.
        console.error(error);
      }
    }