Hi!
I’m working on a project to track landing of several VIPs for an event. I have their departure times and Tail Numbers but I’m not sure how to pass a 2D array into the API.
My code is currently written to call the function FlightInfoEx with only the tail numbers.
function consumeFlightsApi() {
var identifiers = getFlightIdentifiers();
var flights = ];
var airlines = ];
for (var i = 0; i < identifiers.length; i++) {
var id = identifiers*;
var flight_data = requestFlightData(id);
if (!flight_data) {
continue;
}
var airline_data = requestAirlineFlightData(flight_data.faFlightID);
if (!airline_data) {
continue;
}
flights.push(flight_data);
airlines.push(airline_data);
}
if (flights.length == 0 && airlines.length == 0) {
Logger.log("No data received");
return;
}
fillFlightDataSheet(flights);
fillAirlineDataSheet(airlines);
}
function getFlightIdentifiers() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("INFO");
var range = sheet.getRange("V4:V100");
var values = range.getValues();
var identifiers = Array.prototype.concat.apply(], values).filter(function(item) { return item && item.length > 0; });
return identifiers;
}
function requestFlightData(id) {
try {
var data = makeRequest("GET", "FlightInfoEx", { "ident": id, "howMany": 4 });
var flights = data.FlightInfoExResult.flights;
if (flights && flights.length > 0) {
return flights[0];
}
return null;
} catch(e) {
Logger.log("Can't request flight data");
Logger.log(e);
return null;
}
}
function requestAirlineFlightData(id) {
try {
var data = makeRequest("GET", "AirlineFlightInfo", { "faFlightID": id });
var airline = data.AirlineFlightInfoResult;
return airline;
} catch(e) {
Logger.log("Can't request airline flight data");
Logger.log(e);
return null;
}
}
function makeRequest(method, url, payload) {
var options = {
method: method,
headers: {
"Authorization" : "Basic " + Utilities.base64Encode(USER + ':' + API_KEY)
},
payload: payload
};
var response = UrlFetchApp.fetch(BASE_URL + url, options);
var json = response.getContentText();
var data = JSON.parse(json);
return data;
}
function fillFlightDataSheet(data) {
var date_properties = {
"filed_time": true,
"filed_departuretime": true,
"actualdeparturetime": true,
"estimatedarrivaltime": true,
"actualarrivaltime": true
};
fillSheet("FLIGHT_DATA", data, date_properties);
}
function fillAirlineDataSheet(data) {
fillSheet("AIRLINE_DATA", data, {});
}
function fillSheet(sheet_name, data, date_properties) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet_name);
var properties = Object.keys(data[0]); // Get the object property names
var headers = ]; // Array with the columns names
for (var i = 0; i < properties.length; i++) {
var property = properties*;
// Separate datetime values in a date and time column
if (date_properties[property] === true) {
headers.push(property + " - date");
headers.push(property + " - time");
continue;
}
headers.push(property);
}
// Set property names as headers
sheet.getRange(1, 1, 1, headers.length).setValues([headers]);
for (var i = 0; i < data.length; i++) {
var item = data*;
var values = ];
for (var j = 0; j < properties.length; j++) {
var property = properties[j];
var value = item[property];
if (date_properties[property] === true) {
value = new Date(value * 1000);
values.push(Utilities.formatDate(value, "EST", "yyyy-MM-dd"));
values.push(Utilities.formatDate(value, "EST", "HH:mm:ss"));
continue;
}
values.push(value);
}
sheet.getRange(2 + i, 1, 1, values.length).setValues([values]);
}
// Auto size columns
for (var i = 0; i < headers.length; i++) {
sheet.autoResizeColumn(i + 1);
}
}