1

I have a folder: https://drive.google.com/drive/folders/1_70Q4BPQrOHCQU4eUleEPuxbr0hpfhBB

This folder has some files inside. I want to create a google-appscript to list the files in a Spreadsheet, in a way it look as close as possible to this: https://docs.google.com/spreadsheets/d/11KQYtJvEi1nbw0Awp7xgitOPgc9RhkMLICBGvvJrYlU/edit#gid=0

enter image description here

How could I do that?

Marios
  • 26,333
  • 8
  • 32
  • 52
aabujamra
  • 4,494
  • 13
  • 51
  • 101

1 Answers1

4

Solution:

The following code will do the job:

function findFilesInfo() {

  const folderId = '1_70Q4BPQrOHCQU4eUleEPuxbr0hpfhBB'
  const folder = DriveApp.getFolderById(folderId)
  const files = folder.getFiles()
  const source = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = source.getSheetByName('Página1');
  const data = [];
  
  while (files.hasNext()) {
      const childFile = files.next();
      var info = [ 
        childFile.getName(), 
        childFile.getUrl(),
        childFile.getLastUpdated(),
        Drive.Files.get(childFile.getId()).lastModifyingUser.displayName     
      ];
    
    data.push(info);
  }
  sheet.getRange(2,1,data.length,data[0].length).setValues(data);
}

Requirements:

  1. Create a sheet with the name Página1 (you can adjust it of course according to your needs) that has the following structure:

structure

  1. In Resouces > Advanced Google services: enable Drive API:

enable

  1. Specify the 'folderID' of your folder here:

    const folderId = '1_70Q4BPQrOHCQU4eUleEPuxbr0hpfhBB';
    

Restrictions:

You can't use findFilesInfo() as a custom function within your google-sheet file. You can either run it directly from google script editor by clicking on the play button:

restriction

or you can create a custom button (macros) where you can click on it from your google-sheet file and execute the function.

Marios
  • 26,333
  • 8
  • 32
  • 52
  • Thanks, great suggestion. How could I set the folderId as a variable to be inputed in the function formula? `findFilesInfo (x)` and `const folderId = x`? – aabujamra Aug 17 '20 at 20:26
  • I enabled Drive API but I'm still getting this error: "Erro Exception: You do not have permission to call DriveApp.getFolderById. Required permissions: (https://www.googleapis.com/auth/drive.readonly || https://www.googleapis.com/auth/drive) (linha 12).". How could I dribble it? – aabujamra Aug 17 '20 at 20:42
  • @abutremutante please check this post for reference : https://stackoverflow.com/questions/35028119/driveapp-getfolderbyid-results-in-you-do-not-have-permission-error Could you please run only the findFilesInfo() without the X value and the var folderId as I had it in my original code ? – Marios Aug 17 '20 at 20:45
  • Just ran using your original code and using the sample file and folder. Still not working... You can check there: https://docs.google.com/spreadsheets/d/11KQYtJvEi1nbw0Awp7xgitOPgc9RhkMLICBGvvJrYlU/edit#gid=0 – aabujamra Aug 17 '20 at 21:10
  • 3
    @MariosKaramanis OP is probably trying to use it as a custom function. [One of the reasons you avoid codeless questions is follow up questions with little debugging details] – TheMaster Aug 17 '20 at 21:30
  • @abutremutante see my updated answer. You can't run this function as a custom function (formula) in your google-sheet. You can only run it from the script editor or create a macro button to do that. (Let me know if you want the latter, though you should ask another question for that). – Marios Aug 17 '20 at 21:40
  • 1
    @MariosKaramanis - or a menu item, if we are listing possible approaches of how it could be used. Nice and easy solution, btw – Oleg Valter is with Ukraine Aug 17 '20 at 22:53
  • 1
    @OlegValter thanks ! This is what I mean by macro button , a menu button . – Marios Aug 17 '20 at 22:54
  • @MariosKaramanis, great it worked now, thanks a lot – aabujamra Aug 17 '20 at 23:24