-
Notifications
You must be signed in to change notification settings - Fork 0
/
export-to-pipedrive.js
407 lines (304 loc) · 12.4 KB
/
export-to-pipedrive.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
const DATA_SHEET_NAME = "Person data";
const CONFIG_SHEET_NAME = "Config";
const PIPEDRIVE_API = "B1";
const FIELDS_TO_BE_MERGED = "B2"
const EMAIL_ID = "B3";
const ORGANIZATION = "B4";
const TIME_ZONE = "B5";
const FIELD_KEYS_SHEET_NAME = "PersonField keys"
var person_data = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(DATA_SHEET_NAME);
var config_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(CONFIG_SHEET_NAME);
var field_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(FIELD_KEYS_SHEET_NAME);
var pipedriveAPIKey = config_sheet.getRange(PIPEDRIVE_API).getValue();
var fieldsToBeMerged = config_sheet.getRange(FIELDS_TO_BE_MERGED).getValue();
var columnAlphabet = config_sheet.getRange(EMAIL_ID).getValue();
var timezone = config_sheet.getRange(TIME_ZONE).getValue();
var fieldsToBeMergedArray = fieldsToBeMerged.split(",");
var headerRow = person_data.getRange(1, 1, 1, person_data.getLastColumn()).getValues()[0];
var columnIndex = columnAlphabet.charCodeAt(0) - 65 + 1;
//Fetching key to the corresponding label
var labelsInSheet = field_sheet.getRange(2, 1, field_sheet.getLastRow(), 1).getValues().flat();
var keysInSheet = field_sheet.getRange(2, 2, field_sheet.getLastRow(), 1).getValues().flat();
var typesInSheet = field_sheet.getRange(2, 3, field_sheet.getLastRow(), 1).getValues().flat();
var organizations = {};
var labelToKeyMap = {};
for (var i = 0; i < labelsInSheet.length; i++) {
labelToKeyMap[labelsInSheet[i]] = keysInSheet[i];
}
function checkAndUpdateContacts() {
var dataRange = person_data.getDataRange();
var dataValues = dataRange.getValues();
for (var i = 1; i < dataValues.length; i++) { //dataValues = Object that contains all the rows arrays
var columnLabelsWithValues = [];
var row = dataValues[i];
var isUpdated = row[1]; // "Record uploaded?" column is the second column (index 1)
if (isUpdated !== "Done") {
var rowRange = person_data.getRange(i+1, 1, 1, person_data.getLastColumn());
var rowValues = rowRange.getValues()[0];
for (var j = 2; j < rowValues.length; j++) { //rowValues = Array of all columns in that row
if (rowValues[j]) {
columnLabelsWithValues.push(headerRow[j]);
}
}
var rowData = row.slice(0, 28); // Extract the first 28 columns of data
var email = person_data.getRange(i + 1, columnIndex).getValue(); //Fetching the email to check whether that contact exist
// Check if the email exists as a contact in Pipedrive
var contactData = checkContactExistsInPipedrive(email);
try{
if (contactData) { //If contact exist, then update
updateContactInPipedrive(contactData,rowData,columnLabelsWithValues);
person_data.getRange(i+1 ,2).setValue("Done");
} else {
addContactInPipedrive(rowData,columnLabelsWithValues);
person_data.getRange(i+1 ,2).setValue("Done");
}
} catch (error){
Logger.log(`Error adding/updating contact in Pipedrive: ${error}`);
person_data.getRange(i+1 ,2).setValue("Failed");
}
}
}
}
function checkContactExistsInPipedrive(email) {
var url = `https://api.pipedrive.com/v1/persons/search?term=${email}&exact_match=1&api_token=${pipedriveAPIKey}`;
var response = UrlFetchApp.fetch(url);
var data = JSON.parse(response.getContentText());
if (data.data && data.data.items && data.data.items.length > 0) {
return data.data.items[0].item.id;
} else {
return false; // Person with the given email not found
}
}
function updateContactInPipedrive(person_id, rowData, columnLabelsWithValues) {//Updating an existing contact in Pipedrive
let keysForLabels = fetchKeysForLabels(columnLabelsWithValues);
var updatedValues = [];
var payload = {};
for(k = 2; k < rowData.length; k++){
if(rowData[k]){
updatedValues.push(rowData[k])
}
}
for (var i = 0; i < keysForLabels.length; i++) {
if(fetchTypeForFieldKey(keysForLabels[i]) === "Multiple options" || fetchTypeForFieldKey(keysForLabels[i]) === "Single option"){ //Need to check whether the current field type is 'Multiple options' or 'Single option'
var optionValues = updatedValues[i].split(",");
for (var a = 0; a < optionValues.length; a++){
let customFieldOptions = checkForExistingOption(optionValues[a],keysForLabels[i]); //if the provided value exist
var noOptionExist = customFieldOptions[0];
var customFieldId = customFieldOptions[1];
if(noOptionExist){
addOptionToCustomField(keysForLabels[i],optionValues[a],customFieldId);
console.log(noOptionExist);
}
}
}
if(fetchTypeForFieldKey(keysForLabels[i]) === "Date"){
updatedValues[i] = convertDateFormat(updatedValues[i]);
}
if(fetchTypeForFieldKey(keysForLabels[i]) === "Organization"){
if(!organizations){
fetchAllOrganization();
}
checkOrganizationExist
}
for(x = 0; x < fieldsToBeMergedArray.length; x++){
if(columnLabelsWithValues[i]==fieldsToBeMergedArray[x]){ //Checks whether the label current column label matches any of the field name mentioned in the config sheet
var personData = getContactCustomFieldData(person_id);
var previousOptions = personData[labelToKeyMap[columnLabelsWithValues[i]]];
updatedValues[i] = previousOptions + ',' + updatedValues[i]
console.log(updatedValues[i]);
}
}
payload[keysForLabels[i]] = updatedValues[i];
}
var url = `https://api.pipedrive.com/v1/persons/${person_id}?api_token=${pipedriveAPIKey}`;
//console.log(payload);
var options = {
method: "PUT",
contentType: "application/json",
payload: JSON.stringify(payload)
};
var response = UrlFetchApp.fetch(url, options);
console.log(response);
}
function addContactInPipedrive(rowData,columnLabelsWithValues){//Adding new contacts inside Pipedrive
let keysForLabels = fetchKeysForLabels(columnLabelsWithValues);
var updatedValues = [];
var payload = {};
for(k = 2; k < rowData.length; k++){
if(rowData[k]){
updatedValues.push(rowData[k])
}
}
for (var i = 0; i < keysForLabels.length; i++) {//Need to add an IF condition for multiple options
if(keysForLabels[i] == "first_name"){
payload["name"] = updatedValues[i] + ' ';
//console.log(updatedValues[i]);
}
if(keysForLabels[i] == "last_name"){
payload["name"] += updatedValues[i]
//console.log(updatedValues[i]);
}
if(fetchTypeForFieldKey(keysForLabels[i]) === "Date"){
updatedValues[i] = convertDateFormat(updatedValues[i]);
}
if(fetchTypeForFieldKey(keysForLabels[i]) === "Multiple options" || fetchTypeForFieldKey(keysForLabels[i]) === "Single option"){ //Need to check whether the current field type is 'Multiple options' or 'Single option'
var optionValues = updatedValues[i].split(",");
for (var a = 0; a < optionValues.length; a++){
let customFieldOptions = checkForExistingOption(optionValues[a],keysForLabels[i]); //if the provided value exist
var noOptionExist = customFieldOptions[0];
var customFieldId = customFieldOptions[1];
if(noOptionExist){
addOptionToCustomField(keysForLabels[i],optionValues[a],customFieldId);
console.log(noOptionExist);
}
}
}
payload[keysForLabels[i]] = updatedValues[i];
}
var url = `https://api.pipedrive.com/v1/persons?api_token=${pipedriveAPIKey}`;
var options = {
method: "POST",
contentType: "application/json",
payload: JSON.stringify(payload)
};
var response = UrlFetchApp.fetch(url, options);
console.log(response);
}
function fetchKeysForLabels(columnLabelsWithValues) {//Fetching field_keys from PersonField Key sheet
var keysForLabels = [];
for (var i = 0; i < columnLabelsWithValues.length; i++) {
var label = columnLabelsWithValues[i];
if (labelToKeyMap[label]) {
keysForLabels.push(labelToKeyMap[label]);
}
}
return keysForLabels;
}
function fetchTypeForFieldKey(fieldKey){
for (var i = 1; i < typesInSheet.length; i++){
var fieldkey = keysInSheet[i];
var type = typesInSheet[i];
if (fieldkey === fieldKey) {
return type;
}
}
}
function checkForExistingOption(option,fieldkey){ //Option ID can be fetched from here as well
var url = `https://api.pipedrive.com/v1/personFields?api_token=${pipedriveAPIKey}`;
var pfResponse = UrlFetchApp.fetch(url);
var pfData = JSON.parse(pfResponse.getContentText());
var noOptionExist = true;
var customFieldId = '';
for(i = 0; i < pfData.data.length ; i++){
if(pfData.data[i].key == fieldkey){
for(j = 0; j < pfData.data[i].options.length ; j++){
if(pfData.data[i].options[j].label == option){ //option exist
noOptionExist = false;
}
customFieldId = pfData.data[i].id;
}
}
}
var returnValue = [noOptionExist,customFieldId];
return returnValue;
}
function addOptionToCustomField(customFieldKey,optionName,customFieldId){
var dataArray = getAllCustomFieldsData();
var customFieldObject = dataArray.find(function(data) {return data.key === customFieldKey;});
var optionArray = optionName.split(',');
var outputArray = [];
for (var i = 0; i < optionArray.length; i++) {
var obj = {
label: optionArray[i]
};
outputArray.push(obj);
}
var newAppendedOptions = customFieldObject.options.concat(outputArray);
var apiurl = `https://api.pipedrive.com/v1/personFields/${customFieldId}?api_token=${pipedriveAPIKey}`
let payload = {
name: customFieldObject.name,
add_visible_flag: true
};
payload.options = newAppendedOptions;
var options = {
method: "PUT",
contentType: "application/json",
payload: JSON.stringify(payload)
};
const response = UrlFetchApp.fetch(apiurl, options);
const responseData = JSON.parse(response.getContentText());
console.log("addOptionToCustomFieldsData" + responseData);
}
function getAllCustomFieldsData(){
var url = `https://api.pipedrive.com/v1/personFields?api_token=${pipedriveAPIKey}`;
try {
var response = UrlFetchApp.fetch(url);
var responseData = JSON.parse(response.getContentText());
if (responseData.data) {
return responseData.data;
} else {
Logger.log("Failed to fetch person data.");
return null;
}
} catch (error) {
Logger.log(`Error fetching person data: ${error}`);
return null;
}
}
function getContactCustomFieldData(person_id) {
var apiUrl = `https://api.pipedrive.com/v1/persons/${person_id}?api_token=${pipedriveAPIKey}`;
try {
var response = UrlFetchApp.fetch(apiUrl);
var responseData = JSON.parse(response.getContentText());
if (responseData.data) {
return responseData.data;
} else {
Logger.log("Failed to fetch person data.");
return null;
}
} catch (error) {
Logger.log(`Error fetching person data: ${error}`);
return null;
}
}
function fetchAllOrganization(){
var url = `https://api.pipedrive.com/v1/organizations?api_token=${pipedriveAPIKey}`;
var response = UrlFetchApp.fetch(url);
var data = JSON.parse(response.getContentText());
for(i = 0; i < data.data.length; i++){
organizations[data.data[i].name]=data.data[i].id;
}
}
function checkOrganizationExist(org_name){
if(organizations[org_name]){
return organizations[org_name];
}
}
function importPipedriveCustomFieldData(){
var url = `https://api.pipedrive.com/v1/personFields?api_token=${pipedriveAPIKey}`;
var response = UrlFetchApp.fetch(url);
var data = JSON.parse(response.getContentText());
for(i = 0; i < data.data.length; i++){
var personFieldName = data.data[i].name;
var personFieldKey = data.data[i].key;
var newRow = [personFieldName,personFieldKey];
var personFieldkeys = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PersonField keys');
personFieldkeys.appendRow(newRow);
}
}
function convertDateFormat(date) {
// Split the input date into day, month, and year
var dateParts = date.split('/');
// Create a new Date object with the parsed components
var date = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
// Format the date as yyyy-MM-dd HH:mm:ss
var formattedDate = Utilities.formatDate(date, timezone, "yyyy-MM-dd HH:mm:ss");
return formattedDate;
}
function onOpen(){
const ui = SpreadsheetApp.getUi()
ui.createMenu('Pipedrive')
.addItem('Download Custom Field', 'importPipedriveCustomFieldData')
.addItem('Export to Pipedrive', 'checkAndUpdateContacts')
.addToUi();
}