title: Update google spreadsheet automatically when a new file is added to google drive
body: <p>I need to get a google spreadsheet sheet updated when a new file gets added to a certain google drive folder.</p>
  
  <p>It needs to pull data such as 'name', 'date' and 'url link'.</p>
  
  <p>I've read somehwere that google sheet scripts won't let you add "new file" as event, so I will have to run it every 30 minutes, theres no issue with that;
  What I can't do is clear the spreadsheet and create a new one, since the content is a lot and it takes around 10 minutes to fill the spreadsheet with all the data from google drive.</p>
  
  <p>This is the script I've used to create the spreadsheet </p>
  
  <pre><code>function listFolders(folder) {
       var sheet = SpreadsheetApp.getActiveSheet();
       sheet.appendRow(["Name", "Sharing Access", "Sharing Permission", "Get Editors", "Get Viewers", "Date", "Size", "URL", "Download", "Description", "Type"]); //writes the headers
       var folder = DriveApp.getFolderById("YOUR_FOLDER_ID");//that long chunk of random numbers/letters in the URL when you navigate to the folder
  
       var files = folder.getFiles();//initial loop on loose files w/in the folder
  
       var cnt = 0;
       var file;
  
       while (files.hasNext()) {
           var file = files.next();
           var listEditors = file.getEditors(); //gets the editor email(s), doesn't show your own as it's assumed
           var editors = [];
           for (var cnt = 0; cnt &lt; listEditors.length; cnt++) {
               editors.push(listEditors[cnt].getEmail());
               Logger.log(editors);
           };
           var listViewers = file.getViewers(); //gets the viewer email(s)
           var viewers = [];
           for (var cnt = 0; cnt &lt; listViewers.length; cnt++) {
               viewers.push(listViewers[cnt].getEmail());
               Logger.log(viewers);
           }
           cnt++;  //data chunk pushes all the file info to the ss
  
           data = [
               file.getName(),
               file.getSharingAccess(),
               file.getSharingPermission(),
               editors.toString(),
               viewers.toString(),
               file.getDateCreated(),
               file.getSize(),
               file.getUrl(),
               "https://docs.google.com/uc?export=download&amp;confirm=no_antivirus&amp;id=" + file.getId(),
               file.getDescription(),
               file.getMimeType(),
           ];
  
           sheet.appendRow(data);
  
       };
  
  
       var subfolders = folder.getFolders(); //same thing as above but for all the subfolders in the folder
  
       while (subfolders.hasNext()) {
           //Logger.log(folder);
           var name = subfolders.next();
  
           var files = name.getFiles();
  
           var cnt = 0;
           var file;
  
           while (files.hasNext()) {
               var file = files.next();
               var listEditors = file.getEditors();
               var editors = [];
               for (var cnt = 0; cnt &lt; listEditors.length; cnt++) {
                   editors.push(listEditors[cnt].getEmail());
                   Logger.log(editors);
               };
               var listViewers = file.getViewers();
               var viewers = [];
               for (var cnt = 0; cnt &lt; listViewers.length; cnt++) {
                   viewers.push(listViewers[cnt].getEmail());
                   Logger.log(viewers);
               }
               cnt++;
  
               data = [
                   file.getName(),
                   file.getSharingAccess(),
                   file.getSharingPermission(),
                   editors.toString(),
                   viewers.toString(),
                   file.getDateCreated(),
                   file.getSize(),
                   file.getUrl(),
                   "https://docs.google.com/uc?export=download&amp;confirm=no_antivirus&amp;id=" + file.getId(),
                   file.getDescription(),
                   file.getMimeType(),
               ];
               sheet.appendRow(data);
           };
       }
   }
  </code></pre>
  
  <p>Thanks in advance.</p>