天天看点

Script Lab 05:Office JavaScript API助手,Excel 基础操作(3)准备工作OfficeHelpers【功能】【用法】【示例】【小技巧】后记

准备工作

上一期内容中,为了创建一个工作表,曾用到一个 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

【功能】

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)准备工作OfficeHelpers【功能】【用法】【示例】【小技巧】后记

【示例】

从功能性来讲,其最重要的功能之一便是认证(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, , 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 usingauthenticator.endpoints.registerMicrosoftAuth('client id here');
// register Azure AD 1.0 endpoint usingauthenticator.endpoints.registerAzureADAuth('client id here', 'tenant here');
// register Google endpoint usingauthenticator.endpoints.registerGoogleAuth('client id here');
// register Facebook endpoint usingauthenticator.endpoints.registerFacebookAuth('client id here');
// register any 3rd-Party OAuth Implicit Provider usingauthenticator.endpoints.add('Name of provider', { /* Endpoint Configuration - see office-js-helpers/src/authentication/endpoint.manager.ts */ })
// register Microsoft endpoint by overriding default valuesauthenticator.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)准备工作OfficeHelpers【功能】【用法】【示例】【小技巧】后记

后记

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

Script Lab 05:Office JavaScript API助手,Excel 基础操作(3)准备工作OfficeHelpers【功能】【用法】【示例】【小技巧】后记

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

继续阅读