PowerPoint 加载项

使用 PowerPoint 加载项,可以跨平台(包括 Windows、iPad、Mac 和浏览器)生成极具吸引力的解决方案,从而有效展示用户的演示文稿。 可以创建以下两种类型的 PowerPoint 加载项:

  • 使用内容外接程序向演示文稿添加动态 HTML5 内容。 有关示例,请参阅可用于将交互关系图从 LucidChart 插入面板的 PowerPoint 的 LucidChart 关系图外接程序。

  • 使用任务窗格加载项引入参考信息或通过服务将数据插入演示文稿。 有关示例,请参阅可用于在演示文稿中添加专业照片的 Pexels - 免费素材图片加载项。

PowerPoint 加载项方案

本文中的代码示例展示了开发 PowerPoint 加载项涉及的一些基本任务。 请注意以下几点:

  • 这些示例使用 app.showNotification 函数来显示信息,该函数包含在 Visual Studio Office 加载项项目模板中。 如果你没打算使用 Visual Studio 开发加载项,则需要将 showNotification 函数替换为你自己的代码。

  • 其中一些示例还使用 Globals 声明在以下函数范围之外的对象: var Globals = {activeViewHandler:0, firstSlideId:0};

  • 若要使用这些示例,您的加载项项目必须引用 Office.js v1.1 库或更高版本

检测演示文稿的活动视图并处理 ActiveViewChanged 事件

若要生成内容外接程序,则需要获取演示文稿的活动视图,并在 ActiveViewChanged 处理程序期间处理 Office.Initialize 事件。

注意

在 PowerPoint 网页版中,Document.ActiveViewChanged 事件永远不会触发,因为幻灯片放映模式被视为新会话。 在这种情况下,加载项必须在加载时提取活动视图,如下面的代码示例所述。

在以下代码示例中:

  • 函数 getActiveFileView 调用 Document.getActiveViewAsync 方法以返回演示文稿的当前视图是否 (可编辑幻灯片的任何视图(例如“ 普通 视图”或 “大纲视图) ”或“读取” (幻灯片放映阅读视图) )。

  • 函数 registerActiveViewChanged 调用 addHandlerAsync 方法来注册 Document.ActiveViewChanged 事件的处理程序。

//general Office.initialize function. Fires on load of the add-in.
Office.initialize = function(){

    //Gets whether the current view is edit or read.
    const currentView = getActiveFileView();

    //register for the active view changed handler
    registerActiveViewChanged();

    //render the content based off of the currentView
    //....
}

function getActiveFileView()
{
    Office.context.document.getActiveViewAsync(function (asyncResult) {
        if (asyncResult.status == "failed") {
            app.showNotification("Action failed with error: " + asyncResult.error.message);
        }
        else {
            app.showNotification(asyncResult.value);
        }
    });

}

function registerActiveViewChanged() {
    Globals.activeViewHandler = function (args) {
        app.showNotification(JSON.stringify(args));
    }

    Office.context.document.addHandlerAsync(Office.EventType.ActiveViewChanged, Globals.activeViewHandler,
        function (asyncResult) {
            if (asyncResult.status == "failed") {
                app.showNotification("Action failed with error: " + asyncResult.error.message);
            }
            else {
                app.showNotification(asyncResult.status);
            }
        });
}

在以下代码示例中,getSelectedRange 函数将调用 Document.getSelectedDataAsync 方法以获取 asyncResult.value 返回的 JSON 对象,其中包括一个名为 slides 的数组。 slides 数组包含所选范围内的幻灯片(或当前幻灯片,如果未选择多张幻灯片)的 ID、标题和索引。 此外,它会将所选范围内的第一张幻灯片的 ID 保存为全局变量。

function getSelectedRange() {
    // Get the id, title, and index of the current slide (or selected slides) and store the first slide id */
    Globals.firstSlideId = 0;

    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 = asyncResult.value.slides[0].id;
            app.showNotification(JSON.stringify(asyncResult.value));
        }
    });
}

在以下代码示例中,goToFirstSlide 函数将调用 Document.goToByIdAsync 方法,以导航至由之前显示的 getSelectedRange 函数标识的第一张幻灯片。

function goToFirstSlide() {
    Office.context.document.goToByIdAsync(Globals.firstSlideId, Office.GoToType.Slide, function (asyncResult) {
        if (asyncResult.status == "failed") {
            app.showNotification("Action failed with error: " + asyncResult.error.message);
        }
        else {
            app.showNotification("Navigation successful");
        }
    });
}

在以下代码示例中,goToSlideByIndex 函数将调用 Document.goToByIdAsync 方法,以导航至演示文稿中的下一张幻灯片。

function goToSlideByIndex() {
    const goToFirst = Office.Index.First;
    const goToLast = Office.Index.Last;
    const goToPrevious = Office.Index.Previous;
    const goToNext = Office.Index.Next;

    Office.context.document.goToByIdAsync(goToNext, Office.GoToType.Index, function (asyncResult) {
        if (asyncResult.status == "failed") {
            app.showNotification("Action failed with error: " + asyncResult.error.message);
        }
        else {
            app.showNotification("Navigation successful");
        }
    });
}

获取演示文稿的 URL

在以下代码示例中 getFileUrl ,函数调用 Document.getFileProperties 方法以获取演示文稿文件的 URL。

function getFileUrl() {
    //Get the URL of the current file.
    Office.context.document.getFilePropertiesAsync(function (asyncResult) {
        const fileUrl = asyncResult.value.url;
        if (fileUrl == "") {
            app.showNotification("The file hasn't been saved yet. Save the file and try again");
        }
        else {
            app.showNotification(fileUrl);
        }
    });
}

创建演示文稿

加载项可创建新的演示文稿,且与当前运行此加载项的 PowerPoint 实例分开。 PowerPoint 命名空间针对此目的提供了 createPresentation 方法。 调用此方法时,新的演示文稿将立即打开并在 PowerPoint 新实例中显示。 加载项保持打开状态,并随之前的演示文稿一起运行。

PowerPoint.createPresentation();

此外,createPresentation 方法还可创建现有演示文稿的副本。 此方法接受 .pptx 文件的 base64 编码字符串表示形式作为可选参数。 若字符串参数为有效的 .pptx 文件,则生成的演示文稿是该文件的副本。 可以使用 FileReader 类将文件转换为所需的 base64 编码字符串,如以下示例所示。

const myFile = document.getElementById("file");
const reader = new FileReader();

reader.onload = function (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);

    PowerPoint.createPresentation(copyBase64);
};

// read in the file as a data URL so we can parse the base64-encoded string
reader.readAsDataURL(myFile.files[0]);

另请参阅