调用UDF异步功能?

问题描述 投票:0回答:1

如何在Excel电子表格中称呼它?里克在下面为我提供了以下代码,但是我怎么称呼它呢?我正在尝试VLOOKUP numOfParts(在参数中要求)并返回finalInspectionPerPart。

async function inspectionMins(numOfParts) {

  try {
    let finalInspectionPerPart;
    const procEff = 0.72;

    await Excel.run(async function (context) {
      const range = context.workbook.worksheets.getItem("Sheet1").getRange("A1:B9");
      finalInspectionPerPart = context.workbook.functions.vlookup(numOfParts, range, 2, false);
      finalInspectionPerPart.load('value');
      await context.sync();
      return (finalInspectionPerPart.value / procEff);
    })
  } catch (error) {
    // handle error
  };
}

enter image description here

excel typescript user-defined-functions office-js
1个回答
1
投票

很难处理您的代码,因为我们没有A1:B9的示例数据。作为尝试使用ES6语法进行的首次尝试,请尝试:

async function inspectionMins(numOfParts) {

  try {
    let finalInspectionPerPart;
    const procEff = 0.72;

    await Excel.run(async function (context) {
      const range = context.workbook.worksheets.getItem("Sheet1").getRange("A1:B9");
      finalInspectionPerPart = context.workbook.functions.vlookup(numOfParts, range, 2, false);
      finalInspectionPerPart.load('value');

      await context.sync();
      return (finalInspectionPerPart.value / procEff);
    })
  } catch (error) {
      // handle error
  };  
}

您将需要使用await关键字来调用此函数:

await inspectionMins(5);
© www.soinside.com 2019 - 2024. All rights reserved.