PreviousIntegrated Webhooks - Sending data from SprintHub to an external WebHook via automation
NextIntegrated Webhooks - Calendly to SprintHub
Last updated
function onEdit(e) {
var sheetName = 'Leads';
var sheet = e.source.getSheetByName(sheetName);
if (!sheet || e.range.getSheet().getName() !== sheetName) {
return;
}
// Checks whether the edit is on the last row (assumes a new row is added manually)
var lastRow = sheet.getLastRow();
if (e.range.getRow() === lastRow) {
sendLinesToWebhook(sheet, lastRow, lastRow);
}
}
function sendLinesToWebhook(sheet, startRow, endRow) {
var url = 'https://sprinthub-api-master.sprinthub.app/api/hook/gxxxxxets?i=temxxxxp&access_token=ziqTxxxxxx0-X_';
for (var row = startRow; row <= endRow; row++) {
var line = sheet.getRange(row, 1, 1, sheet.getLastColumn()).getValues()[0];
// Checks whether the required fields are filled in
if (line[0] === '' || line[2] === '') {
Logger.log('Row ' + row + ' contains required empty cells.');
continue;
}
// Preparing the data to send in the payload
var payload = {
'name': line[0],
'surname': line[1],
'cellphone': line[2],
'email': line[3]
};
Logger.log('Sent payload:', payload);
var options = {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(payload),
'muteHttpExceptions': true
};
try {
var response = UrlFetchApp.fetch(url, options);
Logger.log('Webhook response for row ' + row + ':', response.getContentText());
} catch (e) {
Logger.log('Error sending the payload for row ' + row + ':', e.message);
}
// Wait 1 second before sending the next row
Utilities.sleep(1000);
}
}