我正在尝试复制到步骤中 https://ctrlq.org/code/19854-list-files-in-google-drive-folder
它成功地为工作表中的列创建了标题,但它没有获取/打印行中的任何数据。我哪里出错了?
这就是我所拥有的:
/* Code written by @hubgit
https://gist.github.com/hubgit/3755293
Updated since DocsList is deprecated
*/
function listFilesInFolder(folderName) {
var folder = DriveApp.getFoldersByName(folderName).next();
var contents = folder.getFiles();
var file, data, sheet = SpreadsheetApp.getActiveSheet();
sheet.clear();
sheet.appendRow(["Name", "Date", "Size", "URL", "Download", "Description", "Type"]);
for (var i = 0; i < contents.length; i++) {
file = contents[i];
if (file.getFileType() == "SPREADSHEET") {
continue;
}
data = [
file.getName(),
file.getDateCreated(),
file.getSize(),
file.getUrl(),
"https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
file.getDescription(),
file.getFileType().toString()
];
sheet.appendRow(data);
}
};
答案 0 :(得分:2)
您需要根据需要定制代码。首先在ListFilesInFolder(folderName)
中没有传递任何内容,因此请删除folderName
部分。然后尝试使用特定的文件夹名称。接下来,如果我们不想限制在Spreadsheets。我们需要删除这些行:
if (file.getFileType() == "SPREADSHEET") {
continue;
}
事实上,for loop
contents.hasNext()
就足够了。所以函数最终会是这样的。它正在发挥作用。
function listFilesInFolder() {
var folder = DriveApp.getFoldersByName('SRL').next();
Logger.log(folder);
var contents = folder.getFiles();
Logger.log(contents);
var file, data, sheet = SpreadsheetApp.getActiveSheet();
sheet.clear();
sheet.appendRow(["Name", "Date", "Size", "URL", "Download", "Description", "Type"]);
if (contents.hasNext()) {
file = contents.next();
Logger.log(file);
data = [
file.getName(),
file.getDateCreated(),
file.getSize(),
file.getUrl(),
"https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
file.getDescription(),
];
sheet.appendRow(data);
}
};
答案 1 :(得分:1)
如果您对文件夹ID进行硬编码,则以下情况有效。
function listFilesInFolder(folderName) {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow(["Name", "Date", "Size", "URL", "Download", "Description", "Type"]);
var folder = DriveApp.getFolderById("PUT_YOUR_FOLDERID_IN_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(),
"https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
file.getDescription(),
file.getMimeType(),
];
sheet.appendRow(data);
};
};