I’m trying to create an handrail app for tracking ambulance usage (one app = one vehicule, no conflict).
When creating a new record, I would like to pre-populate some fields with previous record ones, such
kilometers
general state comment
pictures of possible damages (picture picker)
eg. when creating a new record, re-use C5 value as default value (4000)
and of course be able to override the value.
I wasn’t able to find a way to achieve it so far, I’ve tried some google sheet script with onEdit(), but I suspect a new row isn’t yet triggered on glide “add item” action.
function onEdit(e) {
var sh = SpreadsheetApp.getActiveSheet();
var row = sh.getActiveRange().getRow();
if(sh.getRange(row,3).isBlank()){
sh.getRange(row,3).setValue(sh.getRange(row-1,3).getValue());
}
}
my flow accepts having a 2 steps data entry (add & then edit): first add with generic data (none pre-populated) and the edit one with prepopulated data, and in between trigger a google sheet script.
add generic info (+ small text saying that some extra info need to be added after)
glide triggers google sheet onEvent method (beware, onChange seems not triggered) making the work
google sheet script has to be optimized and reduce amount of call (if you have several field to prepopulate), otherwise glide will re-sync on first of them and then miss the extra ones.
below my script, ids array contains the columns to be replicated
function onEvent(e) {
var ids = [3, 7, 8, 9, 10, 11 ,12].sort(function(a, b) {return a - b;}); // indexes column to replicate from previous row if current value is null, and sort.
var sh = SpreadsheetApp.getActiveSheet();
var row = sh.getActiveRange().getRow();
if(row == 1){
return;
}
// as we prefer less calls to spreedsheet, we get min/max for narrowing down data retrieved & updated
var min = ids[0];
var max = ids[ids.length-1];
// get values from previous line
var previousLineValues = sh.getRange(row-1, min, row-1, max).getValues();
// get range (for future setValues) and values for current lines
var currentLineRange = sh.getRange(row,min, row, max);
var currentLineValues = currentLineRange.getValues();
for(var i=0;i<ids.length;i++)
{
var offset = ids[i] - min; // id of the column shifted as we only got range starting with min
if(currentLineValues[0][offset] == ""){
currentLineValues[0][offset] = previousLineValues[0][offset]; // if empty (isBlank() only on range), we override
}
}
currentLineRange.setValues(currentLineValues); // we update the whole set of values at once, preventing partial retrieval from glide
}
open if better approach preventing me having my entry in 2 screens (add + edit) which could lead to required fields missing.