我怎样才能使这个递归脚本更快?此代码中的 RNumber 通过公式在电子表格中计算,并在每次迭代后检查
我通常从 > 50000 的数字向下迭代,并且脚本运行非常缓慢。结果,我添加了一个可变迭代步骤,但这并不理想,并且仍然运行不快。
真的很感激一些帮助!
async function findMaxDrawdown(context: Excel.RequestContext, currentMonth: number ) {
var maxAvailableMonth;
var monthsCount;
var monthsRange;
var dscrThreshold = context.workbook.worksheets.getItem("DDSHEET1").getRange("B14");
await context.sync();
const end = 0;
var finalTestVariable = context.workbook.worksheets.getItem("DDSHEET2").getRange("B16")
var precisionFactor = context.workbook.worksheets.getItem("DDSHEET1").getRange("B18");
numberMonths = context.workbook.worksheets.getItem("DDSHEET1").getRange("B17")
months.load("values");
precisionFactor.load("values");
finalTestVariable.load("values");
dscrThreshold.load("values");
numberMonths.load("values");
await context.sync();
maxAvailableMonth = months.values[0][currentMonth - 1];
monthsCount = context.workbook.worksheets.getItem("DDSHEET1").getRange("B17");
monthsCount.load("values");
await context.sync();
console.log(currentMonth)
for (let i = maxAvailableMonth; i > -1; i-= precisionFactor.values[0][0]) {
if (currentMonth < numberMonths.values[0][0]-1) {
if( i < precisionFactor.values[0][0]){
i = 0
}
}
context.workbook.worksheets.getItem("DDSHEET2").getRange("D2").getOffsetRange(1, currentMonth).values = [
[i]
];
await context.sync();
var currentMonthRNumber = context.workbook.worksheets.getItem("DDSHEET2").getRange("D2").getOffsetRange(2, currentMonth);
currentMonthRNumber.load("values");
await context.sync();
if (currentMonthRNumber.values[0][0] >= rNumberThreshold.values[0][0]) {
if (currentMonth == monthsCount.values)
{
return;
}
else
{
await findMaxDrawdown(context, currentMonth + 1);
var finalTest = context.workbook.worksheets.getItem("DDSHEET2").getRange("D2").getOffsetRange(1, currentMonth + 1);
finalTest.load("values");
await context.sync();
finalTestVariable.load("values")
await context.sync();
if (finalTest.values[0][0] == finalTestVariable.values[0][0]){
continue
} else{
if (finalTest.values[0][0] > finalTestVariable.values[0][0]) {
return;
}
else {
}
}
break
}
}
else
{
continue;
}
}
}