Been struggling with this for days and cannot get it working the way I like.. Basically I want to have a script that takes a value in a specific cell and copies it into the next available row of a specific column in another sheet (tracking daily changes in a stock portfolio). I want to copy today's date into column D and the value from the other sheet into column E.
I've tried getLastRow() but it just adds the value at the very bottom of the column. I've tried using loop solutions from other questions but it always ends up copying to the wrong place or I just don't comprehend it with my limited coding knowledge.
Here is what I have at the moment which is a mess and I'm just stuck:
function dailyCaptureGains() {
// Get the daily value
var stockdata = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Dividend Portfolio');
var value = stockdata.getRange(27,2).getValue();
Logger.log("Value = " + value)
debugger
// Add the date and stock data to the other sheet
var gains = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Gains');
var formattedDate = Utilities.formatDate(new Date(), "GMT", "MM/dd/yy");
Logger.log(formattedDate);
debugger
// Find next empty cells to add the data and value
var dateRange = gains.getRange('D2:D');
var dateLastRow = dateRange.getLastRow(); // Get last row
Logger.log("last row = " + dateLastRow);
var date = dateLastRow + 1;
debugger
};
Open to any and all solutions. If there is a simpler way to do this please let me know.