Can I download the file from the URL generated by the Google Apps script

Please help me learn google-apps - script for a short time. I want to download a file from a remote site using a url that generates data stored in my spreadsheet.

For example, I have 2 parameters:

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

I split the string from the cell data into arrays and generate the URL. for example: http://mysite.com/files/file.val1.val22.zip How do I need to download a file from this link ...

Can I do this process automatically?

+4
source share
2 answers

This sample function will extract your zip file and put it in your Google Drive in the "StackOverflow" folder. You can also download a more complete version from this value .

 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 instance:

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

Please note that you cannot load by itself, since you do not have access to the resources of your computer (for example, the file system) from applications - a script. The file is still in the cloud ... in this case, it was copied from the website on which it was included into Google Drive. However, if you use the Drive application, the file will now be synchronized with your PC.

+3
source

Yes, you can. The 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()); } }} 

// I hope you get it now

0
source

All Articles