2017-08-07 51 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