LoginSignup
0
1

More than 3 years have passed since last update.

Backlogの課題一覧をGoogleAppsScriptでSpreadSheetに張り付ける

Last updated at Posted at 2019-06-04

Backlogの課題一覧をSpreadsheetに張り付けることがよくあるのでメモしておく:writing_hand:

スプレッドシートへの準備 :footprints:

下表のように、2行目にBacklogのレスポンスのキーを予め記述しておき、課題の値を3行目以降に記述していくこととする。
カスタムフィールドの場合、1つ目はcustomFields1、2つ目はcustomFields2...とする。

一括取得前のイメージ

概要 期限日 ステータス 3番目のカスタムフィールド
summary dueDate status customFields3
 
 

:point_down_tone1::point_down_tone2::point_down_tone3::point_down_tone4::point_down_tone5:

一括取得後のイメージ

概要 期限日 ステータス 3番目のカスタムフィールド
summary dueDate status customFields3
xxxの件 2019/06/06 処理中 xxxxxx
yyyの件 2019/07/07 未対応 yyyyyy

Backlogの課題を取得する :red_car:

ここにメモった。
https://qiita.com/rubyfmzk/items/1c594819134258d1c4e3

SpreadSheetに張り付ける :spider:

SpreadSheetに張り付けるメイン関数 :dancer:

main.js
function writeSpreadsheet(array) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("シート名");//シート名を指定
  var key_row = 2;//キーの行を指定
  var write_issues = [];//書き込み内容を格納する配列

  //SpreadSheetで使用するキー一覧
  var keys = sheet.getRange(key_row, 1, 1, sheet.getLastColumn()).getValues()[0];

  for(var i in array){
    var issue = array[i];
    var write_issue = [];

    //無効と重複は表示しない
    if(issue["resolution"] && (issue["resolution"]["id"] === 2 || issue["resolution"]["id"] === 3)){
      continue;
    }

    for(ii in keys){
      var key = keys[ii];
      if(!key) break;

      //値を配列から取得
      var val = issue[key];

      //カスタムフィールドの値を取得
      if(key.match(/^customFields/)) val = val["customFields"][key.match(/\d+$/) - 1]["value"];

      //配列の場合は値に変換する関数
      val = val2str(val);

      //日付の場合、フォーマットを変更する関数
      val = dateFormat(val);

      //書き込み用の配列に追加
      write_issue.push(val);
    }
    //書き込み用の配列に追加
    write_issues.push(write_issue);
  }

  //データをシートに書き込む
  if(write_issues){
    sheet.getRange(key_row + 1, 1, write_issues.length, write_issues[0].length).setValues(write_issues);
    Browser.msgBox("書き込み完了しました。");
  }
  else{
    Browser.msgBox("データはありませんでした。");
  }
}

型が不明な値を再帰的に文字列に変換 :arrows_counterclockwise:

配列の場合は/で区切り連結するようにする。

util.js
function val2str(val){
  if(val === null) return "";

  if(typeof val === "object"){
    if(val["name"]) return val["name"];

    if(val[0]){
      var val2 = val2str(val[0]);
      for(i in val){
        val2 += "/" + val2str(val[i])
      }
      return val2;
    }
  }
  return val;
}

SpreadSheetの日付型に変換 :clock1:

YYYY-MM-DDTHH:MM:SSZ を YYYY/MM/DD に変換。
SpreadSheetにYYYY/MM/DDで入力してあげると、日付として認識してくれる:v:

util.js
function dateFormat(date){
  if(!date) return date;
  if(typeof date !== "string") return date;

  var regex_date = /^\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}Z$/;
  if(date.match(regex_date)) return date.slice(0,4) + "/" + date.slice(5,7) + "/" + date.slice(8,10);
  return date;
}
0
1
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
0
1