I am using Google Sheets for a (originally planned to be) quick and easy database. Users submit a form, it is then sent to a Sheets for storing.
I have three columns that I'm trying to auto-update through Google Apps Scripts:
- A 'user ID' (this should be automatically created when a new row is added, and shouldn't be able to be changed)
- A 'createdAt' column, which is just a timestamp for when the row was added
- A 'lastChanged' column, which represents the last time a row was changed.
I've used advice from How can I run a script only when someone adds new rows and Determining the last row in a single column, but I'm still having problems.
I'm running it on an onEdit event right now. But it seems that the 'lastChanged' column is the only one that should be triggered by this event, the others should be used with something like an changeType=='INSERT_ROW'
. When the user details are submitted (through Zapier), a row is being added, and so the onEdit doesn't work for setting the userId and createdAt.
I also know that my method for generating the userID is far from ideal.
Here is where I have got so far.
function onEdit(e) {
var userId = 1 // column A
var createdTime = 2 //column B
var lastChangedTime = 24 //column X
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = e.source.getSheets()[1]; //gets the second sheet
var editedCell = sheet.getActiveCell(); //gets the edited cell
var Avals = ss.getRange("A2:A").getValues();
var Alast = Avals.filter(String).length;
//set date in same row as edit happens, at fixed column
ss.getActiveSheet().getRange(ss.getActiveRange().getLastRow(), lastChangedTime, 1, 1).setValue(new Date());
//set the created time
ss.getActiveSheet().getRange(ss.getActiveRange().getLastRow(), createdTime, 1, 1).setValue(new Date());
//set the userID
ss.getActiveSheet().getRange(ss.getActiveRange().getLastRow(), userId, 1, 1).setValue(Alast+1); }