Script Lab 05:Office JavaScript API助手,Excel 基础操作(3)

准备工作

上一期内容中,为了创建一个工作表,曾用到一个 forceCreateSheet 的函数,是由 OfficeHelpers  命名空间下的 ExcelUtilities 类所提供的。本期我们就来讲讲 OfficeHelpers  的故事,这是 OfficeJS 提供的一个 Office JavaScript API 助手(office-js-helpers),用以简化 WebAdd-ins 的开发

这是一个开源项目,官网地址如下:

https://github.com/OfficeDev/office-js-helpers

Script Lab 05:Office JavaScript API助手,Excel 基础操作(3)


OfficeHelpers

【功能】

OfficeHelpers 命名空间下,提供了一组帮助程序,用于简化 Office 加载项和 Microsoft Teams 选项卡的开发。这些帮助程序将功能作为存储管理,身份验证,对话和其他有用的实用程序等,比如最常用的错误记录:



OfficeHelpers.UI.notifyerror);
OfficeHelpers.Utilities.log(error);

认证主要包括了以下五个模块:

  1. 认证
  2. 对话框
  3. 错误记录
  4. 存储助手
  5. 字典


【用法】

在资源库中,引用以下代码:


    
    https://appsforoffice.microsoft.com/lib/1/hosted/office.js
    @types/office-js

    Script Lab 05:Office JavaScript API助手,Excel 基础操作(3)


    【示例】

    从功能性来讲,其最重要的功能之一便是认证(Authenticator)。Authentication helper 是为符合标准的 OAuth Implicit Flow 而构建的。开箱即用,可以方便接入 Microsoft、AzureAD、Google 和 Facebook 等身份验证的集成。以 Microsoft 认证为例:

    
    $("#run").click(() => tryCatch(run));
    
    async function run() {
        await Excel.run(async (context) => {
            //
            var auth = new OfficeHelpers.Authenticator();
            //
            auth.endpoints.registerMicrosoftAuth('6bab39d1-c5a8-4da9-90f9-66f358362e50', {
                redirectUrl: 'https://script-lab.azureedge.net',
                scope: 'api://6bab39d1-c5a8-4da9-90f9-66f358362e50/access_as_user'
            });
            //
            auth.authenticate(OfficeHelpers.DefaultEndpoints.Microsoft)
                .then(tokenHandler)
                .catch(OfficeHelpers.Utilities.log);
            await context.sync();
        });
    }
    
    function tokenHandler(token: OfficeHelpers.IToken) {
        console.log(JSON.stringify(token, null, 4));
    }
    
    async function tryCatch(callback) {
        try {
            await callback();
        }
        catch (error) {
            OfficeHelpers.UI.notify(error);
            OfficeHelpers.Utilities.log(error);
        }
    }


    其它可用认证的示例,还包括了:

    // register Microsoft (Azure AD 2.0 Converged auth) endpoint using
    authenticator.endpoints.registerMicrosoftAuth('client id here');
    
    
    // register Azure AD 1.0 endpoint using
    authenticator.endpoints.registerAzureADAuth('client id here', 'tenant here');
    
    
    // register Google endpoint using
    authenticator.endpoints.registerGoogleAuth('client id here');
    
    
    // register Facebook endpoint using
    authenticator.endpoints.registerFacebookAuth('client id here');
    
    
    // register any 3rd-Party OAuth Implicit Provider using
    authenticator.endpoints.add('Name of provider', { /* Endpoint Configuration - see office-js-helpers/src/authentication/endpoint.manager.ts */ })
    
    
    // register Microsoft endpoint by overriding default values
    authenticator.endpoints.registerMicrosoftAuth('client id here', {
        redirectUrl: 'redirect url here',
        scope: 'list of valid scopes here'
    });


    【小技巧】

    gist.github.com 被墙无法访问解决办法:

    windows下 打开C:\Windows\System32\drivers\etc\hosts文件   

    编辑器打开,在最后行添加192.30.253.118 gist.github.com


    Script Lab 05:Office JavaScript API助手,Excel 基础操作(3)


    后记

    小技巧中提到一个方法,此法小编亲自测试有效,未来给大家分享代码以及方便获取代码都用得上。这个还挺重要的,这为以后使用和建立代码共享机制,提供了一个方向。小编未来可以很方便的把教程相关的代码,直接分享到GITHUB上,方便大家一键获到(通过一个gist链接即可)。

    Script Lab 05:Office JavaScript API助手,Excel 基础操作(3)

    从今天开始,代码和相关资源,都将分享在以下的QQ学习群上,方便大家自取。而代码 yaml 格式进行分享。


     目录索引



    Script Lab 01:快速 Office 365 开发工具

    Script Lab 02:Script Lab,知识储备

    Script Lab 03:Script Lab,启动函数,Excel 基础操作(1)

    Script Lab 04:Script Lab,九九乘法表,Excel 基础操作(2)

    Script Lab 05:Office JavaScript API助手,Excel 基础操作(3)

    Script Lab 06:事件处理,Excel 基础操作(4)

    Script Lab 07:单词“卡拉OK”,Word 基础操作

    Script Lab 08:异步调用函数,PowerPoint 基础操作

    Script Lab 09:为 Officejs 开发配置 VSCode 环境

    Script Lab 10:OIfficeJS 的三种调试方式

    上一篇:使用WebGL + Three.js制作动画场景


    下一篇:【Java摸底自测】10道题目,测测你对Java基础知识掌握了多少