使用 Apps 脚本在电子表格中列出共享云端硬盘的每个文件和文件夹
List every file and folder of a shared drive in a spreadsheet with Apps Script
我找到了一个完美适用于“我的云端硬盘”的脚本,我想对其进行调整以支持共享云端硬盘。
function onOpen() {
var SS = SpreadsheetApp.getActiveSpreadsheet();
var ui = SpreadsheetApp.getUi();
ui.createMenu('List Files/Folders')
.addItem('List All Files and Folders', 'listFilesAndFolders')
.addToUi();
};
function listFilesAndFolders(){
var folderId = Browser.inputBox('Enter folder ID', Browser.Buttons.OK_CANCEL);
if (folderId === "") {
Browser.msgBox('Folder ID is invalid');
return;
}
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","Type" ,"Date", "URL", "Last Updated", "Description", "Size","Owner Email"]);
// 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();
var folderId = childFolder.getId();
data = [
parentName + "/" + childFolder.getName(),
childFolder.getName(),
"Folder",
childFolder.getDateCreated(),
childFolder.getUrl(),
childFolder.getLastUpdated(),
childFolder.getDescription(),
childFolder.getSize()/1024,
childFolder.getOwner().getEmail()
];
// Write
sheet.appendRow(data);
// List files inside the folder
var files = childFolder.getFiles();
while (listAll & files.hasNext()) {
var childFile = files.next();
data = [
parentName + "/" + childFolder.getName() + "/" + childFile.getName(),
childFile.getName(),
"Files",
childFile.getDateCreated(),
childFile.getUrl(),
childFile.getLastUpdated(),
childFile.getDescription(),
childFile.getSize()/1024,
childFile.getOwner().getEmail(),
];
// Write
sheet.appendRow(data);
}
// Recursive call of the subfolder
getChildFolders(parentName + "/" + childFolder.getName(), childFolder, data, sheet, listAll);
}
};
老实说我还是不太明白
{supportsAllDrives: true}
有效,但我假设它正是缺少的东西。
我一直在尝试获得更多有关它的信息,但我发现的内容对我来说并不是很有用,也不是关于如何使用它的完整解释。
我是 Apps Script 的新手,但我正在尽最大努力编写完全可操作的代码或至少理解它,所以如果有人能 link 我找到任何关于的好页面,我将不胜感激它。
我有一个功能,可以将信息复制到 sheet 对我来说,它只需要一个文件夹。您可以根据需要修改:
起始函数:
function AddDataValidation(){
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
const folder = DriveApp.getFolderById("folder id");
getFnF(folder);
}
递归函数:
var level = 1;
function getFnF(folder = DriveApp.getRootFolder()) {
const ss = SpreadsheetApp.getActive();//active spreadsheet
const sh = ss.getSheetByName('Sheet0')//change sheet name
const files = folder.getFiles();
sh.getRange(sh.getLastRow() + 1, level).setValue(folder.getName()).setFontWeight('bold');
if (files.hasNext()) {
sh.getRange(sh.getLastRow() + 1, level).setValue('Files:');
}
while (files.hasNext()) {
let file = files.next();
let firg = sh.getRange(sh.getLastRow() + 1, level + 1);
firg.setValue(Utilities.formatString(file.getName()));
}
const subfolders = folder.getFolders()
while (subfolders.hasNext()) {
let subfolder = subfolders.next();
level++;
getFnF(subfolder);
}
level--;
}
{supportsAllDrives: true}
用于驱动器 API。但是,幸运的是,在现阶段,当使用文件 ID 和文件夹 ID 时,可以使用 Drive 服务(DriveApp)检索共享驱动器中的文件和文件夹。但不幸的是,无法检索文件和文件夹的所有者。我认为这可能是您当前问题的显示脚本的原因。如果我的理解是正确的,下面的修改怎么样?
发件人:
data = [
parentName + "/" + childFolder.getName(),
childFolder.getName(),
"Folder",
childFolder.getDateCreated(),
childFolder.getUrl(),
childFolder.getLastUpdated(),
childFolder.getDescription(),
childFolder.getSize()/1024,
childFolder.getOwner().getEmail()
];
收件人:
var ownerOfFolder = childFolder.getOwner();
data = [
parentName + "/" + childFolder.getName(),
childFolder.getName(),
"Folder",
childFolder.getDateCreated(),
childFolder.getUrl(),
childFolder.getLastUpdated(),
childFolder.getDescription(),
childFolder.getSize()/1024,
ownerOfFolder ? ownerOfFolder.getEmail() : ""
];
另外,请修改如下。
发件人:
data = [
parentName + "/" + childFolder.getName() + "/" + childFile.getName(),
childFile.getName(),
"Files",
childFile.getDateCreated(),
childFile.getUrl(),
childFile.getLastUpdated(),
childFile.getDescription(),
childFile.getSize()/1024,
childFile.getOwner().getEmail(),
];
收件人:
var ownerOfFile = childFile.getOwner();
data = [
parentName + "/" + childFolder.getName() + "/" + childFile.getName(),
childFile.getName(),
"Files",
childFile.getDateCreated(),
childFile.getUrl(),
childFile.getLastUpdated(),
childFile.getDescription(),
childFile.getSize()/1024,
ownerOfFile ? ownerOfFile.getEmail() : "",
];
注:
- 当我测试你的脚本反映了对共享驱动器的上述修改时,我可以确认脚本有效。
我找到了一个完美适用于“我的云端硬盘”的脚本,我想对其进行调整以支持共享云端硬盘。
function onOpen() {
var SS = SpreadsheetApp.getActiveSpreadsheet();
var ui = SpreadsheetApp.getUi();
ui.createMenu('List Files/Folders')
.addItem('List All Files and Folders', 'listFilesAndFolders')
.addToUi();
};
function listFilesAndFolders(){
var folderId = Browser.inputBox('Enter folder ID', Browser.Buttons.OK_CANCEL);
if (folderId === "") {
Browser.msgBox('Folder ID is invalid');
return;
}
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","Type" ,"Date", "URL", "Last Updated", "Description", "Size","Owner Email"]);
// 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();
var folderId = childFolder.getId();
data = [
parentName + "/" + childFolder.getName(),
childFolder.getName(),
"Folder",
childFolder.getDateCreated(),
childFolder.getUrl(),
childFolder.getLastUpdated(),
childFolder.getDescription(),
childFolder.getSize()/1024,
childFolder.getOwner().getEmail()
];
// Write
sheet.appendRow(data);
// List files inside the folder
var files = childFolder.getFiles();
while (listAll & files.hasNext()) {
var childFile = files.next();
data = [
parentName + "/" + childFolder.getName() + "/" + childFile.getName(),
childFile.getName(),
"Files",
childFile.getDateCreated(),
childFile.getUrl(),
childFile.getLastUpdated(),
childFile.getDescription(),
childFile.getSize()/1024,
childFile.getOwner().getEmail(),
];
// Write
sheet.appendRow(data);
}
// Recursive call of the subfolder
getChildFolders(parentName + "/" + childFolder.getName(), childFolder, data, sheet, listAll);
}
};
老实说我还是不太明白
{supportsAllDrives: true}
有效,但我假设它正是缺少的东西。
我一直在尝试获得更多有关它的信息,但我发现的内容对我来说并不是很有用,也不是关于如何使用它的完整解释。
我是 Apps Script 的新手,但我正在尽最大努力编写完全可操作的代码或至少理解它,所以如果有人能 link 我找到任何关于的好页面,我将不胜感激它。
我有一个功能,可以将信息复制到 sheet 对我来说,它只需要一个文件夹。您可以根据需要修改:
起始函数:
function AddDataValidation(){
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
const folder = DriveApp.getFolderById("folder id");
getFnF(folder);
}
递归函数:
var level = 1;
function getFnF(folder = DriveApp.getRootFolder()) {
const ss = SpreadsheetApp.getActive();//active spreadsheet
const sh = ss.getSheetByName('Sheet0')//change sheet name
const files = folder.getFiles();
sh.getRange(sh.getLastRow() + 1, level).setValue(folder.getName()).setFontWeight('bold');
if (files.hasNext()) {
sh.getRange(sh.getLastRow() + 1, level).setValue('Files:');
}
while (files.hasNext()) {
let file = files.next();
let firg = sh.getRange(sh.getLastRow() + 1, level + 1);
firg.setValue(Utilities.formatString(file.getName()));
}
const subfolders = folder.getFolders()
while (subfolders.hasNext()) {
let subfolder = subfolders.next();
level++;
getFnF(subfolder);
}
level--;
}
{supportsAllDrives: true}
用于驱动器 API。但是,幸运的是,在现阶段,当使用文件 ID 和文件夹 ID 时,可以使用 Drive 服务(DriveApp)检索共享驱动器中的文件和文件夹。但不幸的是,无法检索文件和文件夹的所有者。我认为这可能是您当前问题的显示脚本的原因。如果我的理解是正确的,下面的修改怎么样?
发件人:
data = [
parentName + "/" + childFolder.getName(),
childFolder.getName(),
"Folder",
childFolder.getDateCreated(),
childFolder.getUrl(),
childFolder.getLastUpdated(),
childFolder.getDescription(),
childFolder.getSize()/1024,
childFolder.getOwner().getEmail()
];
收件人:
var ownerOfFolder = childFolder.getOwner();
data = [
parentName + "/" + childFolder.getName(),
childFolder.getName(),
"Folder",
childFolder.getDateCreated(),
childFolder.getUrl(),
childFolder.getLastUpdated(),
childFolder.getDescription(),
childFolder.getSize()/1024,
ownerOfFolder ? ownerOfFolder.getEmail() : ""
];
另外,请修改如下。
发件人:
data = [
parentName + "/" + childFolder.getName() + "/" + childFile.getName(),
childFile.getName(),
"Files",
childFile.getDateCreated(),
childFile.getUrl(),
childFile.getLastUpdated(),
childFile.getDescription(),
childFile.getSize()/1024,
childFile.getOwner().getEmail(),
];
收件人:
var ownerOfFile = childFile.getOwner();
data = [
parentName + "/" + childFolder.getName() + "/" + childFile.getName(),
childFile.getName(),
"Files",
childFile.getDateCreated(),
childFile.getUrl(),
childFile.getLastUpdated(),
childFile.getDescription(),
childFile.getSize()/1024,
ownerOfFile ? ownerOfFile.getEmail() : "",
];
注:
- 当我测试你的脚本反映了对共享驱动器的上述修改时,我可以确认脚本有效。