UDF Excel 使用 Vlookup? JavaScript API

UDF Excel Using Vlookup? JavaScript API

如何在我的 Excel 电子表格中调用它? Rick 在下面帮助我编写了以下代码,但我该如何调用它呢?我正在尝试 VLOOKUP numOfParts(在参数中询问)和 return 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
  };
}

很难使用您的代码,因为我们没有 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);