Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Make a list on Google SpreadSheet of all files in a Google Drive folder

I have a Folder With more than 2000 Files.. And i Need to make a List of all these files on google spreadsheet.. I Found online some Scripts.. but they're not completely working. When i hit "RUN" i just get a list of 250 Files.

Reading on Google Developers page i found some things about enabling Google Drive advanced services (and i did it).. And i think i could solve this problem using something named "tokens"??

I don't know.. i'm not a programmer, and i barely know english.. i Tried editing this script making a fusion of what i found online.. But anything works.. i just get errors that i can't even understand..

So.. is there someone able to fix it?

function listFilesInFolder() {
  var folder = DocsList.getFolder("Film");
  var contents = folder.getFiles();
  var file;
  var data;

  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear();

  sheet.appendRow(["Nome", "Data", "Dimensione"]);

  for (var i = 0; i < contents.length; i++) {
    file = contents[i];

    if (file.getFileType() == "SPREADSHEET") {
      continue;
    }

    data = [ 
      file.getName(),
      file.getDateCreated(),
      file.getSize(),
    ];

    sheet.appendRow(data);
  }
};
like image 518
Jonathan Livingston Avatar asked Sep 05 '14 10:09

Jonathan Livingston


People also ask

How do I view files in Google Drive as a list?

Google Drive allows you to view your documents as thumbnail images or in a text list. The icon is in the toolbar to the left of the AZ icon. To switch from tile view to list view the icon resembles the list view of lines of text.


2 Answers

The answer above appends a row in every iteration wich is particularly slow and there is a chance you will exceed the maximum execution time (see best practices)) so here is a version that uses an array to collect data and writes the array using a single setValues() .

The other issue is that it gets all the files in your drive, not in the folder you chose...

so below is a version that gets all files that are not Google documents, ie it counts only files that take space (images, pdf...) with a maximum of 4000 files.

full code below :

function listFilesInFolder() {
  var folder = DocsList.getFolderById('0B3qSFd3iikE3MS0yMzU4YjQ4NC04NjQxLTQyYmEtYTExNC1lMWVhNTZiMjlhMmI');
  var file;
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear();
  var data = [];
  data.push(["Name", "Data", "Size", "url"]);
  var filesresult = folder.getFilesByTypeForPaging(DocsList.FileType.OTHER, 4000);
  var files = filesresult.getFiles();
  Logger.log(files.length);
  for (var i in files) {
    file = files[i];
    data.push([ 
      file.getName(),
      file.getDateCreated(),
      file.getSize(),
      file.getUrl()
    ]);
  }
  sheet.getRange(1,1,data.length,data[0].length).setValues(data);   
}
like image 34
Serge insas Avatar answered Oct 19 '22 22:10

Serge insas


This Script works for at least 2200 Files :)

function listFilesInFolder(id) {
  var folder = DriveApp.getFolderById('MyFolderID');
  var contents = folder.getFiles();
  var file;
  var name;
  var sheet = SpreadsheetApp.getActiveSheet();
  var date;
  var size;

  sheet.clear();
  sheet.appendRow(["Nome", "Data", "Dimensione"]);


  while(contents.hasNext()) {
    file = contents.next();
    name = file.getName();
    date = file.getDateCreated()
    size = file.getSize()

    data = [name, date, size]
    sheet.appendRow(data);
  }
};
like image 65
Jonathan Livingston Avatar answered Oct 19 '22 23:10

Jonathan Livingston