4
5

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.

Trelloのリスト一覧とコメントを取得

Posted at

function getCardId() {
 
  var trelloKey   = "XXX";//keyを入力してください
  var trelloToken = "XXX";//tokenを入力してください
  var listId     = "XXX";//リストIDを入力してください
  var boardUrl = "https://trello.com/1/boards/" + listId + "/lists?key=" + trelloKey + "&&token=" + trelloToken;//メンバー図鑑のリストの一覧を取得
  var boardRes = UrlFetchApp.fetch(boardUrl, {'method':'get'});
  var boardJson = JSON.parse(boardRes.getContentText());
  var boardMaxRows = boardJson.length; //格納されているデータの行数を取得


  
  for(var i = 1; i < boardMaxRows; i++){

    //必要なデータのKeyを指定して値を取得する
    var listName = boardJson[i].name;    
    var listId = boardJson[i].id;
    
    //メンバー図鑑のカード取得
    var listsUrl = "https://trello.com/1/lists/" + listId + "/cards?key=" + trelloKey + "&token=" + trelloToken;    
    var listsRes = UrlFetchApp.fetch(listsUrl, {'method':'get'});
    var listsJson = JSON.parse(listsRes.getContentText());

    var cards =[]; //箱を作る
    var comment =[]; //箱を作る
    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    
    if(GetAllSheet(listName)){
      //既にシートは作成してあるので、何もしない
    }else{
      //シートは作成
    spreadsheet.insertSheet(listName);
    }
    
    var listsMaxRows = listsJson.length; //格納されているデータの行数を取得
    
    for(var j = 0; j < listsMaxRows; j++){
      
      //必要なデータのKeyを指定して値を取得する
      var id = listsJson[j].id;
      var name = listsJson[j].name;
      var desc = listsJson[j].desc;
      var shortUrl = listsJson[j].shortUrl;
      
      //メンバー図鑑のカード取得
      var commentUrl = "https://trello.com/1/cards/" + id + "/actions?key=" + trelloKey + "&token=" + trelloToken;
      var commentRes = UrlFetchApp.fetch(commentUrl, {'method':'get'});
      var commentJson = JSON.parse(commentRes.getContentText());
      var commentMaxRows = commentJson.length; //格納されているデータの行数を取得

      for(var n = 0; n < commentMaxRows; n++){    
        
        var commentpre = commentJson[n].data.text;
              
        if (commentpre == null || commentpre == undefined){
          comment[j] = '';
        }else{
          comment[j] += commentpre + String.fromCharCode(10)  + String.fromCharCode(10) ;
          comment[j] = comment[j].replace(/undefined/g,'');
          Logger.log(comment[j]); 
        }
      }     

      //取得したここのデータをまとめて、ひとつのカード情報としてまとめる
      var card = [name, desc, shortUrl, comment[j]];
      
      //先程取得したcardsという箱に追加する
      cards.push(card);

      //    Logger.log(listsMaxRows);

    var sheet = spreadsheet.getSheetByName(listName);//リストというシートに保存
    var lastRow = cards.length;
    var column = cards[0].length;
    sheet.getRange(2,1,lastRow,column).setValues(cards);
   }
  insertRecords(listName);
  }

}

function insertRecords(listName){
  
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var mySheet = spreadsheet.getSheetByName(listName);
 
  arrData = [
    ['名前','説明','URL','コメント'],
    ];
 
  var rows = arrData.length;
  var cols = arrData[0].length;

  mySheet.getRange(1,1,rows,cols).setValues(arrData);
 
}

//シートがあるかないかの判定
 function GetAllSheet(listName) {
  var objSheets
  objSheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  var intMaxIndex = objSheets.length;
  for (intIndex = 0; intIndex < intMaxIndex; intIndex++) {
    if(objSheets[intIndex].getName() == listName){
      return true;
    }
  }
}

// スプレッドシートのメニューからPDF作成用の関数を実行出来るように、「Trello同期実行」というメニューを追加。
function onOpen() {
    var sheet = SpreadsheetApp.getActiveSpreadsheet();
    var entries = [       
        {
            name : "Trelloのメンバー図鑑と同期する",
            functionName : "getCardId"
        }
        ];
    sheet.addMenu("Trello同期実行", entries);
};

4
5
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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?