/** * Writes a CSV file to a Drive folder (by id) containing filenames and Google Drive download urls (File.webContentLink) * for all files in a specified folder (by id) of a Shared Drive (by id). */ function ExportFilesFromSharedDriveToCSV() { // id of Shared Drive (go to Shared Drive, copy-paste id from browser URL) var sharedDriveId = "0AFR1j1Oo3fnlUk9PVA"; // id of folder in Shared Drive to search (go to folder, copy-paste if from browser URL) var folderIdToSearch = "1Ua6mIE8kL4Q1r-LEEVmAlo-oyrsY2gg9"; // id of folder to save the CSV file var folderIdToSave = "1HOE7yqsk4bQDC6d7CwNoR7wAbeIN5Csi" // list of File objects from a folder. See: // https://developers.google.com/drive/api/v3/reference/files#resource // https://developers.google.com/drive/api/v3/search-files var fileItems = GetFilesFromSharedDrive(sharedDriveId, folderIdToSearch); // join filename and download url into csv, save to a Drive folder var csvText = MakeCSVFromFileItems(fileItems); // write multi-line comma-separated string variable to specified folder (by id) with specified name var file = WriteVariableToFile(csvText, folderIdToSave, 'output.csv'); } /** * Creates a multi-line string of CSV text from list of Files. * * @param {Files resource} fileItems collection of File objects. See: https://developers.google.com/drive/api/v3/reference/files#resource * * @return {String} csvText The multi-line string to write to a CSV file. */ function MakeCSVFromFileItems(fileItems) { // make header row of CSV var list = ['title, downloadUrl']; // format list of files into list of comma-separated strings, for filename and url for (var i = 0; i < fileItems.length; i++) { // ignore files without a webContentLink (download url) if (!fileItems[i].webContentLink) {continue;} // many other File attributes are available. using `title` (filename) and `webContentLink` (download url) list.push(Utilities.formatString('%s,%s', fileItems[i].title, fileItems[i].webContentLink)); } // return a multi-line string from list of values, using newline characters return list.join('\n') } /** * Searches for files in a Shared Drive (by id) whose parent folder matches `folderIdToSearch`. * * @param {String} sharedDriveId Id of Shared Drive (navigate to root in Drive, copy id from URL in browser). * @param {String} folderIdToSearch Id of folder in Shared Drive to search (navigate to folder in Drive, copy id from URL in browser). * * @return {Array} fileItems List of File resources each with attributes like filename and download url. */ function GetFilesFromSharedDrive(sharedDriveId, folderIdToSearch) { // search a folder within a Shared Drive and get all files within it var folderId = folderIdToSearch || "1Ua6mIE8kL4Q1r-LEEVmAlo-oyrsY2gg9"; var sharedDriveId = sharedDriveId || "0AFR1j1Oo3fnlUk9PVA"; var result = Drive.Files.list({ "corpora": "teamDrive", "includeTeamDriveItems": true, "orderBy": "folder", "q": Utilities.formatString('\'%s\' in parents',folderId), // must wrap value with ' ' "supportsTeamDrives": true, "driveId": sharedDriveId }); // return the data as object (JSON parse string into object) return JSON.parse(result).items; } /** * Saves an Apps Script variable to a file (by name) in a specified folder (by id). * * @param {Object} variable Any variable allowed, but String used for this case. * @param {String} folderId Id of folder in Google Drive to save the new file. * @param {String} filename String name of the new file to save. * * @return {File} file The resulting newly-created file. */ function WriteVariableToFile(variable, folderId, filename) { var folderId = folderId || "1HOE7yqsk4bQDC6d7CwNoR7wAbeIN5Csi"; var folder = DriveApp.getFolderById(folderId); var files = folder.getFilesByName(filename); // guarantees there is only one file with that name in the folder. while (files.hasNext()) { files.next().setTrashed(true); } return folder.createFile(filename, variable); }