javascript - 使用 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
}
解决方案
请使用以下代码:
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);
}
});
})
有关更多信息,请查看以下链接:
推荐阅读
- python - QueryDict 中的空列表实际上不为空
- java - 放心 - 如何放心地解析数组元素
- c# - 向上打开 wpf 菜单
- laravel - Laravel导入Excel,传递变量时出错
- elasticsearch - 在 Elassandra 中无法访问弹性搜索 url
- deep-learning - 使用不同的测量单位计算神经网络输出的损失 (MSE)
- html - 应用程序脚本 - 获取侧边栏信息
- reactjs - 带有功能组件的功能道具 - 不是功能(React + Typescript)
- c# - foreach 循环中的多线程
- docker - Docker secret 挂载一个空字符串