I created a new Spreadsheet and shared it with you. For others watching this thread, the code follows. Of interest was learning different ways to determine if a value is a valid Date.
The method I am using only works because of the second condition I'm using (vehicleFound). Any number passed to the isValidDate function will give a false positive.
//Add Menu Item to run the code
function onOpen() {
SpreadsheetApp.getUi()
.createMenu("Trip data")
.addItem("Process trip data","getVehicles")
.addToUi()
}
//Short function to check if a value is an actual Date
//Used to check first column. If true, it is valid data.
function isValidDate(value) {
var dateWrapper = new Date(value);
return !isNaN(dateWrapper.getDate()); //returns true or false
}
function getVehicles() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ws = ss.getSheetByName("Weekly Sheet");
var ts = ss.getSheetByName("Master Sheet");
var data = ws.getDataRange()
.getDisplayValues();
var tempVehicles = [];
var id = "";
var vehicleFound = false; //used to determine if the row data is actual Trip data
data.forEach((vehicle, i) => { //look at each row
if (vehicle.indexOf('VEHICLE') >= 0) { //use the word 'vehicle' as a starting point for valid data
id = data[i][vehicle.indexOf('VEHICLE') + 1]; //if the word 'vehicle' is found, get the ID
vehicleFound = true;
}
if (isValidDate(vehicle[0]) && vehicleFound === true) { //if col A is a valid date, and we found a vehicle/ID
vehicle.push(id); //add vehicle ID to the trip
tempVehicles.push(vehicle); //add the trip to an array
}
if (vehicle.indexOf('VEHICLE TOTALS:') >= 0) { //vehicle data has stopped
vehicleFound = false; //stop adding non-trip data rows to the array
}
})
//loop through each trip data row and append to Master Sheet
tempVehicles.forEach(el => ts.appendRow(el));
}