Skip to content

Instantly share code, notes, and snippets.

@peterfoxflick
Last active June 15, 2018 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterfoxflick/ed2f42e09af171f539226b0a85e72963 to your computer and use it in GitHub Desktop.
Save peterfoxflick/ed2f42e09af171f539226b0a85e72963 to your computer and use it in GitHub Desktop.
Trello Card Counter

Trello Count

This is program will do two things:

  1. Count the number of trello cards in each list on a board
  2. Add compleated courses to a spreadsheet in google This code is ran as a google script.
function countTrello() {
  // Trello api key found at: https://trello.com/app-key
  var key = '';
  // Trello api token found by clicking on the token link at https://trello.com/app-key
  var token = '';
  // Trello board id, this will be the board to count the trello cards on. This is easy to find, simply copy it from the url when you view the board (https://trello.com/b/COPY_THIS_PART/name-of-board)
  var boardid = '';
  // Id of the trello card to add the stats to
  var cardid = '';
  // id of the trello list that cards are moved to when considered complete
  var listid = '';
  // id of google sheet to add list of compleated cards
  var sheetid = '-Ly9-vySDbjyWN2g70A';
  
  var trelloResponce = UrlFetchApp.fetch('https://api.trello.com/1/boards/' + boardid + '/lists?cards=open&card_fields=none&filter=open&fields=name&key=' + key + '&token=' + token );
  data = JSON.parse(trelloResponce.getContentText());
  var des = "";
  
  for(var i = 0; i < data.length; i++){
    des += data[i].cards.length + ": " + data[i].name + "\n";
  }
  
  var now = new Date();
  des += "\n\n Last Updated: " + now;
  var trelloParams = {
     key: key,
     token: token,
     desc: des
   }
  
  var options = {
    method: 'PUT',
    contentType: 'application/json',
    payload: JSON.stringify(trelloParams)
  };
  
  UrlFetchApp.fetch('https://api.trello.com/1/cards/' + cardid + '?', options);

  // Check to see if there are any finished courses
  // If so then add them to the finalized course list
  if(data[14].cards.length > 0 ){
    var trelloResponce = UrlFetchApp.fetch('https://api.trello.com/1/lists/' + listid + '/cards?key=' + key + '&token=' + token);
    data = JSON.parse(trelloResponce.getContentText());
   
    var sheet = SpreadsheetApp.openById(sheetid).getSheets()[1];  
    var range = sheet.getRange('C:C').getValues();
    
    for(var j = 0; j<data.length; j++){
      var found = false;
      for(var i = 1; i < range.length && !found; i++){
        if(range[i][0] == data[j].id){
          found = true;
        }
      }
      if(!found){
        sheet.appendRow([data[j].name, now, data[j].id])
      }
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment