gpt4 book ai didi

google-apps-script - 如何在具有设置名称的文件夹中列出谷歌驱动器中的所有文件?

转载 作者:行者123 更新时间:2023-12-03 20:18:37 24 4
gpt4 key购买 nike

我的驱动器中有一个文件夹,其中包含多个“客户端”文件夹。每个客户文件夹具有相同的结构,其中包括一个包含已完成发票(称为“已完成发票”)的文件夹。

folder structure

我需要一种方法来遍历所有名为“已完成发票”的文件夹,并在 google 表格中列出这些文件夹中的电子表格 ID,以便我以后可以遍历这些文件并从中提取数据。

我发现多个代码源有效,但仅在第一级 - 即它将列出代理/客户端文件夹中的文件,但不会进入子文件夹。例如。见下面的代码

function listFilesInFolder() {

var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow(["Name", "Date", "Size", "URL", "Download", "Description", "Type"]);

var folder = DriveApp.getFoldersByName("FOLDER ID GOES HERE");
//Logger.log(folder);
var contents = folder.getFiles();

var cnt = 0;
var file;

while (contents.hasNext()) {
var file = contents.next();
cnt++;

Logger.log(file);
Logger.log(cnt);

data = [
file.getName(),
file.getDateCreated(),
file.getSize(),
file.getUrl(),
,
];

sheet.appendRow(data);



};
};

最佳答案

感谢大家的回答,但经过更多搜索后,我找到了答案。对于正在寻找类似解决方案的任何人 - 我找到了一个出色的脚本 here它提供了一个非常强大的输出,我想它可以用于各种不同的场景:

/** 
* Google Apps Script - List all files & folders in a Google Drive folder, & write into a speadsheet.
* - Main function 1: List all folders
* - Main function 2: List all files & folders
*
* Hint: Set your folder ID first! You may copy the folder ID from the browser's address field.
* The folder ID is everything after the 'folders/' portion of the URL.
*
* @version 1.0
* @see https://github.com/mesgarpour
*/

// TODO: Set folder ID
var folderId = 'My folder ID';

// Main function 1: List all folders, & write into the current sheet.
function listFolers(){
getFolderTree(folderId, false);
};

// Main function 2: List all files & folders, & write into the current sheet.
function listAll(){
getFolderTree(folderId, true);
};

// =================
// Get Folder Tree
function getFolderTree(folderId, listAll) {
try {
// Get folder by id
var parentFolder = DriveApp.getFolderById(folderId);

// Initialise the sheet
var file, data, sheet = SpreadsheetApp.getActiveSheet();
sheet.clear();
sheet.appendRow(["Full Path", "Name", "Date", "URL", "Last Updated", "Description", "Size"]);

// Get files and folders
getChildFolders(parentFolder.getName(), parentFolder, data, sheet, listAll);

} catch (e) {
Logger.log(e.toString());
}
};

// Get the list of files and folders and their metadata in recursive mode
function getChildFolders(parentName, parent, data, sheet, listAll) {
var childFolders = parent.getFolders();

// List folders inside the folder
while (childFolders.hasNext()) {
var childFolder = childFolders.next();
// Logger.log("Folder Name: " + childFolder.getName());
data = [
parentName + "/" + childFolder.getName(),
childFolder.getName(),
childFolder.getDateCreated(),
childFolder.getUrl(),
childFolder.getLastUpdated(),
childFolder.getDescription(),
childFolder.getSize()
];
// Write
sheet.appendRow(data);

// List files inside the folder
var files = childFolder.getFiles();
while (listAll & files.hasNext()) {
var childFile = files.next();
// Logger.log("File Name: " + childFile.getName());
data = [
parentName + "/" + childFolder.getName() + "/" + childFile.getName(),
childFile.getName(),
childFile.getDateCreated(),
childFile.getUrl(),
childFile.getLastUpdated(),
childFile.getDescription(),
childFile.getSize()
];
// Write
sheet.appendRow(data);
}

// Recursive call of the subfolder
getChildFolders(parentName + "/" + childFolder.getName(), childFolder, data, sheet, listAll);
}
};

关于google-apps-script - 如何在具有设置名称的文件夹中列出谷歌驱动器中的所有文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36274928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com