Office アドイン

[Officeアドイン]ワークシートで選択範囲を変更したときに発生するイベント

ワークシート上で選択範囲の変更を検知する際、VBAでは通常「Worksheet.SelectionChange」イベントを利用します。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Debug.Print Target.Address
End Sub

Office アドインでも同様に、イベントハンドラを追加することによって、選択範囲の変更を検知できるようになります。

マニフェストファイル(manifest.xml)

<?xml version="1.0" encoding="UTF-8"?>
<OfficeApp xmlns="http://schemas.microsoft.com/office/appforoffice/1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="TaskPaneApp">
  <Id>c578d697-f2bc-4eea-9d44-68a35fc24d39</Id>
  <Version>1.0</Version>
  <ProviderName>@kinuasa</ProviderName>
  <DefaultLocale>ja-jp</DefaultLocale>
  <DisplayName DefaultValue="イベントサンプル" />
  <Description DefaultValue="Work with Events using the Excel JavaScript API"/>
  <Hosts>
    <Host Name="Workbook" />
  </Hosts>
  <DefaultSettings>
    <SourceLocation DefaultValue="https://localhost/apps/test0301/index.html" />
  </DefaultSettings>
  <Permissions>ReadWriteDocument</Permissions>
</OfficeApp>

アドイン本体(index.html)

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <title>Sample</title>
    <link rel="stylesheet" href="https://static2.sharepointonline.com/files/fabric/office-ui-fabric-js/1.4.0/css/fabric.components.min.css">
    <style>
      #content-main {
        background: #ffffff;
        position: fixed;
        top: 200px;
        left: 0;
        right: 0;
        bottom: 0;
        overflow: auto;
      }
    </style>
    <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.3.1.min.js"></script>
    <!-- <script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script> -->
    <script src="https://appsforoffice.microsoft.com/lib/beta/hosted/office.js"></script>
    <script src="https://unpkg.com/core-js/client/core.min.js"></script>
    <script src="https://unpkg.com/@microsoft/office-js-helpers@0.7.4/dist/office.helpers.min.js"></script>
    <script src="https://static2.sharepointonline.com/files/fabric/office-ui-fabric-js/1.4.0/js/fabric.min.js"></script>
    <script>
      var eventResult;
      
      Office.initialize = function(reason){
        $(document).ready(function(){
          $("#btn1").click(regOnSelectionChanged);
          $("#btn2").click(removeOnSelectionChanged);
        });
      };
       
      function regOnSelectionChanged(){
        Excel.run(function(context){
          var sht = context.workbook.worksheets.getActiveWorksheet();
          eventResult = sht.onSelectionChanged.add(sht_onSelectionChanged);
          return context.sync().then(function(){
            OfficeHelpers.UI.notify("", "イベントハンドラの追加成功", "success");
          });
        }).catch(function(error){
          OfficeHelpers.UI.notify(error);
        });
      }
      
      function removeOnSelectionChanged(){
        return Excel.run(eventResult.context, function(context){
          eventResult.remove();
          return context.sync().then(function(){
            eventResult = null;
            OfficeHelpers.UI.notify("", "イベントハンドラの削除成功", "success");
          });
        }).catch(function(error){
          OfficeHelpers.UI.notify(error);
        });
      }
      
      function sht_onSelectionChanged(event){
        return Excel.run(function(context){
          return context.sync().then(function(){
            OfficeHelpers.UI.notify("", "type:" + event.type + 
                                        "\naddress:" + event.address + 
                                        "\nworksheetId:" + event.worksheetId, "success");
          });
        }).catch(function(error){
          OfficeHelpers.UI.notify(error);
        });
      }
    </script>
  </head>
  <body>
    <div id="content-main">
      <button id="btn1" class="ms-Button">
        <span class="ms-Button-label">Reg onSelectionChanged</span>
      </button>
      <button id="btn2" class="ms-Button">
        <span class="ms-Button-label">Remove onSelectionChanged</span>
      </button>
    </div>
  </body>
</html>

追加したイベントハンドラは、上記コードのようにremoveメソッドで削除、もしくはアドインを終了した際に破棄されるため、ファイルにイベントが保持されることはありません。

参考Webページ

2018年2月の人気記事前のページ

Nintendo Switch「スナックワールド トレジャラーズ ゴールド」に3DS版からデータ引継ぎはできるの?次のページ

関連記事

  1. Office関連

    PowerPointスライドショー終了後ファイルを閉じるVBAマクロ

    「Excel VBA PowerPoint スライドショー後閉じる」と…

  2. Office関連

    インストールされたフォントの一覧を取得するVBAマクロ

    最近自分の周りでPowerPoint VBAが流行っているようだったの…

  3. Office関連

    Office 365 Soloをインストールしてみました。

    2014年10月17日、Microsoftの新しいOffice製品「O…

  4. Office関連

    ソースコードを番号行付きのテーブルに変換するWordマクロ

    Word文書内のソースコードを、他の文書と区別して目立たせたいときに役…

  5. Office アドイン

    Global Microsoft 365 Developer Bootcamp 2020 TOKYO…

    2020年11月9日(月)と10日(火)の二日間、Microsoft …

コメント

  • コメント (0)

  • トラックバックは利用できません。

  1. この記事へのコメントはありません。

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP