我有一个循环功能,可以在其中找到所有ID,然后将获取所有详细信息报告。
我将每个ID详细信息存储在'reportData'数组中。
当前它将逐个打印每个数组。
我尝试了很多更改,但似乎我的问题只是一个简单的逻辑放置。
function IDDetails()
{
var ID = []
for (row = 4; row < 150; row++)
{
var col = 4;
var activeSheet = SpreadsheetApp.getActiveSpreadsheet();
var activeSheetTab = activeSheet.getSheetByName('Newsletter Report');
var ID = activeSheetTab.getRange(row, col).getValue();
if (ID.length != 0){
var root = '';
var endpoint1 = '';
var endpoint2 = '';
var params =
{
'method': 'GET',
'muteHttpExceptions': true,
'headers': {'Authorization': 'apikey ' + API_KEY}
};
try
{
var response = UrlFetchApp.fetch();
var data = response.getContentText();
var json = JSON.parse(data);
var report = json['details'];
var reportData = [];
for (var i = 0; i < report.length; i++)
{
reportData.push([
ID,
report[i]["item1"],
report[i]["item2"],
report[i]["item3"]
]);
}
} catch(e) {
Logger.log(e);
};
Logger.log(reportData);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Details');
var numRows = reportData.length;
var numCols = reportData[0].length;
sheet.getRange(2,1,numRows,numCols).setValues(reportData);
}
else {}
}
}
但是,我想要做的是,将所有ID的详细信息存储在一个数组中,让其称为“ reportDataALL”
然后将其打印为一个。
示例结果:
ID 1:
+---+---------+--------+--------+
|ID | item1 | item2 | item3 |
+---|---------+--------+--------+
| 1 | xxx | yyy | zzz |
+---+---------+--------+--------+
| 1 | ooo | rrr | eee |
+---+---------+--------+--------+
ID 2:
+---+---------+--------+--------+
|ID |item1 | item2 | item3 |
+---|---------+--------+--------+
| 2 | aaa | bbb | ccc |
+---+---------+--------+--------+
| 2 | ggg | ppp | lll |
+---+---------+--------+--------+
我期望的是:
ALL ID
reportDataALL
+---+---------+--------+---------+
|ID | item1 | item2 | item3 |
+---|---------+--------+---------+
| 1 | xxx | yyy | zzz |
+---|---------+--------+---------+
| 1 | ooo | rrr | eee |
+---+---------+--------+---------+
| 2 | aaa | bbb | ccc |
+---+---------+--------+---------+
| 2 | ggg | ppp | lll |
+---+---------+--------+---------+
答案 0 :(得分:1)
我认为您创建了很多新表,但是您应该将它们推入主结果表
function IDDetails()
{
var ID = []
var reportData = []; /// <<<<<<<<< the target array
for (row = 4; row < 150; row++)
{
var col = 4;
var activeSheet = SpreadsheetApp.getActiveSpreadsheet();
var activeSheetTab = activeSheet.getSheetByName('Newsletter Report');
var ID = activeSheetTab.getRange(row, col).getValue();
if (ID.length != 0){
var root = '';
var endpoint1 = '';
var endpoint2 = '';
var params =
{
'method': 'GET',
'muteHttpExceptions': true,
'headers': {'Authorization': 'apikey ' + API_KEY}
};
try
{
var response = UrlFetchApp.fetch();
var data = response.getContentText();
var json = JSON.parse(data);
var report = json['details'];
for (var i = 0; i < report.length; i++)
var DETAILS=[]; //create next dimension
DETAILS[0]=id;
DETAILS[1]=report[i]["item1"];
DETAILS[2]=report[i]["item2"];
DETAILS[3]=report[i]["item3"];
reportData.push(DETAILS) ;
} catch(e) {
Logger.log(e);
};
Logger.log(reportData);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Details');
var numRows = reportData.length;
var numCols = reportData[0].length;
sheet.getRange(2,1,numRows,numCols).setValues(reportData);
}
else {}
}
}
现在您可以使用
进行访问 reportData[i][j]
如果您想拥有二维数组
与对象相同
var DETAILS={}; //same as object
DETAILS[id]=id;
DETAILS["item1"]=report[i]["item1"];
DETAILS["item2"]=report[i]["item2"];
DETAILS["item3"]=report[i]["item3"];
reportData.push(DETAILS) ;
通过
访问 reportData[i].id
reportData[i].item1
以此类推