I'm trying to figure out how to add a value to the last row in a specific column in Google Sheets. Not all columns in the spreadsheet/table are of equal length so I can't simply look for the last row and add to a column. I need to find the next empty cell in a column and add the new value there. Looking through the docs I think involves the getRange()
and setValue()
functions but I think setting the actual range requires some additional coding to determine the next empty cell. Once I get that then I can do somtrhing like the following.
The button click triggers the addRecord()
function
function addRecord(){
var recVals = {};
recVals.source = document.getElementById("source").value;
recVals.medium = document.getElementById("medium").value;
recVals.product = document.getElementById("product").value;
google.script.run.userClicked(recVals);
}
Then in my apps scripts the userClicked()
function runs and the value(s) are passed to it and written to the spreadsheet:
function userClicked(recVals){
var ss = SpreadsheetApp.openById(ssId)
var ws = ss.getSheetByName("Data");
ws.appendRow([recVals.source, recVals.medium, recVals.product, new Date()]);
}
The problem is, that appendRow()
adds the row at the end, below where I need the values to be set.