非淡泊无以明志,非宁静无以致远。这篇文章主要讲述使用Google Apps脚本查找所有文件夹(和子文件夹)中的文档/幻灯片/表格相关的知识,希望能为你提供帮助。
从指向文件夹的链接开始,我试图获取该文件夹中所有文档/幻灯片/表格的信息,子文件夹(以及子文件夹的子文件夹)。
通过一些在线搜索,我终于得到了一些可以找到所有文件的代码,但是我需要修改它才能找到文档,幻灯片和表格。最好的方法是什么?
这是我目前使用的代码:
function generateFolderTree(my_id) {
var parentFolder = DriveApp.getFolderById(my_id);
var f = parentFolder.getFiles();
Logger.log(f.next().getName())getChildFolders(parentFolder);
}function getChildFolders(parent) {
var childFolders = parent.getFolders();
while (childFolders.hasNext()) {
var childFolder = childFolders.next();
var files = childFolder.getFiles();
while (files.hasNext()) {
Logger.log(files.next().getName());
// Print list of files inside the folder
}
getChildFolders(childFolder);
// Recursive call for any sub-folders
}
}
答案有点玩这个我很肯定你可以在你的代码中实现它; )
https://developers.google.com/apps-script/reference/base/mime-type
// Use MimeType enum to log the name of every Google Doc in the user's Drive.
var docs = DriveApp.getFilesByType(MimeType.GOOGLE_DOCS);
while (docs.hasNext()) {
var doc = docs.next();
Logger.log(doc.getName())
}// Use plain string to log the size of every PNG in the user's Drive.
var pngs = DriveApp.getFilesByType('image/png');
while (pngs.hasNext()) {
var png = pngs.next();
Logger.log(png.getSize());
}
另一答案【使用Google Apps脚本查找所有文件夹(和子文件夹)中的文档/幻灯片/表格】这是我使用的代码,它似乎正在做我想要/需要的一切:
function getFolders() {
var my_link = Browser.inputBox('Enter Folder Link', 'Paste a folder link below...', Browser.Buttons.OK);
var my_id = getIdFromUrl(my_link);
fcount = 2;
SpreadsheetApp.getActiveSpreadsheet().toast("Im looking for SLIDES.", "WORKING...",600);
generateFolderTree(my_id,MimeType.GOOGLE_SLIDES);
SpreadsheetApp.getActiveSpreadsheet().toast("Im looking for DOCS.", "WORKING...",600);
generateFolderTree(my_id,MimeType.GOOGLE_DOCS);
SpreadsheetApp.getActiveSpreadsheet().toast("Im looking for SHEETS.", "WORKING...",600);
generateFolderTree(my_id,MimeType.GOOGLE_SHEETS);
SpreadsheetApp.getActiveSpreadsheet().toast("Your search is complete!", "FINISHED!");
};
// -------------------------------------------------------------------------------function generateFolderTree(my_id,type) {
var parentFolder = DriveApp.getFolderById(my_id);
var f = parentFolder.getFilesByType(type);
while (f.hasNext()) {
file = f.next();
display(file);
}
getChildFolders(parentFolder,type);
}function getChildFolders(parent,type) {
var childFolders = parent.getFolders();
while (childFolders.hasNext()) {
var childFolder = childFolders.next();
var files = childFolder.getFilesByType(type);
while (files.hasNext()) {
mfs = files.next();
display(mfs);
}
getChildFolders(childFolder,type);
// Recursive call for any sub-folders
}
}
另一答案
function myFunction()
{
var folders = DriveApp.getFoldersByName("Projects");
var folder = folders.next();
var sheets = getFilesInFolderByType(folder, MimeType.GOOGLE_SHEETS);
var docs = getFilesInFolderByType(folder, MimeType.GOOGLE_DOCS);
for each (var sheet in sheets)
{
console.log(sheet);
}
for each (var doc in docs)
{
console.log(doc);
}
}function getFilesInFolderByType(folder, type)
{
var files = []
var filesInFolder = folder.getFilesByType(type);
while (filesInFolder.hasNext())
{
files.push(filesInFolder.next());
}
// Use method to recurse through sub folders.
var subFolders = folder.getFolders();
while (subFolders.hasNext())
{
var fileArray = getFilesInFolderByType(subFolders.next(), type);
files.concat(fileArray);
}
return files;
}
推荐阅读
- 请求AppDataFolder时,Google云端硬盘API会返回错误“无效值”和“无效查询”
- Google Apps脚本中的Google Drive通知
- 如何使用Flutter访问Google Drive appdata文件夹文件()
- Google Apps脚本未遍历所有文件
- 谷歌驱动API的Android如何登录并从硬编码我的硬盘帐户注销无需用户交互()
- 谷歌硬盘中App Data文件夹的未来是什么()
- 如何在Android上使用本地化时间单位格式化字符串的持续时间()
- 为什么会出现此错误“com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException”
- DriveApp |获取文件的查看者和评论者