-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathgoogle-sheets-functions-to-trello-api.gs
More file actions
129 lines (110 loc) · 3.7 KB
/
Copy pathgoogle-sheets-functions-to-trello-api.gs
File metadata and controls
129 lines (110 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// How to generate a app key - https://trello.com/app-key/
// How to generate a app token - https://trello.com/1/authorize?expiration=never&scope=read,write,account&response_type=token&name=Server%20Token&key=[YOUR KEY]
//Create a board cloned from a template
function cloneBoard(board,member) {
//Get name of board template
var url_source = 'https://api.trello.com/1/boards/'
+ board
+ '/name?key=[YOUR KEY]&token=[YOUR TOKEN]';
var options_source =
{
"method" : "GET",
"followRedirects" : true,
"muteHttpExceptions": true
};
var response_source = UrlFetchApp.fetch(url_source, options_source);
var json_source = response_source.getContentText();
var data_source = JSON.parse(json_source);
var name_source = data_source._value;
// Create board
var name = name_source + ' - ' + member; //name the new board
var url = 'https://api.trello.com/1/boards/'
+ '?idBoardSource='
+ board
+ '&name='
+ name
+ '&keepFromSource=all'
+ '&idOrganization=5afaf2fc5ce64b274bf9e1d4'
+ '&prefs_permissionLevel=public'
+ '&key=[YOUR KEY]&token=[YOUR TOKEN]';
var options =
{
"method" : "POST",
"followRedirects" : true,
"muteHttpExceptions": true
};
var response = UrlFetchApp.fetch(url, options);
var json = response.getContentText();
Logger.log(response);
var data = JSON.parse(json);
//Add member
var add_member = adicionaMembro(data.id,member); //call addMember function
return data.shortUrl; //return new trello board
}
//Add a member to a specific board
function addMember(board,member) {
var url = 'https://api.trello.com/1/boards/'
+ board
+ '/members/'
+ member
+ '?allowBillableGuest=false&type=normal&key=[YOUR KEY]&token=[YOUR TOKEN]';
var options =
{
"method" : "PUT",
"followRedirects" : true,
"muteHttpExceptions": true
};
var response = UrlFetchApp.fetch(url, options);
return 1;
}
// Remove user from board using shortUrl
function excludeMember(row) {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Participantes");
var member = sheet.getRange("B" + row).getValue();
var email = sheet.getRange("E" + row).getValue();
var name = sheet.getRange("C" + row).getValue();
var boardUrl = sheet.getRange("J"+ row).getValue();
var boardId = "";
var idOrganization = '[YOUR Organization]';
//Search for board
var urlSearch = 'https://api.trello.com/1/organizations/'
+ idOrganization
+ '/boards/'
+ '?key=[YOUR KEY]&token=[YOUR TOKEN]';
var optionsSearch =
{
"method" : "GET",
"followRedirects" : true,
"muteHttpExceptions": true
};
var responseSearch = UrlFetchApp.fetch(urlSearch, optionsSearch);
var jsonSearch = responseSearch.getContentText();
var dataSearch = JSON.parse(jsonSearch);
for (var i = 0; i < dataSearch.length; i++) {
if (dataSearch[i].shortUrl == boardUrl) {
boardId = dataSearch[i].id;
break;
}
}
//Remove member from board
var urlRemove = 'https://api.trello.com/1/boards/'
+ boardId
+ '/members/'
+ member
+ '?key=[YOUR KEY]&token=[YOUR TOKEN]';
var options =
{
"method" : "DELETE",
"followRedirects" : true,
"muteHttpExceptions": true
};
var responseRemove = UrlFetchApp.fetch(urlRemove, options);
Logger.log(responseRemove);
if (responseRemove == "membership not found") {
return "User already excluded, we won't send the email";
}else{
sendEmail(email,name); //Other GAS Script at - https://github.com/efremfilho/GoogleAppsScripts/blob/master/sendEmailViaSendGrid.gs
return "User removed and we sent a email from template";
}
}