如何将Google云端硬盘文件详细信息转换为电子表格 [英] How to get Google Drive file details into spreadsheet
问题描述
我想从Google云端硬盘获取文件详细信息,并将它们存储在Google电子表格中。
我发现了这段代码,但它给出了一个错误:
在对象文件中找不到函数。 (第2行,文件代码)
$ b
/ **
*列出Google云端硬盘文件夹中的所有文件。
*
* http://ctrlq.org/code/19854-list-files-in-google-drive-folder
* https://gist.github.com/hubgit/ 3755293
* /
函数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);
}
};
正如桑迪的答案指出的那样,已收到是因为没有找到文件夹对象
DriveApp.getFoldersByName(folderName)
。 然而,你会仍然存在问题,因为当您弃用FilesList时,您引用的脚本仅部分转换为使用DriveApp。
-
folder.getFiles( )
作为一个数组,当它实际上是一个迭代器时。 DriveApp文件没有 -
该代码中的下载网址不再有w兽人。您可以从高级云端硬盘API 获取导出链接 - 我已经只是评论了有问题的代码。
getFileType()
方法,而是支持 getMimeType()
,并且有一个 MimeType
enumerator 进行比较。 此代码的更完整版本可在此要点中找到。
$ b / **
*列出Google云端硬盘文件夹中的所有文件。
*
* @param {string} folderName(可选)Google云端硬盘文件夹的名称
*
*改编自:
* http://ctrlq.org / code / 19854-list-files-in-google-drive-folder
* https://gist.github.com/hubgit/3755293
* /
函数listFilesInFolder(folderName){
//如果我们没有提供一个文件夹名称,假设我们将与用户交互。
var interactive =(typeof folderName ==='undefined');
//如果(交互式){
folderName = Browser.inputBox(列出文件夹中的文件,输入文件夹名称,浏览器)获取文件夹的名称以列出
。 Buttons.OK_CANCEL);
}
if(folderName ==='')return; //没有提供名字,安静地退出
var folders = DriveApp.getFoldersByName(folderName);
if(!folders.hasNext()){
if(interactive)Browser.msgBox(Folder not found。);
return;
}
var folder = folders.next();
var contents = folder.getFiles();
var file,data,sheet = SpreadsheetApp.getActiveSheet();
sheet.clear();
$ b sheet.appendRow([Name,Date,Size,URL,/ *Download,* /Description,Type]);
//使用文件迭代器循环文件夹中的文件
while(contents.hasNext()){
file = contents.next();
if(file.getMimeType()== MimeType.GOOGLE_SHEETS){//SPREADSHEET
//跳过显示电子表格 - 我不知道为什么...
继续;
}
data = [
file.getName(),
file.getDateCreated(),
file.getSize(),
file.getUrl(),
//\"https://docs.google.com/uc?export=download&confirm=no_antivirus&id=+ file.getId(),
file.getDescription (),
niceFileType(file.getMimeType())
];
sheet.appendRow(data);
$ b
*列出Google云端硬盘文件夹中的所有文件。
*
* @param {string} folderName(可选)Google云端硬盘文件夹的名称
*
*改编自:
* http://ctrlq.org / code / 19854-list-files-in-google-drive-folder
* https://gist.github.com/hubgit/3755293
* /
函数listFilesInFolder(folderName){
//如果我们没有提供一个文件夹名称,假设我们将与用户交互。
var interactive =(typeof folderName ==='undefined');
//如果(交互式){
folderName = Browser.inputBox(列出文件夹中的文件,输入文件夹名称,浏览器)获取文件夹的名称以列出
。 Buttons.OK_CANCEL);
}
if(folderName ==='')return; //没有提供名字,安静地退出
var folders = DriveApp.getFoldersByName(folderName);
if(!folders.hasNext()){
if(interactive)Browser.msgBox(Folder not found。);
return;
}
var folder = folders.next();
var contents = folder.getFiles();
var file,data,sheet = SpreadsheetApp.getActiveSheet();
sheet.clear();
$ b sheet.appendRow([Name,Date,Size,URL,/ *Download,* /Description,Type]);
//使用文件迭代器循环文件夹中的文件
while(contents.hasNext()){
file = contents.next();
if(file.getMimeType()== MimeType.GOOGLE_SHEETS){//SPREADSHEET
//跳过显示电子表格 - 我不知道为什么...
继续;
}
data = [
file.getName(),
file.getDateCreated(),
file.getSize(),
file.getUrl(),
//\"https://docs.google.com/uc?export=download&confirm=no_antivirus&id=+ file.getId(),
file.getDescription (),
niceFileType(file.getMimeType())
];
sheet.appendRow(data);
$ b
niceFileType 助手功能
/ **
* ,可读文件类型匹配给定MimeType。
*请参阅https://developers.google.com/apps-script/reference/base/mime-type
*
* @param {string} mimeType匹配$ b的文件类型$ b *
* @return {string}UNKNOWN或匹配的文件类型。
* /
函数niceFileType(mimeType){
//支持的文件类型存储在全局文件类型对象中用于快速查找。
if(typeof this.fileType ==='undefined'){
//仅初次初始化全局文件类型对象。
this.fileType = {};
// ...
this.fileType [MimeType.FOLDER] =文件夹;
// ...
this.fileType [MimeType.GOOGLE_APPS_SCRIPT] =Google Apps Script;
this.fileType [MimeType.GOOGLE_DOCS] =Google Doc;
this.fileType [MimeType.GOOGLE_DRAWINGS] =Google绘图;
this.fileType [MimeType.GOOGLE_FORMS] =Google Form;
this.fileType [MimeType.GOOGLE_SHEETS] =Google Sheet;
// ...
}
//如果我们知道给定的文件类型,请返回它的名称。
return(this.fileType.hasOwnProperty(mimeType))? this.fileType [mimeType]:UNKNOWN;
}
I want to get file details from Google Drive, and store them in a Google Spreadsheet. I found this code but it gives an error:
Cannot find function next in object Files. (line 2, file "Code")
Code is from this blog, possibly evolved from this gist.
/**
* List all files in Google Drive folder.
*
* http://ctrlq.org/code/19854-list-files-in-google-drive-folder
* https://gist.github.com/hubgit/3755293
*/
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);
}
};
As Sandy's answer points out, the error message you've received is because no folder object is found by DriveApp.getFoldersByName(folderName)
.
However, you'll still have a problem, because the script you reference was only partially converted to use DriveApp when FilesList was deprecated.
It is treating the result of
folder.getFiles()
as an array, when it is actually an iterator.DriveApp files don't have the
getFileType()
method, instead they supportgetMimeType()
, and there is aMimeType
enumerator to compare to.The download URL in that code no longer works. You are able to get an export link from the Advanced Drive API - I've just commented out the offending code.
Code
A more complete version of this code is available in this gist.
/**
* List all files in Google Drive folder.
*
* @param {string} folderName (optional) Name of folder on Google Drive
*
* Adapted from:
* http://ctrlq.org/code/19854-list-files-in-google-drive-folder
* https://gist.github.com/hubgit/3755293
*/
function listFilesInFolder(folderName) {
// If we have not been provided a folderName, assume we will interact with user.
var interactive = (typeof folderName === 'undefined');
// Get name of folder to list
if (interactive) {
folderName = Browser.inputBox("List files in folder", "Enter folder name", Browser.Buttons.OK_CANCEL);
}
if (folderName === '') return; // No name provided, exit quietly
var folders = DriveApp.getFoldersByName(folderName);
if (!folders.hasNext()) {
if (interactive) Browser.msgBox("Folder not found.");
return;
}
var folder = folders.next();
var contents = folder.getFiles();
var file, data, sheet = SpreadsheetApp.getActiveSheet();
sheet.clear();
sheet.appendRow(["Name", "Date", "Size", "URL", /*"Download",*/ "Description", "Type"]);
// Loop over files in folder, using file iterator
while (contents.hasNext()) {
file = contents.next();
if (file.getMimeType() == MimeType.GOOGLE_SHEETS) { // "SPREADSHEET"
// Skip displaying spreadsheets - I don't know why...
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(),
niceFileType( file.getMimeType() )
];
sheet.appendRow(data);
}
}
niceFileType
Helper function
/**
* Get nice, readable file type matching given MimeType.
* Refer to https://developers.google.com/apps-script/reference/base/mime-type
*
* @param {string} mimeType File type to match
*
* @return {string} "UNKNOWN" or matched file type.
*/
function niceFileType( mimeType ) {
// Supported file types stored in global fileType object for fast lookups.
if (typeof this.fileType === 'undefined') {
// Initialize global fileType object first time only.
this.fileType = {};
// ...
this.fileType[MimeType.FOLDER] = "Folder";
// ...
this.fileType[MimeType.GOOGLE_APPS_SCRIPT] = "Google Apps Script";
this.fileType[MimeType.GOOGLE_DOCS] = "Google Doc";
this.fileType[MimeType.GOOGLE_DRAWINGS] = "Google Drawing";
this.fileType[MimeType.GOOGLE_FORMS] = "Google Form";
this.fileType[MimeType.GOOGLE_SHEETS] = "Google Sheet";
// ...
}
// If we know the given filetype, return its name.
return (this.fileType.hasOwnProperty(mimeType)) ? this.fileType[mimeType] : "UNKNOWN";
}
这篇关于如何将Google云端硬盘文件详细信息转换为电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!