列出电子表格中Google驱动器文件夹中的文件 [英] List files inside a google drive folder in spreadsheets
问题描述
我有一个文件夹:
我该怎么办?
解决方案:
以下代码将完成这项工作:
function findFilesInfo(){const folderId ='1_70Q4BPQrOHCQU4eUleEPuxbr0hpfhBB'const文件夹= DriveApp.getFolderById(folderId)常量文件= folder.getFiles()const source = SpreadsheetApp.getActiveSpreadsheet();const sheet = source.getSheetByName('Página1');const data = [];while(files.hasNext()){const childFile = files.next();var info = [childFile.getName(),childFile.getUrl(),childFile.getLastUpdated(),Drive.Files.get(childFile.getId()).lastModifyingUser.displayName];data.push(info);}sheet.getRange(2,1,data.length,data [0] .length).setValues(data);}
要求:
- 创建一个名称为Página1的工作表(您当然可以对其进行调整根据您的需求)具有以下结构:
- 有资源>进阶Google服务:启用 Drive API :
-
在此处指定文件夹的文件夹ID":
const folderId ='1_70Q4BPQrOHCQU4eUleEPuxbr0hpfhBB';
限制:
您不能在您的Google工作表文件中使用 findFilesInfo()
作为自定义函数.您可以通过点击播放按钮直接从Google脚本编辑器中运行它:
或者您可以创建一个自定义按钮(宏),您可以在其中从Google工作表文件中单击该按钮并执行该功能.
I have a folder: https://drive.google.com/drive/folders/1_70Q4BPQrOHCQU4eUleEPuxbr0hpfhBB
This folder has some files inside. I want to create a google-appscript to list the files in a Spreadsheet, in a way it look as close as possible to this: https://docs.google.com/spreadsheets/d/11KQYtJvEi1nbw0Awp7xgitOPgc9RhkMLICBGvvJrYlU/edit#gid=0
How could I do that?
Solution:
The following code will do the job:
function findFilesInfo() {
const folderId = '1_70Q4BPQrOHCQU4eUleEPuxbr0hpfhBB'
const folder = DriveApp.getFolderById(folderId)
const files = folder.getFiles()
const source = SpreadsheetApp.getActiveSpreadsheet();
const sheet = source.getSheetByName('Página1');
const data = [];
while (files.hasNext()) {
const childFile = files.next();
var info = [
childFile.getName(),
childFile.getUrl(),
childFile.getLastUpdated(),
Drive.Files.get(childFile.getId()).lastModifyingUser.displayName
];
data.push(info);
}
sheet.getRange(2,1,data.length,data[0].length).setValues(data);
}
Requirements:
- Create a sheet with the name Página1 (you can adjust it of course according to your needs) that has the following structure:
- In Resouces > Advanced Google services: enable Drive API:
Specify the 'folderID' of your folder here:
const folderId = '1_70Q4BPQrOHCQU4eUleEPuxbr0hpfhBB';
Restrictions:
You can't use findFilesInfo()
as a custom function within your google-sheet file. You can either run it directly from google script editor by clicking on the play button:
or you can create a custom button (macros) where you can click on it from your google-sheet file and execute the function.
这篇关于列出电子表格中Google驱动器文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!