我需要让index_to_write变量在函数之外可见。我怎样才能做到这一点?
const XlsxPopulate = require('xlsx-populate');
let found = false;
let i = 1;
let index_to_write = 0;
XlsxPopulate.fromFileAsync("todo-list.xlsx")
.then(workBook => {
while (i <= 10 && found === false){
const value = workBook.sheet("Do-Shopping").cell("A" + i.toString()).value();
if (value === undefined) {
found = true;
index_to_write = i.toString();
}
i++;
}
});
console.log("A " + index_to_write + " is the first one to be free of data");
我试图在return index_to_write;下面添加,index_to_write = i.toString();但它不能解决问题。
目前显示为0,和初始化一样,但应该打印4。如何修复它?
非常感谢您的先进!