简体   繁体   English

如何遍历来自 API 的所有响应并使用 Javascript 将它们传递给 Google 表格?

[英]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);

    }

}

The return statement finishes the execution of a function and exits it return语句完成 function 的执行并退出它

  • You use return(content) inside the for loop您在for循环中使用return(content)
  • As a consequence the function will be exited after the first iteration因此,function 将在第一次迭代后退出
  • Instead, you should create an array to which you append within each iteration the data for each project相反,您应该创建一个数组,您在每次迭代中将 append 存储到每个项目的数据中
  • Return the array containing all data after the exiting the 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.

相关问题 如何遍历 Javascript 中的 API 中的数据? - How do I loop through data from an API in Javascript? 如何使用JavaScript在Google Picker中下载Google表格? - How do I download a Google Sheet with Google Picker all in JavaScript? 如何使用谷歌脚本自动将表单响应从谷歌表格移动到谷歌文档? - How can I automate moving form responses from a google sheet to a google doc using google scripts? 如何在JavaScript中同步API响应? - How do I synchronise API responses in JavaScript? (Google Sheet API) 如何将 10 添加到 A1? (Javascript) - (Google Sheet API) How do I add 10 to A1? (Javascript) 如何遍历复选框ID并将其传递给SQL DB? - How Do I Loop Through Checkbox IDs and pass them through to SQL DB? 如何使用Google Places和JavaScript API循环浏览其他页面? - How to loop through additional pages using google places and javascript API? 我应该如何直接从前端或通过后端获取 google api 响应? - how should I fetch the google api responses, from directly frontend or going through backend? 如何将变量从JavaScript(Google Maps API)传递给表单输入 - How do I pass a variable from JavaScript (Google Maps API) to my Form Input 如何将其他谷歌表格中的值添加在一起并将它们显示在一张表格上? - How do i add values from other google sheets together and display them on one sheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM