8

Please help I'm learning google-apps-script for short time. I want to download file from remote site by url generating from data that stores in my spreadsheet.

for example, i have 2 paremeters:

Cell1 = val1, val2, ... valN
Cell2 = val21, val22, ...  val2N

I split string from cell data to Arrays and than generate URL. for example: http://mysite.com/files/file.val1.val22.zip Than i need to download file from this link...

Can I do this process automaticaly ?

3 Answers 3

7

This example function will retrieve your zip file, and place it into your Google Drive in folder "StackOverflow". You can also download a more complete version from this gist.

function getFile(fileURL) {
  // see https://developers.google.com/apps-script/class_urlfetchapp
  var response = UrlFetchApp.fetch(fileURL);
  var fileBlob = response.getBlob()
  var folder = DocsList.getFolder('StackOverflow');
  var result = folder.createFile(fileBlob);
  debugger;  // Stop to observe if in debugger
}

For example:

getFile( "http://mysite.com/files/file.val1.val22.zip" );

Note that you cannot download per se, since you have no access to your PC's resources (e.g. file system) from apps-script. The file is still in "the cloud"... in this case, it's been copied from the web site it was on, into Google Drive. If you're running the Drive app, though, the file will now sync to your PC.

Sign up to request clarification or add additional context in comments.

5 Comments

Next problem, if file not exist on server for generated link, code get ERROR: UrlFetchApp.fetch(fileURL); and script finish, i have Error code: 404. How can i check this error ?
Check the more complete version I posted as a gist, follow the link in my answer. In that version I added basic HTTP response checking; the response is returned as "rc". For example, response = getFileFromURL(fileURL); if (response.rc != 200) { // handle failure }.
I see you example, but error in this string var response = UrlFetchApp.fetch(fileURL); this is my code example, link in test function is broken, and i have error in second line of getFile() function
The documentation for getResponseCode() implies that all common HTTP errors would be handled and returned, but it seems that's not the case. Revisit the gist, I've added a try ... catch block to suppress errors and insert a 404 File Not Found instead. (Feel free to upvote this answer if this is helping.)
Yes now it works fine... but i have some permissions to number of requests by Google. But it is still helpful for my needs.
1

Yes, You can.
Hope below code can solve your problem:

//Declare function
function downloadFile() {
  //Getting url,existing name and new name for image from the sheet in 
  //variable url, name and new_name respectively
  var sh = SpreadsheetApp.getActiveSheet();
  var row = sh.getLastRow();
  Logger.log(row);
  for (var i = 2; i <= row; i++) {

    var url = sh.getRange(i, 10).getValue();
    Logger.log(url);
    var name = sh.getRange(i, 13).getValue();
    var new_name = sh.getRange(i, 4).getValue();
    //Creating authentication token for downloading image, it may not be //required if image can be downloaded without login into
    var user = "***************";
    var password = "************";
    var headers = {
      "Accept": "application/xml",
      "Content-Type": "application/xml",
      "Authorization": "Basic " + Utilities.base64Encode(user + ":" + password)
    };
    //defining method to download file
    var options = {
      "method": "get",
      "headers": headers
    };
    //Getting folder name where to store downloaded image
    var folders = DriveApp.getFoldersByName('test');
    while (folders.hasNext()) {
      var folder = folders.next();
      Logger.log(folder.getName());
    }
    //Getting response on hit of url using downloading method defined //earlier storing in Blob
    var response = UrlFetchApp.fetch(url, options).getBlob();
    //Creating image in folder defined with response in blob and logging same file //in log to check, if required
    var file = folder.createFile(response);
    Logger.log(file);

    //renaming image
    var images = folder.getFiles();
    while (images.hasNext()) {
      var image = images.next();
      file.setName(new_name);
      Logger.log(image.getName());
    }
  }
}
//Hope you get it now

2 Comments

As this is a rather complex example, do you mind adding some sort of explanation?
its basically getting image link stored in a sheet and processing one by in loop, it downloading data storing in folder 'test' created in mydrive and same time it renaming the image name provided in other cell of the sheet. Also I have provided authentication method to authenticate download if required. Inserted comment line in code as well
1

It's worked for me.

function downloadFile() {
  var url = "https://raw.githubusercontent.com/hoat23/VisionArtificialAndImageProcessing/master/bin/utils_imgprocessing.py"
  Logger.log(url);
  var response = UrlFetchApp.fetch(url);
  var text = response.getContentText()
  var newFile = DriveApp.createFile('testfilegoogle.txt',text);
  debugger;  // Stop to observe if in debugger
}
//Hope you get it now

Comments

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.