[英]How do I loop through all responses from API and pass them to Google Sheet using Javascript?
I have this code that gets project ID from google sheet if they meet a criteria (between two dates) and uses it for an API to get the users in the project.如果符合条件(在两个日期之间),我有这段代码可以从谷歌表中获取项目 ID,并将其用于 API 以获取项目中的用户。 After, getTeam function append the response (Team) to a sheet.
之后,获取Team function append 对工作表的响应(团队)。 My challenge is that I am not able to get all the users appended to the sheet in the getTeam function.
我的挑战是我无法将所有用户都附加到getTeam function 中的工作表中。 I did not structure well my loops in readDates I will help in structuring the code to get all the users to a sheet.
我没有很好地构建我在readDates中的循环,我将帮助构建代码以使所有用户都进入工作表。
function readDates() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range1 = sheet.getRange("C2:C" + sheet.getLastRow()).getValues();
var searchString = "Project";
var ahead = nextweek()
var behind = lastweek()
var team_array = [];
for (var i = 0; i < range.length; i++) {
if (range1[i][0] >= behind && range1[i][0] <= ahead) {
var lastRow = sheet.getRange(2 + i, 1, 1, 8).getValues();
var dateval = lastRow[0][2]
var data = {
'project_id': lastRow[0][3] // There are 6 projects ID meeting criteria, each project has 7 users
};
var options = {
method: 'get',
headers: {
Authorization: 'Bearer ' + token
}
};
var url = tknurl + Endpoint + data.project_id + '/users?auth=' + token
var response = UrlFetchApp.fetch(url, options);
var team = JSON.parse(response);
var content = team.data;
team_array.push(content);
}
}
return(content)
}
getTeam function.获取团队 function 。 Where I am only getting users for one project, instead of all 6 projects
我只为一个项目而不是所有 6 个项目获取用户
function getTeam() {
var ss = SpreadsheetApp.getActive().getSheetByName('Team');
var Pro_data = readDates()
for (var j = 0; j < Pro_data.length; j++) {
ss.getRange(2 + j, 1).setValue(Pro_data[j].id);
ss.getRange(2 + j, 2).setValue(Pro_data[j].first_name);
ss.getRange(2 + j, 3).setValue(Pro_data[j].last_name);
ss.getRange(2 + j, 4).setValue(Pro_data[j].display_name);
ss.getRange(2 + j, 5).setValue(Pro_data[j].email);
ss.getRange(2 + j, 6).setValue(Pro_data[j].user_type_id);
ss.getRange(2 + j, 7).setValue(Pro_data[j].role);
}
}
return(content)
inside the for
loopfor
循环中使用return(content)
for
loop.for
循环后返回包含所有数据的数组。 Sample:样本:
...
var myArray = [];
for (var i = 0; i < range1.length; i++) {
...
var content = team.data;
myArray.push(content);
...
}
return content;
...
Side note:边注:
You can replace the nested if statement您可以替换嵌套的 if 语句
if (range1[i][0] >= behind) {
if (range1[i][0] <= ahead) {
...
}
}
through a combined one:通过一个组合:
if (range1[i][0] >= behind && range1[i][0] <= ahead) {
...
}
You are returning only the first result in readDates function.您只返回 readDates function 中的第一个结果。 Your return instruction is inside the loop, you need to aggregate all the results somewhere, you can use an array before executing the return.
您的返回指令在循环内,您需要在某处聚合所有结果,您可以在执行返回之前使用数组。
var contents = [];
for (var i = 0; i < range.length; i++) {
...
contents.push(content);
} //close if2
} //close if1
}//close for
return contents;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.