【发布时间】:2022-08-10 06:45:51
【问题描述】:
嗨,我是 JavaScript 新手,但仍然不知道为什么我的代码在之前的代码之前运行 \"console.log(\"ok\")\"。我已经阅读了很多文章并观看了一些视频,但仍然找不到答案。感谢你的帮助!
编辑: 这很有趣。我在代码中添加了一个新的 Promise,但在工作表插入完成之前 console.log 仍然启动。我可能需要构建另一个函数来让它工作
function importProjects() {
const myFiles = <HTMLInputElement>document.getElementById(\"file\");
var numberofFiles = myFiles.files.length;
for (let i = 0; i < numberofFiles; i++) {
new Promise(function(resolve){
let reader = new FileReader();
reader.onload = (event) => {
Excel.run((context) => {
// Remove the metadata before the base64-encoded string.
let startIndex = reader.result.toString().indexOf(\"base64,\");
let externalWorkbook = reader.result.toString().substr(startIndex + 7);
// Retrieve the current workbook.
let workbook = context.workbook;
// Set up the insert options.
let options = {
sheetNamesToInsert: [], // Insert all the worksheets from the source workbook.
positionType: Excel.WorksheetPositionType.after, // Insert after the `relativeTo` sheet.
relativeTo: \"Sheet1\" // The sheet relative to which the other worksheets will be inserted. Used with `positionType`.
};
// Insert the new worksheets into the current workbook.
workbook.insertWorksheetsFromBase64(externalWorkbook, options);
return context.sync();
});
};
// Read the file as a data URL so we can parse the base64-encoded string.
reader.readAsDataURL(myFiles.files[i]);
resolve()
}).then(function(){
setTimeout(function(){
console.log(\"ok\");
},2000)
})
}
}
标签: excel office-js office-addins excel-addins excel-web-addins