如何将Google云端硬盘文件详细信息转换为电子表格

时间:2015-04-30 09:39:48

标签: google-apps-script google-sheets

我想从Google云端硬盘获取文件详细信息,并将其存储在Google电子表格中。 我找到了这段代码,但它给出了一个错误:

  

在对象文件中找不到下一个功能。 (第2行,文件“代码”)

代码来自this blog,可能是从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);

  }

};

2 个答案:

答案 0 :(得分:2)

正如Sandy的回答所指出的,您收到的错误消息是因为DriveApp.getFoldersByName(folderName)找不到文件夹对象。

但是,您仍然会遇到问题,因为您引用的脚本在不推荐使用FilesList时仅部分转换为使用DriveApp。

  • 它将folder.getFiles()的结果视为一个数组,当它实际上是一个迭代器时。

  • DriveApp文件没有getFileType()方法,而是支持getMimeType(),并且需要MimeType enumerator进行比较。

  • 该代码中的下载网址不再有效。您可以从Advanced Drive API获取导出链接 - 我刚刚注释了有问题的代码。

代码

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助手功能

/**
 * 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";
}

答案 1 :(得分:0)

你可以改变这个:

function listFilesInFolder(folderName) {

  var folder = DriveApp.getFoldersByName(folderName).next();

对此:

function listFilesInFolder() {

  var folderName = "type your folder name here between the quotes";
  var folder = DriveApp.getFoldersByName(folderName).next();

显然,您正在运行代码,因此通过在语句中键入 您的 文件夹名称来“硬编码”文件夹名称。