1. 程式人生 > >基於Web技術的Outlook Add-ins開發簡介

基於Web技術的Outlook Add-ins開發簡介

繼續 self 其中 ref -i sed packages cmd default

基於Web技術的Outlook Add-ins開發簡介

  我也是剛剛接觸Outlook Add-ins的開發,水平有限;文中若有錯誤,拍磚請輕一些!

零、關於Outlook Add-ins

  Outlook插件的種類不止一種,早期的有基於COM技術的、基於VSTO(Visual Studio Tools for Office)的,還有就是這裏介紹的基於Web技術實現的方式。微軟的官網上將其稱為:Outlook add-ins。這種插件好處:

  (1) 跨平臺。開發一次,可能用於Outlook的Windows版、Mac版、手機版、以及Web(Office 365 和 Outlook.com)版。

  (2) 安裝方便靈活。可以本地安裝,也可以從Office store上一鍵安裝。註意:微軟現在搞了一個Office store,這個市場將來也許會成為風口哦。風口,風口,風口;重要的事情說三遍。

  (3) 開發相對(COM插件)簡單。工程由manifest.xml、JavaScript、css、Html構成;換句話說,這種插件是B/S結構的,因此正式發布的插件,必須要有一個Web Server與前端協作。下面的圖片基本可以說明這種插件的結構:

  技術分享圖片

  需要註意的是:

  [1] 這種插件並不安裝在本地,而存在於雲端。

  [2] 並非所有客戶端都同時支持最新的功能。目前Outlook 2013 及更高版本、Outlook 2016 for Mac、Outlook Web App (Exchange 2013)、Outlook 網頁版(Office 365 和 Outlook.com)均支持 Outlook Add-ins。

一、 Outlook Add-ins初探

下面是Office Store的URL,大家可以安裝幾個看看, 目前大多數插件是免費的,收費的不多。

https://appsource.microsoft.com/zh-cn/marketplace/apps?product=outlook&page=1&src=office

我看找到幾個比較有意思的插件:

  [1] 對郵件進行安全掃描的:

  https://appsource.microsoft.com/en-us/product/office/WA104381359?src=office&tab=Overview

技術分享圖片

  [2] 翻譯郵件的:

  https://appsource.microsoft.com/en-us/product/office/WA104124372?src=office

技術分享圖片

  [3] 預定星巴克場地的:

  https://appsource.microsoft.com/en-us/product/office/WA104380233?src=office&tab=Overview

技術分享圖片

安裝完後郵件工具欄上會出現相應的圖標。插件的使用非常簡單:打開一封郵件後,單擊某個圖標,右側會自動彈出一個窗口,顯示相關內容。這裏以一款反病毒插件為例,首先打開可疑的郵件,然後點擊圖標進行掃描,幾秒種後返回該郵件中URL是否安全。

技術分享圖片

右側的小窗口是插件的工作區,可以在其中顯示與插件相關的內容,如等待動畫、結果等等。多說一句,這個反病毒插件似乎還有點用,界面也挺漂亮,可以避免亂點URL中招;就是這個“TrendMicro”沒怎麽聽過,估計是個創業公司;要是大廠商出的就更讓人放心了!

二、開發實例

若要開發Outlook 插件需要了解Javascript & JQuery & Html & XML。這個例子來自於微軟的官網,我實踐後加入了自己的理解。本例功能很簡單,就是獲取某封郵件的信息,如發件人、ID等。

3.1 開發環境搭建

l 安裝 Node.js, 不會裝的自行百度

l 安裝Yeoman : npm install -g yo generator-office

3.2 開發過程

第一步:創建一個文件夾,並以管理員權限打開CMD,進入該目錄

第二步:使用Yeoman生成器創建一個Outlook add-in project

技術分享圖片

註意:該cmd shell要有管理員權限! CMD中全部內容如下:

H:\>cd H:\myWorkProj\test\my-outlook-addin

H:\myWorkProj\test\my-outlook-addin>

H:\myWorkProj\test\my-outlook-addin>yo office

_-----_ ╭──────────────────────────╮

| | │ Welcome to the Office │

|--(o)--| │ Add-in generator, by │

`---------′ │ @OfficeDev! Let‘s create │

( _′U`_ ) │ a project together! │

/___A___\ /╰──────────────────────────╯

| ~ |

__‘.___.‘__

′ ` |° ′ Y `

? Would you like to create a new subfolder for your project? No

? What do you want to name your add-in? My Office Add-in

? Which Office client application would you like to support? Outlook

? Would you like to create a new add-in? Yes, I need to create a new web app and manifest file for

my add-in.

? Would you like to use TypeScript? No

? Choose a framework: Jquery

For more information and resources on your next steps, we have created a resource.html file in your project.

? Would you like to open it now while we finish creating your project? Yes

----------------------------------------------------------------------------------

Creating My Office Add-in add-in using js and jquery

----------------------------------------------------------------------------------

create package.json

create my-office-add-in-manifest.xml

create app.css

create assets\icon-16.png

create assets\icon-32.png

create assets\icon-80.png

create assets\logo-filled.png

create bsconfig.json

create function-file\function-file.html

create function-file\function-file.js

create app.js

create index.html

create resource.html

I‘m all done. Running npm install for you to install the required dependencies. If this fails, try running the command yourself.

npm WARN deprecated [email protected]: express 2.x series is deprecated

npm WARN deprecated [email protected]: connect 1.x series is deprecated

npm notice created a lockfile as package-lock.json. You should commit this file.

npm WARN [email protected] No description

npm WARN [email protected] No repository field.

npm WARN [email protected] No license field.

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\fsevents):

npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

added 438 packages in 66.034s

第三步:修改默認代碼和配置

[1] 在代碼編輯器(如Notepad++)中,打開項目根目錄中的 index.html。 index.html決定outlook右側窗口的內容。將 <body> 元素內的 <header> 和 <main> 替換為以下標記並保存該文件。

<div class="ms-Fabric content-main">

<h1 class="ms-font-xxl">Message properties</h1>

<table class="ms-Table ms-Table--selectable">

<thead>

<tr>

<th>Property</th>

<th>Value</th>

</tr>

</thead>

<tbody class="prop-table"/>

</table>

</div>

[2] 在項目的根目錄中找到app.js編輯,用以下代碼替換全部內容,然後保存該文件。

‘use strict‘;
 
(function () {
 
  // The initialize function must be run each time a new page is loaded
  Office.initialize = function (reason) {
    $(document).ready(function () {
      loadItemProps(Office.context.mailbox.item);
    });
  };
 
  function loadItemProps(item) {
    // Get the table body element
    var tbody = $(‘.prop-table‘);
 
    // Add a row to the table for each message property
    tbody.append(makeTableRow("Id", item.itemId));
    tbody.append(makeTableRow("Subject", item.subject));
    tbody.append(makeTableRow("Message Id", item.internetMessageId));
    tbody.append(makeTableRow("From", item.from.displayName + " &lt;" +
      item.from.emailAddress + "&gt;"));
  }
 
  function makeTableRow(name, value) {
    return $("<tr><td><strong>" + name + 
      "</strong></td><td class=\"prop-val\"><code>" +
      value + "</code></td></tr>");
  }
 
})();

[3] 在項目的根目錄中打開 app.css,用以下代碼替換全部內容,然後保存該文件。

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}
 
td.prop-val {
    word-break: break-all;
}
 
.content-main {
    margin: 10px;
}

[4] 修改manifest.xml

  1. 打開 my-office-add-in-manifest.xml 文件。
  2. ProviderName 元素具有占位符值。 將其替換為你的姓名。
  3. Description 元素的 DefaultValue 屬性具有占位符。 將其替換為 My First Outlook Add-in。
  4. SupportUrl 元素的 DefaultValue 屬性具有占位符。 將其替換為 https://localhost:3000。

...
<ProviderName>Jason Johnston</ProviderName>
<DefaultLocale>en-US</DefaultLocale>
<!-- The display name of your add-in. Used on the store and various places of the Office UI such as the add-ins dialog. -->
<DisplayName DefaultValue="My Office Add-in" />
<Description DefaultValue="My First Outlook Add-in"/>
 
<!-- Icon for your add-in. Used on installation screens and the add-ins dialog. -->
<IconUrl DefaultValue="https://localhost:3000/assets/icon-32.png" />
<HighResolutionIconUrl DefaultValue="https://localhost:3000/assets/hi-res-icon.png"/>
 
<!--If you plan to submit this add-in to the Office Store, uncomment the SupportUrl element below-->
<SupportUrl DefaultValue="https://localhost:3000" />
...

三、安裝測試插件

  1. 在命令提示符/行中,確保自己處於項目的根目錄中,然後輸入 npm start。此操作將啟動 Web 服務器(地址為 https://localhost:3000)並在默認瀏覽器中打開該地址,確保正確啟動了web server。

  2. Outlook安裝插件。我用的是Outlook 2016, 安裝方式如下:

  技術分享圖片

  跳到瀏覽器後,點擊“+”選擇從文件加載,瀏覽器在未出現任何證書錯誤的情況下加載外接程序頁面後,加載 my-office-add-in-manifest.xml 文件。

技術分享圖片

  選擇manifest.xml後就按提示安裝

技術分享圖片

  安裝成功後Outlook工具欄上面會出現一個圖標。

  3. 使用時,打開一封 Outlook 中的郵件,點擊插件圖標。

技術分享圖片

  Microsoft Office 項目生成器生成的自簽名證書目前存在問題,這將導致瀏覽器報告外接程序站點不安全,並阻止插件在 Outlook 客戶端中加載。看到這個頁面不用管,點擊“Refresh the page”繼續。

技術分享圖片

  接下來點擊“Continue to this website …”:

技術分享圖片

  1. 接下來會自動顯示右側的任務窗格,我們可以看到當前郵件的信息:

技術分享圖片

到此結束!

基於Web技術的Outlook Add-ins開發簡介