注册 onSelectionChange

Registering onSelectionChange

最近几天我一直在尝试为 excel 制作一个基于 VUE 的任务面板插件。

我已按照 link 中的指南进行操作,并已尝试为 onSelectionChange 注册一个事件处理程序。它取得了一定的成功,但似乎在一段时间后对事件进行了排队。然后我必须将鼠标悬停在任务窗格上或执行类似 ctrl+c 的操作才能执行排队的代码。

我是 JS 和 Vue 的新手,所以很可能我错过了一个重要的上下文。

我的代码如下:

import Vue from 'vue'
import App from './App'

Vue.config.productionTip = false

/* eslint-disable no-new */

const Office = window.Office
Office.onReady()
  .then(
    registerSelectionChange(), // moved in here based on input from Rick
    new Vue({
      el: '#app',
      components: {App},
      template: '<App></App>'
    })
  )

async function registerSelectionChange() {
  return await Excel.run(async function (context) {
      let ws = context.workbook.worksheets.getActiveWorksheet();
      ws.onSelectionChanged.add(handleSelectionChange);
      return await context.sync()
  })
}

async function handleSelectionChange(event){
    return await Excel.run(async function(context){
        let ws = context.workbook.worksheets.getActiveWorksheet();
        let range = ws.getRange(event.address)
        range.values = [[1]]
        return await context.sync()
    })
}

由于registerSelectionChange()调用了Office API,你应该在Office.onReady中调用它。有关详细信息,请参阅 Initializing your add-in。请看看是否有帮助。

我现在发现 registerSelectionChange() 有问题。解决方法是在该部分中添加 <script> MutationObserver=null; </script>。 更多信息 here.