显示Google网站上所有Google云端硬盘文件的列表

时间:2016-02-26 02:35:51

标签: html google-apps-script google-sites drive

我正在尝试将其显示为网络应用。它只返回网站上的1个文件。日志显示50多个文件。为什么htmloutput没有像日志那样显示所有文件。任何帮助都会很棒。

function doGet(e) {
  var query = 'fullText contains "Printers"' ;
  var folders, pageToken;
  do {
    folders = Drive.Files.list({
      q: query,
      maxResults: 100,
      pageToken: pageToken
    });

    if (folders.items && folders.items.length > 0) {
      for (var i = 0; i < folders.items.length; i++) {
        var folder = folders.items[i];

        Logger.log('%s (ID: %s)', folder.title, folder.id);
      }
    } else {
    }
    var Output3 = folder.id; 
    var output = HtmlService.createHtmlOutput(Output3);
    pageToken = folders.nextPageToken;
  } while (pageToken);

  return output
}

1 个答案:

答案 0 :(得分:1)

每次for循环迭代时,它都会覆盖以前的内容。

var Output3 = folder.id;

你需要使用类似的东西:

var Output3 = "";//define this outside the `for` loop
Output3 = Output3 + folder.id;

以下是对完整代码的修改:

function doGet() {
  var query = 'fullText contains "Printers"' ;
  var folders, folder, pageToken, foldersLngth = 0, theItems, output="";

  do {
    folders = Drive.Files.list({
    q: query,
    maxResults: 100,
    pageToken: pageToken
  });

  foldersLngth = folders.items.length;
  theItems = folders.items;

  if (theItems && foldersLngth > 0) {

    for (var i=0; i < foldersLngth; i++) {
      folder = folders.items[i];
      Logger.log('%s (ID: %s)', folder.title, folder.id);
      output = output + folder.title + ", " + folder.id;
    };
  };

  //var Output3 = folder.id; 
  //HtmlService.createHtmlOutput(Output3);
  pageToken = folders.nextPageToken;
  } while (pageToken);

  return HtmlService.createHtmlOutput(output);
};