1

So i recently found this google drive script and tried to use it. The script should normally get file names and url on my google drive folder and copy them in a spreadsheet.

When i click Run, I get no error and nothing happens in my drive.

Is there variables i should change to make it work ?

PS : I'm very new to coding and can't seem to find what is wrong with this code

Thanks in advance for your help !

Here is the code :

function myFunction() {

  function listFilesInFolder(foldername) {
  // If we have not been provided a foldername, assume we will interact with user.
  var interactive = (typeof foldername === 'undefined');

  // Get name of folder to list
  if (interactive) {
    foldername = Browser.inputBox("List files in folder", "Enter folder name", Browser.Buttons.OK_CANCEL);
  }

  if (foldername === '') return;  // No name provided, exit quietly

  var folders = DriveApp.getFoldersByName(foldername);
  if (!folders.hasNext()) {
    if (interactive) Browser.msgBox("Folder not found.");
    return;
  }
  var folder = folders.next();
  var contents = folder.getFiles();

  var file, data, sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear();

  sheet.appendRow(["Name", "Date", "Size", "URL", /*"Download",*/ "Description", "Type"]);

  // Loop over files in folder, using file iterator
  while (contents.hasNext()) {
    file = contents.next();

    if (file.getMimeType() == MimeType.GOOGLE_SHEETS) { // "SPREADSHEET"
      // Skip displaying spreadsheets - I don't know why...
      continue;
    }

    data = [ 
      file.getName(),
      file.getDateCreated(),
      file.getSize(),
      file.getUrl(),
      //"https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
      file.getDescription(),
      niceFileType( file.getMimeType() )
    ];

    sheet.appendRow(data);

  }
}

}
6
  • Maybe you should enable Drive API first. Check this. Commented Feb 28, 2017 at 2:55
  • @SangbokLee Just did it, and script still not working
    – Mk1
    Commented Feb 28, 2017 at 10:21
  • You don't need to enable the Drive API to use DriveApp. Is this script attached to a spreadsheet? Commented Feb 28, 2017 at 13:03
  • @SpencerEaston I don't know really. The only line i find mentioning "spreadsheet" is this [ var file, data, sheet = SpreadsheetApp.getActiveSheet(); sheet.clear(); ] but i don't know if i should create a specific sheet or add name to code for it to work
    – Mk1
    Commented Feb 28, 2017 at 14:34
  • This script needs to be ran in a spreadsheet. Create a new spreadsheet. Click tools->Script editor. Add your code to the script that is opened. It should be able to run in that context. Commented Feb 28, 2017 at 14:42

2 Answers 2

1

Two issues. This needs to be ran in a script attached to a spreadsheet and you have a nested function.

Your code:

function myFunction() {
  function listFilesInFolder(foldername) {
    ...
  }
 }

remove the outer function decleration and its matching closing bracket. The code will run.

It should look like:

function listFilesInFolder(foldername) {
 ...
}
0

Try this modified code in spreadsheet instead:

function myfunction(){
//Declaring the function listFolders to temp
var temp = function listFolders(foldername) {
  // If we have not been provided a foldername, assume we will interact with user.
  var interactive = (typeof foldername === 'undefined');

  // Get name of folder to list
  if (interactive) {
    foldername = Browser.inputBox("List files in folder", "Enter folder name", Browser.Buttons.OK_CANCEL);
  }

  if (foldername === '') return;  // No name provided, exit quietly

  var folders = DriveApp.getFoldersByName(foldername);
  if (!folders.hasNext()) {
    if (interactive) Browser.msgBox("Folder not found.");
    return;
  }
  var folder = folders.next();
  var contents = folder.getFiles();

  var file, data, sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear();

  sheet.appendRow(["Name", "Date", "Size", "URL", /*"Download",*/ "Description", "Type"]);

  // Loop over files in folder, using file iterator
  while (contents.hasNext()) {
    file = contents.next();

    if (file.getMimeType() == MimeType.GOOGLE_SHEETS) { // "SPREADSHEET"
      // Skip displaying spreadsheets - I don't know why...
      continue;
    }

    data = [ 
      file.getName(),
      file.getDateCreated(),
      file.getSize(),
      file.getUrl(),
      //"https://docs.google.com/uc?export=download&confirm=no_antivirus&id=" + file.getId(),
      file.getDescription(),
      file.getMimeType() 
    ];

    sheet.appendRow(data);

  }
}
// calls the  function listFolders below
temp()
}
2
  • I assumed niceFileType() was somewhere in the code, but good call fixing it. Commented Feb 28, 2017 at 21:16
  • Thanks, I had just finished typing the answer out when you posted the answer. Else wouldn't have posted it, rather modified your answer to include the change. .
    – Jack Brown
    Commented Feb 28, 2017 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.