首页 > 解决方案 > 使用 vue 创建 Excel 插件时。如何根据 Excel 笔记本上的数据计算属性?

问题描述

我想获取工作表名称的列表,以便在我的插件中创建一个下拉列表。我尝试通过 aa 计算属性来实现,但在计算属性时无法运行异步函数来与 excel 交互。现在我在计算属性时调用了一个方法,但我认为它没有运行 excel.run 中的代码。在下面的代码中,只有 a 和 d 被推送到数组中。

computed: {
    formats: function () {
      return this.getSheetNames()
    }
  },
  methods: {
    getSheetNames () {
      var sheetNames = ['a']
      window.Excel.run(async (context, sheetNames) => {
        let promise = Promise.resolve(['b'])
        let list = await promise
        sheetNames.push(list)
        sheetNames.push('c')
      })
      sheetNames.push('d')
      return sheetNames
    }

标签: javascriptexcelsharepointoffice365office-js

解决方案


请使用以下代码:

Window.Excel.run(async(context) {
    var sheets = context.workbook.worksheets;
    sheets.load("items/name");
    var sheetNames = [];
    return context.sync()
        .then(function () {
            if (sheets.items.length > 1) {
                console.log(`There are ${sheets.items.length} worksheets in the workbook:`);
            } else {
                console.log(`There is one worksheet in the workbook:`);
            }
            for (var i in sheets.items) {
                sheetNames.push(sheets.items[i].name); 

            }
        });
})

有关更多信息,请查看以下链接:

获取工作表


推荐阅读