Created
May 29, 2023 20:32
-
-
Save mark05e/2c182e23f4b08e6e4a56533e4942d268 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Retrieves the filenames and corresponding file IDs of all files within a given Google Drive folder. | |
* | |
* @param {string} folderId - The ID of the folder. | |
* @returns {Object[]} - An array of objects containing the file ID and name. | |
*/ | |
function getFilenamesAndIds(folderId) { | |
// Get the folder using the provided folderId | |
let folder = DriveApp.getFolderById(folderId); | |
// Get all the files within the folder | |
let files = folder.getFiles(); | |
// Initialize an empty array to store the filenames and file IDs | |
let filenamesAndIds = []; | |
// Iterate over each file in the folder | |
while (files.hasNext()) { | |
// Get the next file | |
let file = files.next(); | |
// Retrieve the file ID and name | |
let fileId = file.getId(); | |
let filename = file.getName(); | |
// Create an object with the file ID and name | |
let fileInfo = { | |
id: fileId, | |
name: filename | |
}; | |
// Add the fileInfo object to the filenamesAndIds array | |
filenamesAndIds.push(fileInfo); | |
} | |
// Return the array containing the filenames and file IDs | |
return filenamesAndIds; | |
} | |
function getFilenamesAndIds_test() { | |
let folderId = "your-folder-id"; // Replace with the actual folder ID | |
let filenamesAndIds = getFilenamesAndIds(folderId); | |
console.log(filenamesAndIds); | |
// [ | |
// { id: "file1-id", name: "File 1" }, | |
// { id: "file2-id", name: "File 2" }, | |
// { id: "file3-id", name: "File 3" }, | |
// // Additional files... | |
// ] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment