Google Script可将Trello数据导入Google表格

时间:2016-03-08 21:55:37

标签: google-apps-script trello

我正在使用Google脚本将有关Trello卡的信息提取到Google表格中。它对我来说很好,但是今天突然间,所有的卡片都为每条数据提供了“Undefined”。但我无法理解为什么突然停止工作。

Google脚本如下:

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [ {name: "Update from Trello", functionName: "main"}];
  ss.addMenu("Trello", menuEntries);
}

// trello variables
var api_key = "XXXXX";
var api_token = "XXXXX";
var board_id = "XXXXX";
var url = "https://api.trello.com/1/";


var key_and_token = "key=" + api_key + "&token=" + api_token;


//called by google docs apps
function main() {
  var ss = SpreadsheetApp.getActiveSheet().clear();
  ss.appendRow(["Date", "Task", "Who", "List", "Link"]);
  var response = UrlFetchApp.fetch(url + "boards/" + board_id + "/lists/all/?" + key_and_token);
  var lists = JSON.parse((response.getContentText()));
  Logger.log(lists);

  for (var i = 0; i < lists.length; i++) {
    var list = lists[i];

    var response = UrlFetchApp.fetch(url + "list/" + list.id + "/cards/all?" + key_and_token);
    var cards = JSON.parse(response.getContentText());
    if (!cards)
      continue;

    for (var j = 0; j < cards.length; j++) {
      var card = cards[j];
      Logger.log(cards);
      var response = UrlFetchApp.fetch(url + "cards/" + card.id + "/actions/?" + key_and_token);
      var carddetails = JSON.parse(response.getContentText());
      if (!carddetails)
        continue;
      Logger.log(carddetails);

      for (var k = 0; k < carddetails.length; k++) {        
        var dato = carddetails[k].date;
        var fullname = carddetails[k].memberCreator.fullName;
        var name = card.name;
        var link = card.url;
        var listname = list.name;

        Logger.log(name);
        Logger.log(dato);
        Logger.log(fullname);
        Logger.log(link);
        Logger.log(listname);
      }
      ss.appendRow([dato, name, fullname, listname, link]);     
    }                                       
  }
}

1 个答案:

答案 0 :(得分:2)

在那个问题之后的一年,我正在搜索这个代码然后我意识到Trello Api在创建此代码时不会以相同的方式响应。所以我解决它...
这是适合我的代码

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [ {name: "Update from Trello", functionName: "main"},];
  ss.addMenu("Trello", menuEntries);

}

// trello variables
var api_key = "XXXXX";
var api_token = "XXXXX";
var board_id = "XXXXX";
var url = "https://api.trello.com/1/";


var key_and_token = "key="+api_key+"&token="+api_token;


//called by google docs apps
function main() {
  var ss = SpreadsheetApp.getActiveSheet().clear();
  ss.appendRow(["Date", "Task", "Who", "List", "Link"]);
  var response = UrlFetchApp.fetch(url + "boards/" + board_id + "/lists?cards=all&" + key_and_token);
  var lists = JSON.parse((response.getContentText()));
  Logger.log(lists);

  for (var i=0; i < lists.length; i++) {
    var list = lists[i];
    //if(!list.closed) continue; //ignore open
    //Logger.log(list);

      var response = UrlFetchApp.fetch(url + "list/" + list.id + "/cards?" + key_and_token);
      var cards = JSON.parse(response.getContentText());
      if(!cards) continue;



    for (var j=0; j < cards.length; j++) {
      var card = cards[j];
      Logger.log(url + "cards/" + card.id + "/actions?" + key_and_token);
      var response = UrlFetchApp.fetch(url + "cards/" + card.id + "/?actions=all&" + key_and_token);
      var carddetails = JSON.parse(response.getContentText()).actions;
      if(!carddetails) continue;
      Logger.log(carddetails);

    for (var k=0; k < carddetails.length; k++) {

      var dato = carddetails[k].date;
      var fullname = carddetails[k].memberCreator.fullName;
      var name = card.name;
      var link = card.url;
      var listname = list.name;

      //var labelname = carddetails[k].labeldetails.name;

      Logger.log(name);
      Logger.log(dato);
      Logger.log(fullname);
      Logger.log(link);
      //Logger.log(labelname);
      Logger.log(listname);
    }
      ss.appendRow([dato, name, fullname, listname, link]);

   }                                      
  }
}