I'm trying to find a way to find the last row with data in Column D. I also want the search to start at Row 4.
I'm really struggling and would appericate any help please.
I'm trying to find a way to find the last row with data in Column D. I also want the search to start at Row 4.
I'm really struggling and would appericate any help please.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet2');
var rowOffset = 3;
var count = sheet.getRange('D4:D').getDisplayValues().flat().filter(String).length;
var lastRow = count+rowOffset;
Logger.log(lastRow);
D4:D
and get its value, since you want to get the last row with data in column D starting at row 4.Execution log
2:01:53 AM Notice Execution started
2:01:54 AM Info 13.0
2:01:55 AM Notice Execution completed
There are many ways to find the last value in a column, but here is one. See if this helps!
function myFunction() {
const spreadsheet = SpreadsheetApp.openByUrl('Insert Sheet URL Here');
const sheet = spreadsheet.getSheetByName('Insert Sheet Name Here - e.g. Sheet1');
const lastSheetRow = sheet.getLastRow();
let lastColumnRow = 'This column is empty';
let reversedColumnValues = sheet.getRange(1, 4, lastSheetRow).getValues().reverse();
for (index in reversedColumnValues) {
if (reversedColumnValues[index][0] != '') {
lastColumnRow = lastSheetRow - index;
break;
}
}
Logger.log(lastColumnRow);
}
This Apps Script Video may help you out too: https://www.youtube.com/watch?v=1Po1QElOFPk