Very new to the Google Sheets script editing, but I'm trying to move rows between sheets. I've done this successfully using getLastRow however now I'm attempting to do this based on a specific column. The reason for this is that the sheet the info is heading to has preset data validation fields and checkboxes, resulting in the row going to row 1000 every time.
I'm looking for when an option is selected on the dropdown menu on the current sheet, it will find the first free row based on a certain column, and replace that row (which is essentially empty except for the preset fields).
Currently I have as follows:
function onEdit(event) {
// Move rows to different sheets based on action selected
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = SpreadsheetApp.getActiveRange();
if(range.getColumn() == 5 && range.getValue() == "Active") {
var row = range.getRow();
var numColumns = sheet.getLastColumn();
var targetSheet = spreadsheet.getSheetByName("Active");
var target = targetSheet.getRange(targetSheet.getLastRowSpecial(8) + 1, 1);
sheet.getRange(row, 1, 1, numColumns).moveTo(target);
sheet.deleteRow(row);
and I found the below and other examples when googling but nothing seems to work:
function getLastRowSpecial(column){
// Get the last row with data for the whole sheet.
var numRows = targetSheet.getLastRow();
// Get all data for the given column
var data = targetSheet.getRange(1, column, numRows).getValues();
// Iterate backwards and find first non empty cell
for(var i = data.length - 1 ; i >= 0 ; i--){
if (data[i][0] != null && data[i][0] != ""){
return i + 1;
}
}
}
Any help is greatly appreciated!
targetSheet.getLastRowSpecial(8)
– CoopergetLastRowSpecial
isn't a method of Class Sheet – Rubén