I am building a Google Sheet to act as a workflow of sorts, where each sheet is a stage of the work and upon changing a checkbox Boolean to True the row is moved to the next stage. I also have it set to reverse this process by changing the same Boolean to False.
This appears to be working as intended, and I can move any row to the next stage or send it back. The only downside is that it takes between 1.8s - 3.2s for this operation to take place. This wouldn't be an issue except that if I change one checkbox to True and then, before it has completed the operation, change a second row's checkbox to True, it doesn't run for that second instance because it is still trying to complete the first one. You can imagine that waiting 3 seconds between each operation is not ideal.
I assume that while my script works, it is poorly optimized with a lot of hardcoded variables. I have only done a little work with apps script and am learning, so I am looking as much for understanding and guidance as I am to fixing this issue. I appreciate any help offered, it will help me learn how to tackle the next problem!
Here is my code, if providing anything else is helpful just let me know:
Also, here is a link to a Copy spreadsheet in case you wanted to test what I have so far: https://docs.google.com/spreadsheets/d/1CRJxKhS26nD8Gqd0ozBYSYee7HgypnXcFbuCRanAwVA/edit?usp=sharing
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
// select target sheet based on active sheet
// ps = prior sheet, as = active sheet, ts = target sheet
// selects trigger column and back column
// tc = trigger column, bc = back column
if(s.getName() == "Prospects") {
var as = "Prospects";
var ts = "Pricing";
var tc = 11;
var bc = 200;
} else if(s.getName() == "Pricing") {
var ps = "Prospects";
var as = "Pricing";
var ts = "Onboarding";
var tc = 34;
var bc = 11;
} else if(s.getName() == "Onboarding") {
var ps = "Pricing";
var as = "Onboarding";
var ts = "DASH";
var tc = 45;
var bc = 34;
}
// get first empty row of target sheet
function getFirstEmptyRowA() {
var spr = ss.getSheetByName(ts);
var column = spr.getRange('A:A');
var values = column.getValues(); // get all data in one call
var ct = 0;
while ( values[ct] && values[ct][0] != "" ) {
ct++;
}
return (ct);
}
// get first empty row of prior sheet
function getFirstEmptyRowB() {
var spr = ss.getSheetByName(ps);
var column = spr.getRange('A:A');
var values = column.getValues(); // get all data in one call
var cy = 0;
while ( values[cy] && values[cy][0] != "" ) {
cy++;
}
return (cy);
}
if(r.getColumn() == tc && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName(ts);
var targetLine = getFirstEmptyRowA();
var target = targetSheet.getRange(getFirstEmptyRowA() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
} else if(r.getColumn() == bc && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName(ps);
var targetLine = getFirstEmptyRowB();
var target = targetSheet.getRange(getFirstEmptyRowB() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}