Generate Download URLs for Files in Google Drive Directory

I’m trying to build a script that lists all the files in a specific Google Drive folder and then converts each file’s URL into a download link. The listing part works fine, but I’m stuck on transforming the generated URLs into download links.

Below is the modified version of my code:

function showFolderItems() {
  var dirName = 'MyImages';
  var dirList = 'Contents of ' + dirName + ' ' + new Date();
  
  var folderIterator = DriveApp.getFoldersByName(dirName);
  if (folderIterator.hasNext()) {
    var folder = folderIterator.next();
    var fileIterator = folder.getFiles();

    var ss = SpreadsheetApp.create(dirList);
    var sheet = ss.getActiveSheet();
    sheet.appendRow(['File', 'URL']);

    while(fileIterator.hasNext()) {
      var file = fileIterator.next();
      // Adjust the URL format here to create a download link
      var originalUrl = file.getUrl();
      var baseUrl = originalUrl.split('/edit')[0];
      var downloadUrl = baseUrl + '/export?format=png';
      sheet.appendRow([file.getName(), downloadUrl]);
    }
  }
}

I set the folder to be viewable by anyone with the link and all files in the folder are PNG images. Any guidance on tweaking this download link conversion would be appreciated. I’m not a coding expert, so any simple, clear help is welcome. Thanks!

I’ve encountered this issue before, and there’s a simpler solution. Instead of modifying the URL, you can use the built-in method getDownloadUrl() for each file. This method automatically generates the correct download link.

Here’s how you can update your code:

while(fileIterator.hasNext()) {
  var file = fileIterator.next();
  var downloadUrl = file.getDownloadUrl();
  sheet.appendRow([file.getName(), downloadUrl]);
}

This approach works for all file types and doesn’t require any URL manipulation. Just ensure your folder permissions are set correctly. If you’re still having issues, double-check that the folder and files are shared with ‘Anyone with the link can view’ access.

Remember, Google might limit the number of downloads or change their API, so always test your script thoroughly.

I’ve dealt with a similar issue before, and I think I can help you out. The main problem is that you’re using the wrong URL format for direct downloads. Instead of ‘/export?format=png’, you should use ‘/uc?export=download’.

Here’s how you can modify your code:

var downloadUrl = 'https://drive.google.com/uc?export=download&id=' + file.getId();

This format works for any file type, not just PNGs. It tells Google Drive to serve the file as a download rather than trying to display it in the browser.

Also, make sure your folder and files have the correct sharing settings. They should be set to ‘Anyone with the link can view’ for this to work properly.

One last tip: consider adding error handling. Sometimes Google Drive can be finicky, so it’s good to have some checks in place. You could wrap your main code in a try-catch block to handle any unexpected issues.

Hope this helps! Let me know if you run into any other problems.

hey JackHero77, i’ve got another way to do this. instead of messing with URLs, try using the file ID directly. Change your code to:

var downloadUrl = 'https://drive.google.com/uc?id=' + file.getId();

this should work for any file type. just make sure your folder permissions are set right. lmk if you need more help!