我在自定义函数 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;
但是为这样的 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 列表中,但仍然没有效果。