3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Backlogの課題を一括取得してGoogleAppsScriptで配列や連想配列として扱う

Last updated at Posted at 2019-06-04

BacklogAPIで課題を一括取得してJSやGASで配列や連想配列として扱うことがよくあるのでメモしておく。

Backlogの仕様書

GoogleAppsScriptで一括取得

最大100件しか取得できないので、何度もコールします。
パラメータは適宜変更します。

getBacklogIssues.js
function getBacklogIssues(){
  var space_id   = "スペース名";
  var api_key    = "APIキー";
  var project_id = "プロジェクトID";
  var count = 100;//最大取得数100件
  var params = "&order=asc";//その他パラメータをご自由に
  var issues = [];//結果を格納

  while(true){
    //課題を取得
    var response = UrlFetchApp.fetch("https://" + space_id + ".backlog.jp/api/v2/issues?apiKey=" + api_key + "&projectId[]=" + project_id + "&count=" + count + "&offset=" + issues.length + params);

    //エラーの場合は終了
    if(response.getResponseCode() != 200) return false;
    
    //配列に追加
    issues = issues.concat(JSON.parse(response));
      
    //取得が100件未満ならループから抜ける。
    if(JSON.parse(response).length < count) break;
  }
  return issues;
}

issueKeyがキーの連想配列に変換

PHPのarray_column()的なことをして、単純な配列を連想配列に変える。

arrayColumn.js
function arrayColumn(array, key){
  var new_array = {};
  for(row in array) new_array[array[row][key]] = array[row];
  return new_array;
}

var issues = arrayColumn(getBacklogIssues(), "issueKey");
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?