-
-
Save webmaxru/f03126b342d752bf321f3dae848cad98 to your computer and use it in GitHub Desktop.
Google Forms to Trello
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var authentication = "key=...&token=..."; | |
var idBoard = "..."; | |
var inboxList = "..."; | |
var updatedLabel = "..."; | |
var travelLabel = "..."; | |
// To process all current records - run sendAll() | |
// To set up a trigger for the new records - run init() | |
function init() { | |
var triggers = ScriptApp.getProjectTriggers(); | |
var form = FormApp.getActiveForm(); | |
// Delete all triggers before making a brand new one. | |
for(var i in triggers) { | |
ScriptApp.deleteTrigger(triggers[i]); | |
} | |
// Set up a new trigger | |
ScriptApp.newTrigger('submitLatestToTrello') | |
.forForm(form) | |
.onFormSubmit() | |
.create(); | |
Logger.log('Successful creation of new submitToTrello trigger.'); | |
} | |
function submitLatestToTrello(e) { | |
var form = FormApp.getActiveForm(); | |
var latestItemResponses = form.getResponses().pop(); | |
saveToTrello({response: latestItemResponses}) | |
} | |
function getField(itemResponses, fieldName) { | |
var titles = []; | |
for (var i in itemResponses) { | |
if (itemResponses[i].getItem().getTitle() === fieldName && itemResponses[i].getResponse().length > 0) { | |
return itemResponses[i].getResponse(); | |
} | |
titles.push(itemResponses[i].getItem().getTitle()); | |
} | |
throw "Field [" + fieldName + "] not found in [" + titles + "]"; | |
} | |
function getCardName(response) { | |
var itemResponses = response.getItemResponses(); | |
return getField(itemResponses, "Название доклада") + | |
" (" + getField(itemResponses, "Фамилия Имя Отчество ") + | |
" - " + getField(itemResponses, "Где работаете?") + ")"; | |
} | |
function getCardLabelIds(response) { | |
var itemResponses = response.getItemResponses(); | |
var labelResponse = UrlFetchApp.fetch("https://api.trello.com/1/boards/" + idBoard + "/labels?" + authentication); | |
var labelsJson = JSON.parse(labelResponse.getContentText()); | |
var idLabels = labelsJson.filter(function(label) { | |
return label.name === getField(itemResponses, "Секция"); | |
}).map(function(label) { return label.id; }); | |
var travelFieldValue = getField(itemResponses, "Требуется ли вам оплата проезда и проживания с нашей стороны") | |
if (travelFieldValue === "Да — я приеду из другого города России" || travelFieldValue === "Да — я зарубежный гость") { | |
idLabels.push(travelLabel); | |
} | |
return idLabels; | |
} | |
function getCardDesc(response) { | |
var itemResponses = response.getItemResponses(); | |
var body = ""; | |
for (var j in itemResponses) { | |
var itemResponse = itemResponses[j]; | |
if (itemResponse.getResponse().length == 0) { | |
continue; | |
} | |
if (itemResponse.getItem().getType() == FormApp.ItemType.PARAGRAPH_TEXT) { | |
body += "\n## " + itemResponse.getItem().getTitle() + "\n" + itemResponse.getResponse() + "\n\n"; | |
} else { | |
body += "**" + itemResponse.getItem().getTitle() + ":** " + itemResponse.getResponse() + "\n"; | |
} | |
} | |
return body; | |
} | |
function saveToTrello(args) { | |
var response = args.response; | |
var cardId = PropertiesService.getDocumentProperties().getProperty("trello_card_id_" + response.getId()); | |
if (cardId) { | |
updateCard(cardId, response); | |
} else { | |
createCard(response); | |
} | |
} | |
function sendAll() { | |
var responses = FormApp.getActiveForm().getResponses(); | |
for (var i in responses) { | |
saveToTrello({response: responses[i]}); | |
} | |
} | |
function updateCard(cardId, response) { | |
var idLabels = getCardLabelIds(response); | |
idLabels.push(updatedLabel); | |
var payload = { | |
name: getCardName(response), | |
desc: getCardDesc(response), | |
closed: false, | |
idLabels: idLabels.join(',') | |
}; | |
var url = 'https://api.trello.com/1/cards/' + cardId + '?' + authentication; | |
var resp = UrlFetchApp.fetch(url, {"method" : "PUT", "payload" : payload, muteHttpExceptions: true}); | |
Logger.log(resp); | |
if (resp.getResponseCode() == 404) { | |
createCard(response); | |
} | |
} | |
function createCard(response) { | |
var payload = { | |
name: getCardName(response), | |
desc: getCardDesc(response), | |
idList: inboxList, | |
idLabels: getCardLabelIds(response).join(',') | |
}; | |
var url = 'https://api.trello.com/1/cards?' + authentication; | |
var resp = UrlFetchApp.fetch(url, {"method" : "POST", "payload" : payload}); | |
Logger.log(resp); | |
var responseJson = JSON.parse(resp.getContentText()); | |
PropertiesService.getDocumentProperties().setProperty("trello_card_id_" + response.getId(), responseJson.id); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment