javascript - 如何获取当前打开的excel工作簿的本地路径?
问题描述
我在 Visual Studio 2017 中使用默认的“Excel Web 加载项”模板。我正在尝试创建一个 Excel 加载项,将现有工作簿的副本插入到当前工作簿中。第一步是获取当前工作簿的完整路径和名称。我从这里得到了代码。我正在使用 beta excel API。在'var myFile = document.getElementById("file");'这一行 myFile 始终为空。我认为空值是因为工作簿没有“加载”,但是当我运行程序时工作簿确实打开了。
这是来自 Home.js 的代码:
'use strict';
(function () {
Office.onReady(function () {
// Office is ready
$(document).ready(function () {
// The document is ready
$('#RunMacroButton').click(RunMacro);
});
});
function RunMacro() {
var myFile = document.getElementById("file");
var reader = new FileReader();
reader.onload = (function (event) {
Excel.run(function (context) {
// strip off the metadata before the base64-encoded string
var startIndex = event.target.result.indexOf("base64,");
var workbookContents = event.target.result.substr(startIndex + 7);
Excel.createWorkbook(workbookContents);
return context.sync();
}).catch(errorHandlerFunction);
});
// read in the file as a data URL so we can parse the base64-encoded string
reader.readAsDataURL(myFile.files[0]);
}
})();
解决方案
'var myFile = document.getElementById("file");' 行 始终为 null,因为没有称为“文件”的元素。这也是获取当前打开的工作簿路径的错误方式。而是使用“Office.context.document.url”返回路径。
推荐阅读
- python - 如何在Python中找到百分位数
- javascript - Node.js 未处理的拒绝类型错误:无法读取 Javascript 中未定义的属性“0”
- jquery - 为什么使用多个表单输入的 Dropzone 图像上传不起作用
- c# - 将数据传递到 excel - 通过 ASP.NET Web 应用程序的多个工作表
- swift - 理解 SwiftUI 中的 @Binding
- javascript - 尝试对数字输入求和时出现 NaN 错误
- c - 释放导致崩溃
- javascript - jQuery如何从焦点输入字段值中删除
- ios - tableView 单元格未显示单元格中的任何内容,但它正在控制台中打印内容
- ansible - ansible角色造成的模板错误如何解决