龙空技术网

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

硅创社 207

前言:

而今大家对“html助手”大体比较关注,看官们都想要知道一些“html助手”的相关资讯。那么小编在网络上网罗了一些对于“html助手””的相关文章,希望我们能喜欢,朋友们快快来学习一下吧!

准备工作

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

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

OfficeHelpers【功能】

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

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

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

认证对话框错误记录存储助手字典【用法】

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

【示例】

从功能性来讲,其最重要的功能之一便是认证(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: '', 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

后记

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

从今天开始,代码和相关资源,都将分享在以下的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 的三种调试方式

标签: #html助手