2017-08-07 29 views
0

我正在做多個查詢(查找開放/關閉問題的數量,使用開放/關閉問題(每人)創建開發人員問題關閉的排行榜等 - 查找本週有多少問題已關閉,上週 - 什麼是最新的問題,什麼是最古老的問題)到GitHub的API,並試圖將結果數據寫入Google表格。我正在使用Google Apps腳本編輯器來執行此操作。如何解析GitHub API向Google表格提供的JSON?

我可以從GitHub API獲取JSON數據到控制檯沒有問題,但我不知道如何解析並將其寫入Google表格。我怎麼能這樣做?

一個JSON數據的例子(這一次是「開放性問題」,從http://github.com/vizorvr/patches/搶下)

[17-08-08 13:47:46:353 EEST] {"total_count":271,"incomplete_results":false,"items":[{"url":"https://api.github.com/repos/vizorvr/patches/issues/2035","repository_url":"https://api.github.com/repos/vizorvr/patches","labels_url":"https://api.github.com/repos/vizorvr/patches/issues/2035/labels{/name}","comments_url":"https://api.github.com/repos/vizorvr/patches/issues/2035/comments","events_url":"https://api.github.com/repos/vizorvr/patches/issues/2035/events","html_url":"https://github.com/vizorvr/patches/issues/2035","id":245953104,"number":2035,"title":"Figure out which Three.JS plugins are missing from Patches and add them","user":{"login":"esaruoho","id":4966687,"avatar_url":"https://avatars1.githubusercontent.com/u/4966687?v=4","gravatar_id":"","url":"https://api.github.com/users/esaruoho","html_url":"https://github.com/esaruoho","followers_url":"https://api.github.com/users/esaruoho/followers","following_url":"https://api.github.com/users/esaruoho/following{/other_user}","gists_url":"https://api.github.com/users/esaruoho/gists{/gist_id}","starred_url":"https://api.github.com/users/esaruoho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/esaruoho/subscriptions","organizations_url":"https://api.github.com/users/esaruoho/orgs","repos_url":"https://api.github.com/users/esaruoho/repos","events_url":"https://api.github.com/users/esaruoho/events{/privacy}","received_events_url":"https://api.github.com/users/esaruoho/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-07-27T07:41:52Z","updated_at":"2017-07-27T07:41:52Z","closed_at":null,"body":"Go to http://threejs.org and compare the list of ThreeJS plugins in Patches and what is available on the ThreeJS site. Then implement them.","score":1.0},{"url":"https://api.github.com/repos/vizorvr/patches/issues/2034","repository_url":"https://api.github.com/repos/vizorvr/patches","labels_url":"https://api.github.com/repos/vizorvr/patches/issues/2034/labels{/name}","comments_url":"https://api.github.com/repos/vizorvr/patches/issues/2034/comments","events_url":"https://api.github.com/repos/vizorvr/patches/issues/2034/events","html_url":"https://github.com/vizorvr/patches/issues/2034","id":245952930,"number":2034,"title":"Inspect the Radial Point Geometry -plugin and figure out what it does or should do","user":{"login":"esaruoho","id":4966687,"avatar_url":"https://avatars1.githubusercontent.com/u/4966687?v=4","gravatar_id":"","url":"https://api.github.com/users/esaruoho","html_url":"https://github.com/esaruoho","followers_url":"https://api.github.com/users/esaruoho/followers","following_url":"https://api.github.com/users/esaruoho/following{/other_user}","gists_url":"https://api.github.com/users/esaruoho/gists{/gist_id}","starred_url":"https://api.github.com/users/esaruoho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/esaruoho/subscriptions","organizations_url":"https://api.github.com/users/esaruoho/orgs","repos_url":"https://api.github.com/users/esaruoho/repos","events_url":"https://api.github.com/users/esaruoho/events{/privacy}","received_events_url":"https://api.github.com/users/esaruoho/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-07-27T07:41:07Z","updated_at":"2017-07-27T07:41:07Z","closed_at":null,"body":"Go into Patches and add the Radial Point Geometry -patch.\r\n\r\nStudy what it does. Figure if it does anything. If it does something, come up with an example explaining how it should be used - then help with the popup documentation.\r\n\r\n![vizor](https://user-images.githubusercontent.com/4966687/28659400-fb6f7a5c-72b7-11e7-83c9-6da26fe5ce13.png)\r\n","score":1.0},{"url":"https://api.github.com/repos/vizorvr/patches/issues/2023","repository_url":"https://api.github.com/repos/vizorvr/patches","labels_url":"https://api.github.com/repos/vizorvr/patches/issues/2023/labels{/name}","comments_url":"https://api.github.com/repos/vizorvr/patches/issues/2023/comments","events_url":"https://api.github.com/repos/vizorvr/patches/issues/2023/events","html_url":"https://github.com/vizorvr/patches/issues/2023","id":241289959,"number":2023,"title":"Shortcut: Shift-Tab: improve by opening \"Inspector->Properties\"","user":{"login":"esaruoho","id":4966687,"avatar_url":"https://avatars1.githubusercontent.com/u/4966687?v=4","gravatar_id":"","url":"https://api.github.com/users/esaruoho","html_url":"https://github.com/esaruoho","followers_url":"https://api.github.com/users/esaruoho/followers","following_url":"https://api.github.com/users/esaruoho/following{/other_user}","gists_url":"https://api.github.com/users/esaruoho/gists{/gist_id}","starred_url":"https://api.github.com/users/esaruoho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/esaruoho/subscriptions","organizations_url":"https://api.github.com/users/esaruoho/orgs","repos_url":"https://api.github.com/users/esaruoho/repos","events_url":"https://api.github.com/users/esaruoho/events{/privacy}","received_events_url":"https://api.github.com/users/esaruoho/received_events","type":"User","site_admin":false},"labels":[{"id":165125694,"url":"https://api.github.com/repos/vizorvr/patches/labels/enhancement","name":"enhancement","color":"84b6eb","default":true},{"id":346495796,"url":"https://api.github.com/repos/vizorvr/patches/labels/user","name":"user","color":"fbca04","default":false},{"id":270777736,"url":"https://api.github.com/repos/vizorvr/patches/labels/UX","name":"UX","color":"5319e7","default":false}],"state":"open","locked":false,"assignee":{"login":"gmarinov","id":14101296,"avatar_url":"https://avatars0.githubusercontent.com/u/14101296?v=4","gravatar_id":"","url":"https://api.github.com/users/gmarinov","html_url":"https://github.com/gmarinov","followers_url":"https://api.github.com/users/gmarinov/followers","following_url":"https://api.github.com/users/gmarinov/following{/other_user}","gists_url":"https://api.github.com/users/gmarinov/gists{/gist_id}","starred_url":"https://api.github.com/users/gmarinov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gmarinov/subscriptions","organizations_url":"https://api.github.com/users/gmarinov/orgs","repos_url":"https://api.github.com/users/gmarinov/repos","events_url":"https://api.github.com/users/gmarinov/events{/privacy}","received_events_url":"https://api.github.com/users/gmarinov/received_events","type":"User","site_admin":false},"assignees":[{"login":"gmarinov","id":14101296,"avatar_url":"https://avatars0.githubusercontent.com/u/14101296?v=4","gravatar_id":"","url":"https://api.github.com/users/gmarinov","html_url":"https://github.com/gmarinov","followers_url":"https://api.github.com/users/gmarinov/followers","following_url":"https://api.github.com/users/gmarinov/following{/other_user}","gists_url":"https://api.github.com/users/gmarinov/gists{/gist_id}","starred_url":"https://api.github.com/users/gmarinov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gmarinov/subscriptions","organizations_url":"https://api.github.com/users/gmarinov/orgs","repos_url":"https://api.github.com/users/gmarinov/repos","events_url":"https://api.github.com/users/gmarinov/events{/privacy}","received_events_url":"https://api.github.com/users/gmarinov/received_events","type":"User","site_admin":false}],"milestone":null,"comments":0,"created_at":"2017-07-07T14:57:29Z","updated_at":"2017-07-07T15:44:19Z","closed_at":null,"body":"Improve Shift-Tab so that it opens the Inspector (press I) AND selects Properties-tab instead of Node.\r\n\r\n","score":1.0},{"url":"https://api.github.com/repos/vizorvr/patches/issues/2022","repository_url":"https://api.github.com/repos/vizorvr/patches","labels_url":"https://api.github.com/repos/vizorvr/patches/issues/2022/labels{/name}","comments_url":"https://api.github.com/repos/vizorvr/patches/issues/2022/comments","events_url":"https://api.github.com/repos/vizorvr/patches/issues/2022/events","html_url":"https://github.com/vizorvr/patches/issues/2022","id":24086639 

我有,迄今爲止,抓起東西叫pullJSON嘗試並提取數據,它看起來像然而,不幸的是,它看起來具有破壞性 - 它不會寫入特定的工作表,只是任何舊的活動工作表。我不知道如何修改它以將其寫入特定的工作表,以便它能夠工作,所以這就是爲什麼我要問如何解析工作。

function pullJSON() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheets = ss.getSheets(); 
    var sheet = ss.getActiveSheet(); 

    var url="https://api.github.com/search/issues?q=repo:vizorvr/patches+state:closed"; // Paste your JSON URL here 

    var response = UrlFetchApp.fetch(url); // get feed 
    var dataAll = JSON.parse(response.getContentText()); // 
    var dataSet = dataAll; 

    var rows = [], 
     data; 

    for (i = 0; i < dataSet.length; i++) { 
    data = dataSet[i]; 
    rows.push([data.id, data.name,data.email]); //your JSON entities here 
    } 

    dataRange = sheet.getRange(1, 1, rows.length, 3); // 3 Denotes total number of entites 
    dataRange.setValues(rows); 

} 

編輯2

能得到的回購開放問題的數量,以及在回購封閉的問題數量,而是把它們轉儲到一個特定的細胞還很模糊爲了我。

var openPatches = "https://api.github.com/search/issues?q=repo:vizorvr/patches+state:open" 
    var closedPatches = "https://api.github.com/search/issues?q=repo:vizorvr/patches+state:closed" 

    var openResponse = UrlFetchApp.fetch(openPatches, {'muteHttpExceptions': true}); 
    var Data = JSON.parse(openResponse.getContentText()); 
    Logger.log("Open issues on Patches GitHub:") 
    Logger.log(Data.total_count) 

    var closedresponse = UrlFetchApp.fetch(closedPatches, {'muteHttpExceptions': true}); 
    var data = JSON.parse(closedresponse.getContentText()); 
    Logger.log("Closed issues on Patches GitHub:") 
    Logger.log(data.total_count) 
    Logger.log(Object.keys(data)) 

難道我的理論只是寫Data.total_count和data.total_count在紙張上不同的細胞,而不必擁有一個谷歌片上的瀏覽器另一個選項卡打開?

+1

提供示例JSON數據 – Ritz

+2

試試Google搜索。您會驚訝StackOverFlow.com在搜索中出現多少次。這裏有一個[鏈接](https://stackoverflow.com/questions/40552779/how-to-parse-json-response-in-google-app-script)給一個可能的答案。 – Cooper

+1

標籤不應該被強制標題。參考:http://stackoverflow.com/help/tagging。另一方面,你應該描述你的搜索/研究工作。參考:[問]。 –

回答

1

根據您的更新,在數據寫入到你需要使用getSheetByName在寫入到一個特定的表

var url ="<<SPREADSHEET URL>>"; 
var ss= SpreadsheetApp.openByUrl(url) 
var sheet = ss.getSheetByName("<<SHEET NAME>>"); 

要存儲在不同的單元格的值的特定表,您可以定義範圍並相應地設置值。

sheet.getRange("A1").setValue(Data.total_count); //Open issues 
sheet.getRange("B1").setValue(data.total_count); //Closed issues 
+0

做一個指定工作表的完整URL,包括http:// etc?這整個getActiveSpreadsheet拋出我旋轉,如果我沒有一個單一的活動工作表.. – esaruoho

+1

是的,你可以指定電子表格url ...我修改了答案 – Ritz

+1

我修改了代碼 – Ritz