Skip to content

Instantly share code, notes, and snippets.

@cmoore4
Last active August 29, 2015 13:56
Show Gist options
  • Save cmoore4/9118653 to your computer and use it in GitHub Desktop.
Save cmoore4/9118653 to your computer and use it in GitHub Desktop.
Trello Board Cards to CSV File
(function () {
function JSON2CSV(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
var line = '';
var head = array[0];
for (var index in array[0]) {
var value = index + "";
line += '"' + value.replace(/"/g, '""') + '",';
}
line = line.slice(0, -1);
str += line + '\r\n';
for (var i = 0; i < array.length; i++) {
line = '';
for (var index in array[i]) {
var value = array[i][index] + "";
line += '"' + value.replace(/"/g, '""') + '",';
}
line = line.slice(0, -1);
str += line + '\r\n';
}
return str;
}
function trelloParse(trello) {
var cards = trello.cards;
var cardsSimple = cards.map(function (e, i) {
if (!e.closed) {
var labels = [];
for (var ii = 0; ii < e.labels.length; ii++) {
labels.push(e.labels[ii].name);
}
labels = labels.join(', ');
var item = {
"name": e.name,
"url": e.url,
"lastUpdate": e.dateLastActivity,
"dueDate": ((e.due) ? e.due : ''),
"users": e.idMembers,
"labels": labels,
"status": e.idList
};
return item;
}
return false;
});
var cardsFlat = cardsSimple.filter(function (e) {
if (e === false) {
return false;
}
return true;
});
var members = {};
trello.members.forEach(function (member, idx) {
members[member.id] = member.fullName;
});
cardsFlat.forEach(function (el, i) {
if (el.users.length) {
var users = [];
el.users.forEach(function (uid, idx) {
users.push(members[uid]);
});
el.users = users.join(', ');
} else {
el.users = '';
}
});
var lists = {};
trello.lists.forEach(function (list, idx) {
lists[list.id] = list.name;
});
cardsFlat.forEach(function (el, i) {
el.status = lists[el.status];
});
var csv = JSON2CSV(cardsFlat);
console.log(csv);
return csv;
}
$.get($('.js-export-json').attr('href'), function (trelloJSON) {
window.open("data:text/csv;charset=utf-8," + escape(trelloParse(trelloJSON)));
});
})()
@cmoore4
Copy link
Author

cmoore4 commented Feb 20, 2014

To make this work, you need to have the sidebar opened, and have clicked on "Share, Print, and Export" so that the modal box is open. Then execute this as a bookmarket or from your console.

A quick link to the bookmaklet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment