Before you get started, ensure you have:
A Mailercloud account with API access.
The Mailercloud API key.
The Mailercloud List ID where contacts will be added.
Step 1: Setting Up Google Apps Script
To access Google Apps Script within your Google Sheets document:
4. Add the code in the editor section in app script .
Step 2: Writing the create contact from sheet function
In the Apps Script editor, add the following code to create a contact in Mailercloud from the last row of your Google Sheet:
function createContactFromSheet() {
// Get the active Google Sheet and specify the sheet name
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
// Get the last row with data in column A (assuming it's a single-column list)
var lastRow = sheet.getLastRow();
// Get the data in the last row
var rowData = sheet.getRange(lastRow, 1, 1, 2).getValues()[0]; // Assumes name is in column A and email is in column B
var dataInColumn1 = sheet.getRange(lastRow, 1).getValue();
var dataInColumn2 = sheet.getRange(lastRow, 2).getValue();
// Check if both name and email have values
var name = rowData[0];
var email = rowData[1];
// Call the Numbers API for random math fact
var apiUrl = "https://cloudapi.mailercloud.com/v1/contacts";
// Define your API key or any other required headers
var headers = {
"Authorization": "**************************************************", // Replace with your actual API key
"Content-Type": "application/json"
};
if (name && email) {
// Define the contact data you want to send to the API
var payload = {
"email": email,
"name": name,
"list_id":"bAIUjI"
// Add other contact properties as needed
};
// Make an HTTP POST request to create the contact
var options = {
"method": "post",
"headers": headers,
"payload": JSON.stringify(payload)
};
// Send the request
var response = UrlFetchApp.fetch(apiUrl, options);
// Parse the response JSON (if the API returns JSON)
var responseData = JSON.parse(response.getContentText());
// You can log the response or perform other actions based on the API's response
Logger.log(responseData);
Logger.log(response.getContentText());
}
}
Please replace "YOUR_API_KEY" and "YOUR_LIST_ID" with your actual Mailercloud API key and List ID respectively.
Step 3: Writing the on edit trigger function
The following function will trigger the contact creation whenever an edit is made to the name and email columns (columns A and B) in your specified sheet:
function onEdit(e) {
var sheet = e.source.getActiveSheet();
if (sheet.getName() === "Sheet1" && e.range.columnStart === 1 && e.range.columnEnd === 2) {
// This assumes that the name and email are in columns A and B (1 and 2) and a change has been made in these columns.
createContactFromSheet();
}
}
This assumes that the name and email are in columns A and B (1 and 2) respectively, and a change has been made in these columns.
Step 4: Setting Up Trigger in Apps Script Editor
To set up a trigger for the on edit function in your Apps Script editor, perform the following steps:
Now, every time you add a contact in your Google Sheet, this contact information will automatically be added to your Mailercloud account.
Note : For updating existing contacts, users can utilize our contact update API. To learn more about our available APIs and perform various actions, please visit our API documentation at https://apidoc.mailercloud.com/. Users will need to modify the API URL in their script accordingly.