I am making a tab in a Google Sheet to keep track of files in a particular folder. I have successfully modified a script I found online to get the list by folder id, but I can't seem to figure out how to get the results to appear in order by name. Here is the code I'm using but I'm putting the folder id in place of myFolderId:
/**
* List all files in Google Drive folder.
*
* @param {string} folderName (optional) Name of folder on Google Drive
*
* Adapted from:
* http://ctrlq.org/code/19854-list-files-in-google-drive-folder
* https://gist.github.com/hubgit/3755293
*/
function listFilesInFolder(id) {
// If we have not been provided a folderName, assume we will interact with user.
var interactive = (typeof folderName === 'undefined');
// Get name of folder to list
if (interactive) {
id = 'myFolderId';
}
if (id === '')
return; // No name provided, exit quietly
var folder = DriveApp.getFolderById(id);
var contents = folder.getFiles();
var file, data, sheet = SpreadsheetApp.getActiveSheet();
sheet.clear();
sheet.appendRow(["Name", "Date"]);
// Loop over files in folder, using file iterator
while (contents.hasNext()) {
file = contents.next();
if (file.getMimeType() == MimeType.GOOGLE_SHEETS) { // "SPREADSHEET"
// Skip displaying spreadsheets - I don't know why...
continue;
}
data = [
file.getName(),
file.getDateCreated(),
];
sheet.appendRow(data);
}
}