Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function doPost(e) {
- var ss = SpreadsheetApp.getActiveSpreadsheet();
- try {
- var jsonData = JSON.parse(e.parameter.rawRequest);
- addRowToSheet(jsonData)
- createOrUpdateContact(jsonData);
- } catch (error) {
- console.log(error)
- var sheet = ss.getSheetByName('ERRORS');
- sheet.appendRow([new Date(), "ERROR", error, JSON.stringify(e)])
- }
- }
- function addRowToSheet(jsonData) {
- var ss = SpreadsheetApp.getActiveSpreadsheet();
- var sheet = ss.getSheetByName('Data');
- // Get the header row (row 1)
- var headerRow = sheet.getDataRange().getValues()[0];
- // Flatten the JSON data
- var flattenedData = flattenJson(jsonData);
- console.log(JSON.stringify(flattenedData));
- // Iterate over the flattened data and add missing keys to the header row
- Object.keys(flattenedData).forEach(function (key) {
- if (!headerRow.includes(key)) {
- sheet.getRange(1, headerRow.length + 1).setValue(key);
- headerRow.push(key);
- }
- });
- // Create a new row array based on the flattened data
- var newRow = headerRow.map(function (header) {
- var value = flattenedData[header];
- return value !== undefined ? value : '';
- });
- // Append the new row to the sheet
- sheet.appendRow(newRow);
- Logger.log('New row added successfully.');
- }
- function flattenJson(obj, parentKey = '') {
- let flattened = {};
- for (let key in obj) {
- if (obj.hasOwnProperty(key)) {
- let newKey = parentKey ? parentKey + '_' + key : key;
- if (typeof obj[key] === 'object' && obj[key] !== null) {
- let nested = flattenJson(obj[key], newKey);
- flattened = { ...flattened, ...nested };
- } else {
- flattened[newKey] = obj[key];
- }
- }
- }
- return flattened;
- }
- function createOrUpdateContact(jsonData) {
- var existingContact = getContactByEmail(jsonData.q49_typeA49);
- if(existingContact) {
- // Update the existing contact
- updateContact(existingContact, jsonData);
- Logger.log("Contact updated: " + existingContact.resourceName);
- } else {
- // Create a new contact
- createContact(jsonData);
- Logger.log("Contact created");
- }
- }
- function getContactByEmail(email) {
- var response = People.People.searchContacts({
- query: email,
- pageSize: 1,
- readMask: "emailAddresses,names"
- });
- var response = People.People.searchContacts({
- query: email,
- pageSize: 1,
- readMask: "emailAddresses,names"
- });
- console.log(email)
- console.log(JSON.stringify(response))
- if(response && response.results && response.results.length > 0) {
- return response.results[0];
- } else {
- return null;
- }
- }
- function updateContact(contact, jsonData) {
- var resource = {
- resourceName: contact.person.resourceName,
- etag: contact.person.etag,
- names: [{
- givenName: jsonData.q3_parentName3.first,
- familyName: jsonData.q3_parentName3.last
- }],
- emailAddresses: [{
- value: jsonData.q49_typeA49,
- type: "home"
- }],
- phoneNumbers: [{
- value: jsonData.q34_parentPhone.full,
- type: "Parent Phone"
- }, {
- value: jsonData.q35_emergencyContact35.full,
- type: "Emergency Contact1"
- }, {
- value: jsonData.q36_emergencyContact36.full,
- type: "Emergency Contact2"
- }],
- addresses: [{
- streetAddress: jsonData.q15_mailingAddress.addr_line1,
- extendedAddress: jsonData.q15_mailingAddress.addr_line2,
- city: jsonData.q15_mailingAddress.city,
- region: jsonData.q15_mailingAddress.state,
- postalCode: jsonData.q15_mailingAddress.postal,
- type: "home"
- }],
- birthdays: [{
- date: {
- year: Number(jsonData.q6_camperDate6.year),
- month: Number(jsonData.q6_camperDate6.month),
- day: Number(jsonData.q6_camperDate6.day)
- }
- }]
- };
- var updatedContact = People.People.updateContact(resource, contact.person.resourceName, {
- updatePersonFields: "names,emailAddresses,phoneNumbers,addresses,birthdays" // Specify the fields to be updated
- });
- return updatedContact;
- }
- function createContact(jsonData) {
- var resource = {
- names: [{
- givenName: jsonData.q3_parentName3.first,
- familyName: jsonData.q3_parentName3.last
- }],
- emailAddresses: [{
- value: jsonData.q49_typeA49,
- type: "home"
- }],
- phoneNumbers: [{
- value: jsonData.q34_parentPhone.full,
- type: "Parent Phone"
- }, {
- value: jsonData.q35_emergencyContact35.full,
- type: "Emergency Contact1"
- }, {
- value: jsonData.q36_emergencyContact36.full,
- type: "Emergency Contact2"
- }],
- addresses: [{
- streetAddress: jsonData.q15_mailingAddress.addr_line1,
- extendedAddress: jsonData.q15_mailingAddress.addr_line2,
- city: jsonData.q15_mailingAddress.city,
- region: jsonData.q15_mailingAddress.state,
- postalCode: jsonData.q15_mailingAddress.postal,
- type: "home"
- }],
- birthdays: [{
- date: {
- year: Number(jsonData.q6_camperDate6.year),
- month: Number(jsonData.q6_camperDate6.month),
- day: Number(jsonData.q6_camperDate6.day)
- }
- }]
- };
- var contact = People.People.createContact(resource);
- return contact;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement