Advertisement
anonydee

Untitled

Jun 28th, 2023
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | Source Code | 0 0
  1. function doPost(e) {
  2. var ss = SpreadsheetApp.getActiveSpreadsheet();
  3.  
  4. try {
  5. var jsonData = JSON.parse(e.parameter.rawRequest);
  6.  
  7. addRowToSheet(jsonData)
  8. createOrUpdateContact(jsonData);
  9. } catch (error) {
  10. console.log(error)
  11. var sheet = ss.getSheetByName('ERRORS');
  12. sheet.appendRow([new Date(), "ERROR", error, JSON.stringify(e)])
  13. }
  14. }
  15.  
  16. function addRowToSheet(jsonData) {
  17. var ss = SpreadsheetApp.getActiveSpreadsheet();
  18. var sheet = ss.getSheetByName('Data');
  19.  
  20. // Get the header row (row 1)
  21. var headerRow = sheet.getDataRange().getValues()[0];
  22.  
  23. // Flatten the JSON data
  24. var flattenedData = flattenJson(jsonData);
  25.  
  26. console.log(JSON.stringify(flattenedData));
  27.  
  28. // Iterate over the flattened data and add missing keys to the header row
  29. Object.keys(flattenedData).forEach(function (key) {
  30. if (!headerRow.includes(key)) {
  31. sheet.getRange(1, headerRow.length + 1).setValue(key);
  32. headerRow.push(key);
  33. }
  34. });
  35.  
  36. // Create a new row array based on the flattened data
  37. var newRow = headerRow.map(function (header) {
  38. var value = flattenedData[header];
  39. return value !== undefined ? value : '';
  40. });
  41.  
  42. // Append the new row to the sheet
  43. sheet.appendRow(newRow);
  44.  
  45. Logger.log('New row added successfully.');
  46. }
  47.  
  48. function flattenJson(obj, parentKey = '') {
  49. let flattened = {};
  50.  
  51. for (let key in obj) {
  52. if (obj.hasOwnProperty(key)) {
  53. let newKey = parentKey ? parentKey + '_' + key : key;
  54.  
  55. if (typeof obj[key] === 'object' && obj[key] !== null) {
  56. let nested = flattenJson(obj[key], newKey);
  57. flattened = { ...flattened, ...nested };
  58. } else {
  59. flattened[newKey] = obj[key];
  60. }
  61. }
  62. }
  63.  
  64. return flattened;
  65. }
  66.  
  67.  
  68. function createOrUpdateContact(jsonData) {
  69. var existingContact = getContactByEmail(jsonData.q49_typeA49);
  70. if(existingContact) {
  71. // Update the existing contact
  72. updateContact(existingContact, jsonData);
  73. Logger.log("Contact updated: " + existingContact.resourceName);
  74. } else {
  75. // Create a new contact
  76. createContact(jsonData);
  77. Logger.log("Contact created");
  78. }
  79. }
  80.  
  81. function getContactByEmail(email) {
  82. var response = People.People.searchContacts({
  83. query: email,
  84. pageSize: 1,
  85. readMask: "emailAddresses,names"
  86. });
  87. var response = People.People.searchContacts({
  88. query: email,
  89. pageSize: 1,
  90. readMask: "emailAddresses,names"
  91. });
  92. console.log(email)
  93. console.log(JSON.stringify(response))
  94. if(response && response.results && response.results.length > 0) {
  95. return response.results[0];
  96. } else {
  97. return null;
  98. }
  99. }
  100.  
  101. function updateContact(contact, jsonData) {
  102. var resource = {
  103. resourceName: contact.person.resourceName,
  104. etag: contact.person.etag,
  105. names: [{
  106. givenName: jsonData.q3_parentName3.first,
  107. familyName: jsonData.q3_parentName3.last
  108. }],
  109. emailAddresses: [{
  110. value: jsonData.q49_typeA49,
  111. type: "home"
  112. }],
  113. phoneNumbers: [{
  114. value: jsonData.q34_parentPhone.full,
  115. type: "Parent Phone"
  116. }, {
  117. value: jsonData.q35_emergencyContact35.full,
  118. type: "Emergency Contact1"
  119. }, {
  120. value: jsonData.q36_emergencyContact36.full,
  121. type: "Emergency Contact2"
  122. }],
  123. addresses: [{
  124. streetAddress: jsonData.q15_mailingAddress.addr_line1,
  125. extendedAddress: jsonData.q15_mailingAddress.addr_line2,
  126. city: jsonData.q15_mailingAddress.city,
  127. region: jsonData.q15_mailingAddress.state,
  128. postalCode: jsonData.q15_mailingAddress.postal,
  129. type: "home"
  130. }],
  131. birthdays: [{
  132. date: {
  133. year: Number(jsonData.q6_camperDate6.year),
  134. month: Number(jsonData.q6_camperDate6.month),
  135. day: Number(jsonData.q6_camperDate6.day)
  136. }
  137. }]
  138. };
  139. var updatedContact = People.People.updateContact(resource, contact.person.resourceName, {
  140. updatePersonFields: "names,emailAddresses,phoneNumbers,addresses,birthdays" // Specify the fields to be updated
  141. });
  142. return updatedContact;
  143. }
  144.  
  145. function createContact(jsonData) {
  146. var resource = {
  147. names: [{
  148. givenName: jsonData.q3_parentName3.first,
  149. familyName: jsonData.q3_parentName3.last
  150. }],
  151. emailAddresses: [{
  152. value: jsonData.q49_typeA49,
  153. type: "home"
  154. }],
  155. phoneNumbers: [{
  156. value: jsonData.q34_parentPhone.full,
  157. type: "Parent Phone"
  158. }, {
  159. value: jsonData.q35_emergencyContact35.full,
  160. type: "Emergency Contact1"
  161. }, {
  162. value: jsonData.q36_emergencyContact36.full,
  163. type: "Emergency Contact2"
  164. }],
  165. addresses: [{
  166. streetAddress: jsonData.q15_mailingAddress.addr_line1,
  167. extendedAddress: jsonData.q15_mailingAddress.addr_line2,
  168. city: jsonData.q15_mailingAddress.city,
  169. region: jsonData.q15_mailingAddress.state,
  170. postalCode: jsonData.q15_mailingAddress.postal,
  171. type: "home"
  172. }],
  173. birthdays: [{
  174. date: {
  175. year: Number(jsonData.q6_camperDate6.year),
  176. month: Number(jsonData.q6_camperDate6.month),
  177. day: Number(jsonData.q6_camperDate6.day)
  178. }
  179. }]
  180. };
  181. var contact = People.People.createContact(resource);
  182. return contact;
  183. }
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement