MS PowerPoint JavaScript API 测试版 - 如何使用 insertSlidesFromBase64(base64File, options) 插入新幻灯片?

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

我正在对 beta/preview 中的 JavaScript API 进行 MS PowerPoint 加载项试验。 我想要实现的是将 base64 编码的 .pttx 文件中的新幻灯片插入到当前文档中。

我希望使用 insertSlidesFromBase64(base64File, options) 方法可以做到这一点,该方法记录在此处: PowerPoint API doc

我已将 https://appsforoffice.microsoft.com/lib/beta/hosted/office.js 添加到插件中

我正在 Mac OS 10.15.7

我已将 PowerPoint 更新到 Beta 频道中的最新版本。 PowerPoint 版本为 16.44 (20111100)。

现在,我不太确定 beta API 方法在我的环境中是否真的可用。 不过,我面临的更大问题是,我不知道可以在哪个对象上调用此方法。我认为该方法应该在当前 document/presentation?!?

的上下文中的某个地方可用

我想一个非常简单的例子,说明如何插入“base64EncodedPptx” insertSlidesFromBase64("base64EncodedPptx") 会解决问题。

您的 Mac PowerPoint 版本应该有此 API 的实现。

就非常简单的用法而言,这里有一些代码片段:

    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();
    });

从 javascript 端,您可以使用文件选择器来获取 base64 字符串: 如果你在 HTML

中有这个
    <form>
        <input type="file" id="file" />
    </form>

脚本中的这个:

$("#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);
  }
}