Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I download file from URL link generated by google apps script

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 ?

like image 895
mult_ru Avatar asked Jan 28 '13 23:01

mult_ru


1 Answers

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.

like image 89
Mogsdad Avatar answered Sep 18 '22 14:09

Mogsdad