I have a CRM system that pushes data to my Google Sheet every day through an API connection. The CRM fills these columns:
- Column A gets a tracking ID
- Column B stays empty
- Column C gets a timestamp in UNIX format
- Column D stays empty
- Column E stays empty
I wrote a Google Apps Script to process this data and fill the empty columns:
- Column A stays the same
- Column B gets a description text
- Column C converts the UNIX timestamp to readable date
- Column D gets a number value
- Column E gets a currency code
My script works when I manually add data to test it, but it doesn’t run when the CRM API adds new rows automatically. I need the script to trigger whenever new data comes from the CRM.
function onEdit(e) {
var activeSheet = e.source.getActiveSheet();
var editedRange = e.range;
var firstRow = editedRange.getRow();
var lastRow = firstRow + editedRange.getNumRows() - 1;
for (var currentRow = firstRow; currentRow <= lastRow; currentRow++) {
// Set description in column B
activeSheet.getRange(currentRow, 2).setValue('Final - Google Ads - Offline Conversions - All Match');
// Convert UNIX timestamp in column C
var timestampCell = activeSheet.getRange(currentRow, 3);
var unixValue = timestampCell.getValue();
if (!isNaN(unixValue) && timestampCell.getFormula() === '') {
timestampCell.setFormula('=EPOCHTODATE(' + unixValue + ', 2)');
Utilities.sleep(300);
var convertedDate = timestampCell.getValue();
timestampCell.setValue(convertedDate);
}
// Set value in column D
activeSheet.getRange(currentRow, 4).setValue(1500);
// Set currency in column E
activeSheet.getRange(currentRow, 5).setValue('USD');
}
}
I tried using onChange and onEdit triggers but neither works with API updates. I also tried time-based triggers but they process all rows instead of just the new ones. How can I make this work only for newly added rows from the API?