为什么执行UrlFetchApp.fetchAll()后函数会失败?
Why does the function fail after UrlFetchApp.fetchAll () is executed?
执行useFetchAll函数后,我将response值传递给stringform函数,报错
TypeError: A.forEach is not a function
在示例中,我创建了变量 asdf 并将 Logger.log(响应)值复制到其中,stringform 函数起作用并且 returns 是一个数组。为什么函数在值为(response)时抛出错误?
// Using UrlFetchApp.fetchAll()
function useFetchAll(req) {
var response = UrlFetchApp.fetchAll(req);
var asdf=[[["a",1],["a",2],["a",3],["a",4],["a",5]], [["b",6],["b",7],["b",8],["b",9],["b",10]], [["c",11],["c",12],["c",13],["c",14],["c",15]], [["d",16],["d",17],["d",18],["d",19],["d",20]], [["e",21],["e",22],["e",23],["e",24],["e",25]]];
Logger.log(response);
Logger.log(stringform(asdf));
}
function stringform(asdf) {
let formad = '';
asdf.forEach(A => {
A.forEach(r => {
formad += r.join(',') + '%0A';
});
});
return formad;
}
这是一个 3 深的数组。
function myfunction() {
let s = '';
var asdf = [[["a", 1], ["a", 2], ["a", 3], ["a", 4], ["a", 5]], [["b", 6], ["b", 7], ["b", 8], ["b", 9], ["b", 10]], [["c", 11], ["c", 12], ["c", 13], ["c", 14], ["c", 15]], [["d", 16], ["d", 17], ["d", 18], ["d", 19], ["d", 20]], [["e", 21], ["e", 22], ["e", 23], ["e", 24], ["e", 25]]];
asdf.forEach(A => {
A.forEach(r => {
s += r.join(',')+ '\n';
})
})
Logger.log(s);
}
Execution log
3:53:29 PM Notice Execution started
3:53:29 PM Info a,1
a,2
a,3
a,4
a,5
b,6
b,7
b,8
b,9
b,10
c,11
c,12
c,13
c,14
c,15
d,16
d,17
d,18
d,19
d,20
e,21
e,22
e,23
e,24
e,25
3:53:30 PM Notice Execution completed
您的下层函数本来可以工作,但该函数中未定义 asdf。
你的底层函数是这样运行的:
function stringform(asdf) {
var asdf = [[["a", 1], ["a", 2], ["a", 3], ["a", 4], ["a", 5]], [["b", 6], ["b", 7], ["b", 8], ["b", 9], ["b", 10]], [["c", 11], ["c", 12], ["c", 13], ["c", 14], ["c", 15]], [["d", 16], ["d", 17], ["d", 18], ["d", 19], ["d", 20]], [["e", 21], ["e", 22], ["e", 23], ["e", 24], ["e", 25]]];
let formad = '';
asdf.forEach(A => {
A.forEach(r => {
formad += r.join(',') + '\n';
});
});
Logger.log(formad)
}
关于你 Why does the function fail after UrlFetchApp.fetchAll () is executed?
的问题,根据你的脚本和 In the example, I made the variable asdf and copied the Logger.log (response) value into it, the stringform function works and returns an array. Why does the function throw an error when the value is (response)?
,我认为你的问题的原因是 fetchAll
returns HTTPResponse[]
这是一个数组。
例如,当其中一个请求 req
returns 二维数组如 [["a",1],["a",2],["a",3],["a",4],["a",5]]
时,var response = UrlFetchApp.fetchAll(req)
的 Logger.log(response)
显示 [[["a",1],["a",2],["a",3]], [["a",1],["a",2],["a",3]]]
。在您的情况下,我认为来自多个请求的值可能包含在 response
.
中
如果我对你的情况的理解是正确的,下面的修改怎么样?
修改后的脚本:
function useFetchAll(req) {
var response = UrlFetchApp.fetchAll(req);
var asdf = response.map(r => JSON.parse(r.getContentText()));
Logger.log(stringform(asdf));
}
function stringform(asdf) {
let formad = '';
asdf.forEach(A => {
A.forEach(r => {
formad += r.join(',') + '%0A';
});
});
return formad;
}
或者,
function useFetchAll(req) {
var response = UrlFetchApp.fetchAll(req);
var values = response.reduce((t, r) => t += JSON.parse(r.getContentText()).map(c => c.join(',') + '%0A'), "");
Logger.log(values)
}
参考:
-
Return: HTTPResponse[] — An array of HTTP response data from each input request.
执行useFetchAll函数后,我将response值传递给stringform函数,报错
TypeError: A.forEach is not a function
在示例中,我创建了变量 asdf 并将 Logger.log(响应)值复制到其中,stringform 函数起作用并且 returns 是一个数组。为什么函数在值为(response)时抛出错误?
// Using UrlFetchApp.fetchAll()
function useFetchAll(req) {
var response = UrlFetchApp.fetchAll(req);
var asdf=[[["a",1],["a",2],["a",3],["a",4],["a",5]], [["b",6],["b",7],["b",8],["b",9],["b",10]], [["c",11],["c",12],["c",13],["c",14],["c",15]], [["d",16],["d",17],["d",18],["d",19],["d",20]], [["e",21],["e",22],["e",23],["e",24],["e",25]]];
Logger.log(response);
Logger.log(stringform(asdf));
}
function stringform(asdf) {
let formad = '';
asdf.forEach(A => {
A.forEach(r => {
formad += r.join(',') + '%0A';
});
});
return formad;
}
这是一个 3 深的数组。
function myfunction() {
let s = '';
var asdf = [[["a", 1], ["a", 2], ["a", 3], ["a", 4], ["a", 5]], [["b", 6], ["b", 7], ["b", 8], ["b", 9], ["b", 10]], [["c", 11], ["c", 12], ["c", 13], ["c", 14], ["c", 15]], [["d", 16], ["d", 17], ["d", 18], ["d", 19], ["d", 20]], [["e", 21], ["e", 22], ["e", 23], ["e", 24], ["e", 25]]];
asdf.forEach(A => {
A.forEach(r => {
s += r.join(',')+ '\n';
})
})
Logger.log(s);
}
Execution log
3:53:29 PM Notice Execution started
3:53:29 PM Info a,1
a,2
a,3
a,4
a,5
b,6
b,7
b,8
b,9
b,10
c,11
c,12
c,13
c,14
c,15
d,16
d,17
d,18
d,19
d,20
e,21
e,22
e,23
e,24
e,25
3:53:30 PM Notice Execution completed
您的下层函数本来可以工作,但该函数中未定义 asdf。
你的底层函数是这样运行的:
function stringform(asdf) {
var asdf = [[["a", 1], ["a", 2], ["a", 3], ["a", 4], ["a", 5]], [["b", 6], ["b", 7], ["b", 8], ["b", 9], ["b", 10]], [["c", 11], ["c", 12], ["c", 13], ["c", 14], ["c", 15]], [["d", 16], ["d", 17], ["d", 18], ["d", 19], ["d", 20]], [["e", 21], ["e", 22], ["e", 23], ["e", 24], ["e", 25]]];
let formad = '';
asdf.forEach(A => {
A.forEach(r => {
formad += r.join(',') + '\n';
});
});
Logger.log(formad)
}
关于你 Why does the function fail after UrlFetchApp.fetchAll () is executed?
的问题,根据你的脚本和 In the example, I made the variable asdf and copied the Logger.log (response) value into it, the stringform function works and returns an array. Why does the function throw an error when the value is (response)?
,我认为你的问题的原因是 fetchAll
returns HTTPResponse[]
这是一个数组。
例如,当其中一个请求 req
returns 二维数组如 [["a",1],["a",2],["a",3],["a",4],["a",5]]
时,var response = UrlFetchApp.fetchAll(req)
的 Logger.log(response)
显示 [[["a",1],["a",2],["a",3]], [["a",1],["a",2],["a",3]]]
。在您的情况下,我认为来自多个请求的值可能包含在 response
.
如果我对你的情况的理解是正确的,下面的修改怎么样?
修改后的脚本:
function useFetchAll(req) {
var response = UrlFetchApp.fetchAll(req);
var asdf = response.map(r => JSON.parse(r.getContentText()));
Logger.log(stringform(asdf));
}
function stringform(asdf) {
let formad = '';
asdf.forEach(A => {
A.forEach(r => {
formad += r.join(',') + '%0A';
});
});
return formad;
}
或者,
function useFetchAll(req) {
var response = UrlFetchApp.fetchAll(req);
var values = response.reduce((t, r) => t += JSON.parse(r.getContentText()).map(c => c.join(',') + '%0A'), "");
Logger.log(values)
}
参考:
-
Return: HTTPResponse[] — An array of HTTP response data from each input request.