使用Excel内置函数从自定义函数调用Microsoft Excel API?

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

由于此链接Call Microsft Excel APIs from a custom function,我从自定义函数成功调用了我的第一个Microsoft Excel API。但是,我试图在自定义函数中添加VLOOKUP内置Excel函数。在尝试使我的代码正常工作之后,我仍然得到了#VALUE!在牢房里我的代码丢失了什么?我知道这是5/11时的全新产品,因此此功能尚不可用吗?

/**
 * @customfunction
 * @param firstName person's first name
 **/
export async function getRangeValue (firstName: string) {
    let lastName;
    let context = new Excel.RequestContext();
    const range = context.workbook.worksheets.getActiveWorksheet().getRange("A1:C5");
    range.load();
    await context.sync();
    lastName = context.workbook.functions.vlookup(firstName, range, 3, false);
    lastName.load('value');
    await context.sync();
    return lastName.value;
}

enter image description here

=CONTOSOSHARE.GETRANGEVALUE("Happy")
excel office-js custom-functions-excel
1个回答
0
投票

由于您在函数中有两次调用,请考虑使用Exce.run。例如:

export async function getRangeValue (firstName: string) {
let lastName;
await Excel.run(async (context) => {
    const range = context.workbook.worksheets.getActiveWorksheet().getRange("A1:B5");
    range.load();
    await context.sync();
    lastName = context.workbook.functions.vlookup("Happy", range, 2, false);
    lastName.load("value");
    await context.sync();
    console.log(lastName.value);
  });
return lastName.value;
}

请让我知道它是否有效。

© www.soinside.com 2019 - 2024. All rights reserved.