function createNewFile(){
// Check if the file already exists
var folder = DriveApp.getFolderById(FOLDERID);
var existingFiles = checkExistingFiles();
var existingURLs = checkExistingURLs();
var name = getFlightCode();
// If the file doesn't exist, create it and paste the info
if (existingFiles.includes(name) == false){
// Create the file and get it's ID
var newSpreadsheet = SpreadsheetApp.create(name);
var fileId = newSpreadsheet.getId();
var file = DriveApp.getFileById(fileId);
// Add new tabs and name them. Paste Titles from Master Sheet
var ss = SpreadsheetApp.openById(fileId);
var tabs = ['Guest Details', 'Dietaries', 'Emergency Details', 'Insurance Details', 'Room Allocations','Extras'];
var masterRanges = ["A6:O7","Q6:S7",'U6:Z7','AB6:AG7','AI6:AK7','AM6:BB7'];
var titleRanges = ["A1:O1","A1:C1",'A1:F1','A1:F1','A1:C1','A1:O1'];
var subTitleRanges = ["A2:O2","A2:C2",'A2:F2','A2:F2','A2:C2','A2:O2'];
for (var i = 0; i < tabs.length; i ++) {
var tabName = tabs[i];
var newSheet = ss.insertSheet();
newSheet.setName(tabName);
copyTitles(fileId,tabs[i],masterRanges[i]);
formatTitles(fileId,tabs[i],titleRanges[i],subTitleRanges[i]);
newSheet.autoResizeColumns(1,15);
}
var sheetToDelete = ss.getSheetByName("Sheet1");
ss.deleteSheet(sheetToDelete);
// Add Data
addGuestInformation(name,fileId,'Guest Details');
addDietaries(name,fileId,'Dietaries');
addEmercencyDetails(name,fileId,'Emergency Details');
addInsuranceDetails(name,fileId,'Insurance Details');
addRoomAllocations(name,fileId,'Room Allocations');
addExtras(name,fileId,'Extras');
// Move file to desired folder and remove it from Root
// folder.addFile(file);
// DriveApp.getRootFolder().removeFile(file);
var sheetURL = file.getUrl();
// Show Success message and button to access the new report
var message = 'The report was successfully created. File Name: ' + name + '\n \nPress "OK" to go to the file';
var ui = SpreadsheetApp.getUi();
var response = ui.alert('Report Created', message, ui.ButtonSet.OK_CANCEL);
if (response == ui.Button.OK) {
var html = '<script> window.open("' + sheetURL + '"); google.script.host.close(); </script>';
ui.showModalDialog(HtmlService.createHtmlOutput(html), 'Existing File');
}
return fileId;
} else {
var nameIndex = existingFiles.indexOf(name);
var nameURL = existingURLs[nameIndex];
var message = 'It appears that the Trip Code you are trying to use already has a file created. If you want to create it again, delete the previous file. \n \nPress "OK" to go to the file';
var ui = SpreadsheetApp.getUi();
var response = ui.alert('File Already Exists', message, ui.ButtonSet.OK_CANCEL);
if (response == ui.Button.OK) {
var html = '<script> window.open("' + nameURL + '"); google.script.host.close(); </script>';
ui.showModalDialog(HtmlService.createHtmlOutput(html), 'Existing File');
}
}