2

I currently have a Google App Script in Google Sheet that gives me the URL of a folder, which I can then use to download. Though it is an extra step I would like to remove, and get the URL of the zipped content directly.

Here's my code (google app script):

function downloadSelectedScripts() {
  // ...
  var scriptFiles = getScriptFiles(scriptFileNames)
  var tempFolder = copyFilesToTempFolder(scriptFiles)
  Browser.msgBox(tempFolder.getUrl())
}

function copyFilesToTempFolder(files) {
  var tempFolder = DriveApp.getFolderById(FOLDERS.TEMP)
  var tempSubFolder = tempFolder.createFolder('download_' + Date.now())

  for (var i in files) {
    var file = files[i]
    file.makeCopy(file.getName(), tempSubFolder)
  }

  return tempSubFolder
}
Dave
  • 457
  • 1
  • 7
  • 16
  • I apologize for my poor English skill. Can I ask you about the detail of ``I would like to remove, and get the URL of the zipped content directly.``? – Tanaike May 24 '19 at 22:13
  • @Tanaike tempFolder.getUrl() gives me the drive folder URL. When I got to this URL I then have to click "Download" on the folder, then it downloads as zip. I would like to skip this step and get the ZIP file url directly. – Dave May 25 '19 at 06:19
  • Try `webContentLink` – pa1.Shetty May 25 '19 at 07:15
  • @Dave Thank you for replying. Unfortunately, I cannot understand about your goal, yet. I'm sorry. So can I ask you about your replying? 1. What mean about ``click "Download" on the folder``? 2. What is ``this step`` of ``skip this step``? 3. You want to download all files in a folder as a zip file. Is my understanding correct? – Tanaike May 25 '19 at 08:00
  • @pa1.Shetty I just tried https://www.googleapis.com/drive/v2/files/folderId on a folder and the response does not contain webContentLink – Dave May 25 '19 at 14:53
  • @Tanaike yes I want to download all files in a folder as zip using the API – Dave May 25 '19 at 14:53
  • @Dave Thank you for replying. I could understand about your goal. In order to think of the solution of your goal, I have several questions. 1. How much total size of all files in the folder? 2. Do the folder have any subfolders? 3. When Google Docs (Spreadsheet, Document, Slides and so on) are included in the folder, how do you want to do? – Tanaike May 25 '19 at 23:17
  • @Tanaike there are no sub folders. The files are small .txt files. No google docs files – Dave May 26 '19 at 08:26
  • @Dave Thank you for replying. I thought that I could confirm your situation. So I proposed a sample script as an answer. Could you please confirm it? If I misunderstood your question and that was not the result you want, I apologize. – Tanaike May 26 '19 at 08:53

1 Answers1

2
  • You want to compress all files in a folder as a zip file.
    • The folder has no subfolders.
    • All files are only text files.
    • The total size of all files is less than 50 MB.
  • You want to retrieve the URL for downloading the zip file.

If my understanding is correct, how about this sample script? The flow of this script is as follows.

  1. Retrieve folder.
  2. Retrieve blobs of all files in the folder.
  3. Compress blobs and retrieve a blob of zip.
  4. Create a zip blob as a file.
  5. Retrieve URL for downloading.

Sample script:

When you use this script, please set the folder ID of folder that you want to compress.

function myFunction() {
  var folderId = "###"; // Please set the folder ID here.

  var folder = DriveApp.getFolderById(folderId);
  var files = folder.getFiles();
  var blobs = [];
  while (files.hasNext()) {
    blobs.push(files.next().getBlob());
  }
  var zipBlob = Utilities.zip(blobs, folder.getName() + ".zip");
  var fileId = DriveApp.createFile(zipBlob).getId();
  var url = "https://drive.google.com/uc?export=download&id=" + fileId;
  Logger.log(url);
}

Result:

The direct link of the zip file is returned as follows.

https://drive.google.com/uc?export=download&id=###

Note:

  • In the current stage, unfortunately, the folders cannot be included in the zip data with Google Apps Script.
  • In this sample script, the filename of zip file is the folder name. So please modify it for your situation.
  • If you want to download the zip file without login to Google, please share the file.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thanks, your solution actually works but there is one major problem, if I am not logged in with the user that owns the drive as user #1, then it says "We're sorry, but you do not have access to this page." I have many google accounts so most of the time the user will be #2 or #3 – Dave May 27 '19 at 08:05
  • @Dave Thank you for replying. I apologize for my poor English skill. When you want to download the zip file from other accounts (those other accounts are logged in Google.), the created zip file is required to be shared with other accounts. When you want to download the zip file without login to Google, the created zip file is required to be shared publicly. – Tanaike May 27 '19 at 08:18
  • I am logged in, but the user is not logged in as primary, it is logged in as user #2. – Dave May 27 '19 at 08:20
  • @Dave I apologize for my poor English skill. Unfortunately, I couldn't understand about your situation from your reply comment. So can you post it by including more information as new question? By this, users including me can think of your situation. I would like to correctly understand about it. I apologize that I couldn't resolve your new question soon. If you can cooperate to resolve your issue, I'm glad. – Tanaike May 27 '19 at 08:24
  • Make 2 google accounts. Log in with both of the accounts. Now you have account #1, account #2. If you create the download link using account #2, and try to download, it will say "We're sorry, but you do not have access to this page." because it only looks at account #1. – Dave May 27 '19 at 08:29
  • @Dave About [your new question](https://stackoverflow.com/questions/56297366/download-folder-as-zip-google-drive-api/56312048?noredirect=1#comment99252075_56312048), I had commented about that at [my 1st comment](https://stackoverflow.com/questions/56297366/download-folder-as-zip-google-drive-api/56312048?noredirect=1#comment99252371_56312048). But from your reply comment, I thought that it was not useful for your situation. So I couldn't understand about your goal, and I proposed to post it as new question. The reason I couldn't resolve your new question is due to my poor skill. I'm so sorry. – Tanaike May 27 '19 at 08:37