1

我在自定义函数 excel 插件中遇到了这个错误,我试图在自定义函数中调用外部服务。它适用于这样的 GET 请求:

function stockPrice(ticker) {
var url = "https://api.iextrading.com/1.0/stock/" + ticker + "/price";
return fetch(url)
    .then(function(response) {
        return response.text();
    })
    .then(function(text) {
        return parseFloat(text);
    });
}

CustomFunctionMappings.STOCKPRICE = stockPrice;

取自https://docs.microsoft.com/en-us/office/dev/add-ins/excel/excel-tutorial-custom-functions#create-a-custom-function-that-requests-data-from-网络

但是为这样的 POST 请求提供了一个例外:

function stockPrice(ticker) {
var url = "https://westcentralus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment";
return fetch(url, {
        method: 'POST',
        headers: {
            'Ocp-Apim-Subscription-Key': key,
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        },
        body: JSON.stringify(body))
    .then(function(response) {
        return response.json();
    })
    .then(function(response) {
        return response.somevalue;
    })
    .catch(e => {
        console.error("Caught exception");
        return JSON.stringify(e);
     });
}

以上只是一个关于我如何调用我的服务的想法的示例。我用 2-3 种不同的服务尝试过,我发现在运行 fetch 之后,代码进入 catch 块,并且在 excel 中返​​回的错误值是一个空对象'{}'。由于没有办法在 windows 上调试自定义函数,并且由于没有具体的错误描述,我无法找出问题所在。我还将我的服务域添加到清单文件中的 App Domain 列表中,但仍然没有效果。

4

1 回答 1

1

我不确定特定的 API 是否接受 POST 请求,因此您可能会遇到这种情况。

Windows 中的调试仍在进行中,但您可以使用 Excel online 和 F12tools 进行调试。

如果您使用的是 Windows,则可以将 console.log 语句与运行时日志记录结合使用: https ://docs.microsoft.com/en-us/office/dev/add-ins/excel/custom-functions-best-practices #故障排除

希望对您有所帮助,我们将在 Windows 桌面上的自定义功能调试就绪时更新此内容。

于 2018-10-31T04:10:25.110 回答