0

I want to list all the files in a folder on google drive but I'm having problems as it gives me the error:

Exceeded maximum execution time

So I'm unable to list all the files.

I'm using this google sheet script

// replace your-folder below with the folder for which you want a listing
function listFolderContents() {
  var foldername = 'your-folder';
  var folderlisting = 'listing of folder ' + foldername;

  var folders = DriveApp.getFoldersByName(foldername)
  var folder = folders.next();
  var contents = folder.getFiles();

  var ss = SpreadsheetApp.create(folderlisting);
  var sheet = ss.getActiveSheet();
  sheet.appendRow( ['name', 'link'] );

  var file;
  var name;
  var link;
  var row;
  while(contents.hasNext()) {
    file = contents.next();
    name = file.getName();
    link = file.getUrl();
    sheet.appendRow( [name, link] );     
  }  
};

Is there anyway to avoid the max execution time? like continue where it last stopped?

Rubén
  • 34,714
  • 9
  • 70
  • 166
TravelWhere
  • 327
  • 1
  • 5
  • 17
  • 2
    Possible duplicate of [Correct usage of DriveApp.continueFileIterator(continuationToken)](https://stackoverflow.com/questions/22365681/correct-usage-of-driveapp-continuefileiteratorcontinuationtoken) – Rubén Dec 05 '18 at 01:06
  • 2
    I think that @Rubén's comment can resolve your issue. As other solution, how about this? From your script, I thought that the process cost of ``appendRow()`` might be the reason of your issue. So how about trying to use [``setValues()``](https://developers.google.com/apps-script/reference/spreadsheet/range#setvaluesvalues) instead of ``appendRow()``, because the cost of ``appendRow()`` is much higher than that of ``setValues()``? The experimental data of cost can be seen at https://gist.github.com/tanaikech/d102c9600ba12a162c667287d2f20fe4 – Tanaike Dec 05 '18 at 01:16

0 Answers0