我正在Apps脚本中实现自定义功能。
要允许该函数在Google表格中用作数组公式,我正在使用以下模板作为该函数:
function myfunction(input) {
if (input.map) { // Test whether input is an array.
return input.map(myfunction); // Recurse over array if so.
} else {
// Handle individual values here...
}
}
这样做可以使自定义函数以二维数组的形式接受范围作为输入,然后返回可以溢出到适当单元格中的二维数组。 This is documented here.
但是,使用下面的代码,我没有使用“输入”,而是使用了“ URL”,我相信这是问题的根源。我产生错误:Attribute provided with no value: url (line 5)
。
function getRedirect(url) {
if (url.map) {
return url.map(getRedirect);
} else {
var response = UrlFetchApp.fetch(url,{'followRedirects': false,'muteHttpExceptions': false});
var redirectUrl = response.getHeaders()['Location']; // undefined if no redirect,so...
var responseCode = response.getResponseCode();
if (redirectUrl) { // ...if redirected...
var nextRedirectUrl = getRedirect(redirectUrl); // ...it calls itself recursively...
Logger.log(url + " is redirecting to " + redirectUrl + ". (" + responseCode + ")");
return nextRedirectUrl;
} else { // ...until it's not
Logger.log(url + " is canonical. (" + responseCode + ")");
return url;
}
}
}
该如何解决?