We're managing all changes from multiple sheets (one for each of 29 different locations) to be reflected in one main_db. The main_db is then hooked up to Data Studio for Analytics.
We have implemented a "updates_to_db" record sheet for the changes being made, and then running a triggered service every 10 mins that does the actual getting and setting of data in the main_db.
I faced issues with onEdit() functions writing data over each other when getLastRow() was returning the same value for onEdit()'s that were fired off in quick succession. I've implemented a PropertyKey to now hold this value. onEdit() first updates this value, and then continues. This way all subsequent onEdits() are correctly being written to the updated lastRow.
onOpen() function This sets the PropertyKey value for real last row in the updates records sheet accurately.
function onOpen() {
// Welcome message
var message = 'The current time is ' + new Date().toString();
var title = 'Welcome to xxxxxx DASHBOARD';
SpreadsheetApp.getActiveSpreadsheet().toast(message, title);
var last_row = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(push_to_db_sheet).getLastRow();
PropertiesService.getScriptProperties().setProperty('last_row_updates_sheet', last_row + 1);
}
onEdit() function
function onEdit(e){
var activeSheet = e.source.getActiveSheet();
var date_stamp = new Date();
var tabs = ["Customer Requests","Indent Status","Tracking Acknowledgements","Actual Costs","Balance Payments","Payment Updates"];
if(tabs.indexOf(activeSheet.getName()) !== -1){
var row_to_edit = PropertiesService.getScriptProperties().getProperty('last_row_updates_sheet');
row_to_edit= +row_to_edit; // converting string to int
PropertiesService.getScriptProperties().setProperty('last_row_updates_sheet', row_to_edit + 1);
var cell = e.range;
var col = cell.getColumn();
var changes_sheet = e.source.getSheetByName(push_to_db_sheet);
// var last_row = changes_sheet.getLastRow() + 1;
changes_sheet.getRange(row_to_edit, 1).setValue(date_stamp);
changes_sheet.getRange(row_to_edit, 2).setValue(activeSheet.getName());
changes_sheet.getRange(row_to_edit, 3).setValue(activeSheet.getRange(cell.getRow(),3,1,1).getValue());
changes_sheet.getRange(row_to_edit, 4).setValue(cell.getColumn())
changes_sheet.getRange(row_to_edit, 5).setValue(e.value);
cell.setNote('Last modified: ' + date_stamp); // helps the user know when the last edit was made
cell.setBackground("#FBF7EC"); // helps the user visually see when the update has been captured. I can move this up to make it "seem" faster...
} // IF tab name matches
}
Any ideas on how I can speed up onEdit()? It is currently taking between 4 - 13 seconds to run. Eventually, I will be calling email and SMS trigger functions based on specific cells which were edited. I'd like a pop-up with the email/sms to be shown, and then a confirmation button will trigger the email/sms.
I'm worried that with a long onEdit() time the user will be several columns ahead when the email/sms pop-up dialogue is displayed.