【问题标题】:Add an event handler to selected range将事件处理程序添加到选定范围
【发布时间】:2016-05-18 02:56:55
【问题描述】:

我想为用户在工作簿中选择的范围或单元格添加事件处理程序:如果用户选择另一个单元格或范围,则需要系统地执行一些操作。

我正在查看this reference,但我不知道在哪里将代码集成到整个加载项中。

例如,在我的Home.html 中,我有

    <div id="content-main">
        <div class = "padding" id="message"></div>
        <div class="padding">
            <button class="ms-Button" id="button">Click me!</button>
        </div>
    </div>

这里是Home.js

(function() {
    "use strict";

    Office.initialize = function(reason) {
        $(document).ready(function() {
            app.initialize();
            $('#button').click(activateEventHandler);
        });
    };

    function onBindingSelectionChanged(eventArgs) {
        write(eventArgs.binding.id + " has been selected.");
    }
    function write(message) {
        document.getElementById('message').innerText += message;
    }

    function activateEventHandler() {
        Excel.run(function(ctx) {
            var selectedRange = ctx.workbook.getSelectedRange();
            selectedRange.addHandlerAsync(Office.EventType.BindingSelectionChanged, onBindingSelectionChanged); 
            return ctx.sync()
        }).then(function() {
            console.log("done");
        }).catch(function(error) {
            ...
            }
        });
    }        
})();

我希望按钮激活监听器。然后,每次用户选择另一个单元格或范围时,都会系统地将一条消息附加到任务窗格中。但是,上面的代码引发了一个错误:

错误:TypeError:selectedRange.addHandlerAsync 不是函数

在线selectedRange.addHandlerAsync(...

实际上,我什至不确定这段代码应该是这样的结构......有谁知道如何修改代码来完成我的任务?

【问题讨论】:

    标签: javascript jquery office-js jquery-events


    【解决方案1】:

    错误的原因是您试图在 Range 对象上调用函数 addHandlerAsync。但处理程序只能添加到 Binding 对象或 Document 对象。您选择哪一个取决于您想要完成的任务。在您的情况下,我认为您不是在尝试在 特定 范围内侦听选择,而是实际上是在文档中侦听 anywhere 的选择。所以你实际上会使用DocumentSelectionChanged event:

    (function() {
        "use strict";
    
        Office.initialize = function(reason) {
            $(document).ready(function() {
                app.initialize();
                $('#button').click(activateEventHandler);
            });
        };
    
        function onMySelectionChanged(eventArgs) {
            write(eventArgs.binding.id + " has been selected.");
            doStuffWithNewSelection();
    
        }
        function write(message) {
            document.getElementById('message').innerText += message;
        }
    
        function activateEventHandler(){
            Office.context.document.addHandlerAsync(Office.EventType.DocumentSelectionChanged, onMySelectionChanged);
        }
    
        function doStuffWithNewSelection() {
            Excel.run(function(ctx) {
                var selectedRange = ctx.workbook.getSelectedRange();
                // do other stuff with the selection as you wish, like read and display
            }).then(function() {
                console.log("done");
            }).catch(function(error) {
                ...
                }
            });
        }        
    })();
    

    【讨论】:

      猜你喜欢
      • 2011-07-05
      • 2021-02-26
      • 2018-08-24
      • 2018-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      相关资源
      最近更新 更多