使用GAS将Google徽标API数据提取到Google电子表格

时间:2017-02-10 09:38:40

标签: api google-apps-script google-sheets google-spreadsheet-api

我使用GAS从Pingdom中提取了API数据,但我能够提取的唯一数据是..

TestID   |  TestName |  ResponseTime | Status
1299405     Google         243          up
1299434     Yahoo          342          up
1299499     Bling          299          up

使用此脚本..

function pingdom() {

var headers = 
{  
    "Accept": "application/json",
    "Authorization": "Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "App-Key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "Account-Email": "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
 var options = 
{ 
    "headers" : headers, 
    "method" : "get"
}

var response = UrlFetchApp.fetch("https://api.pingdom.com/api/2.0/checks", options); 
var response_json = JSON.parse(response.getContentText()); 

var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var sheet = ss.getSheetByName("Sheet1");


var rows = [], data; 

for (i = 0; i < response_json.checks["length"]; i++) { 
    data = response_json.checks[i]; 
    rows.push([data.id, data.name, data.lastresponsetime, data.status]); 
}

//Set column headings 
var colHeaders = [[ "Test ID", "Test Name", "Response Time", "Status"]]; 
dataRange = sheet.getRange(1, 1, 1, 4); 
dataRange.setValues(colHeaders) ;

//Display data 
dataRange = sheet.getRange(2, 1, rows.length, 4); 
dataRange.setValues(rows);
}

现在我正在尝试提取更多数据,例如&#34;平均响应时间&amp;正常运行时间&#34;不幸的是,Pingdom API只允许我提取单个testID的数据(TestID将在API URL https://api.pingdom.com/api/2.0/summary.performance/1299405?includeuptime=true中给出)

使用此谷歌脚本...

function uptime() {
var headers = 
{  
    "Accept": "application/json",
    "Authorization": "Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "App-Key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "Account-Email": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
 var options = 
{ 
    "headers" : headers, 
    "method" : "get"
}

 var rows = [] , sum = {};
 var response = UrlFetchApp.fetch("https://api.pingdom.com/api/2.0/summary.performance/1299405?includeuptime=true", options); 
 var response_json = JSON.parse(response.getContentText()); 
 //var sum = response_json.summary.states;
 for (i = 0; i < response_json.summary.hours.length; i++) 
 {
   sum = response_json.summary.hours[i] ;
 rows.push([sum["avgresponse"],sum["uptime"]]);
 break;
 }
 var ss = SpreadsheetApp.getActiveSpreadsheet(); 
 var sheet = ss.getSheetByName("Sheet2");
 dataRange = sheet.getRange(1, 1, 1, 2); 

  //Set column headings 
var colHeaders = [[ "avg","uptime"]]; 
dataRange.setValues(colHeaders) ;

//Display data 
dataRange = sheet.getRange(2, 1, rows.length, 2); 
dataRange.setValues(rows);


}

我尝试从sheet1中获取TestID并创建一个循环,以便获得&#34;平均响应时间&amp;正常运行时间&#34;在sheet1中出现的所有testID。

data_id = response_json.checks[i]; 
     //for ( i = response_json.checks.id; i++){
        var response1 = UrlFetchApp.fetch("https://api.pingdom.com/api/2.0/summary.performance/" + data_id.id, options);

但不幸的是,我无法提取任何数据。如果有人能帮助我实现这一点,那将会非常有帮助。

(这是Pingdom API文档 - https://www.pingdom.com/resources/api

1 个答案:

答案 0 :(得分:0)

您可以通过创建检查数组并循环遍历它们来完成此操作。我的床单使用了这​​个ID很多,所以我所做的就是创建一张包含所有支票的工作表,然后我可以在整个地方使用它。所以我的checkID表看起来像这样:

CheckName                  | CheckID
someendpoint.com/api         456213
someotherendpoint.com/api    456213

...等

然后你可以创建一个函数并从工作表中获取这些值。这是一个如何从工作表中获取它并将其放入数组的示例:

var datetimeArr = new Array(); 
var ids =  new Array();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s2 = ss.getSheetByName('checkID');
var names = s2.getRange('a1:a').getValues();
var ids = s2.getRange('b1:b').getValues();