Based on an URL link I need to download a file to Google Drive with a specific file name. But I need to do that only when I edit an specific column (trigger).
Doing some research in the web I found out how to download files to Google Drive using Google Apps Script. This tutorial is quite helpful in that matter.
I created a Google Sheets sample file for this question in this link. Since we will need to upload files to a folder, I created a sample one too in this link.
I adapted the tutorial code with my file and folder:
function DownloadFile (){
var fileName=""
var filesize=""
var response = UrlFetchApp.fetch("https://www.mprj.mp.br/documents/20184/1238340/Rogerio_Pacheco_Alves.pdf")
var rc = response.getResponseCode()
if(rc == 200) {
var fileBlob = response.getBlob()
var folder = DriveApp.getFolderById("1cWt9e_xT7-8N8uwDPFisQe_4YCPuyCxn")
if (folder !=null){
var file = folder.createFile(fileBlob)
filename = file.getName()
filesize = file.getSize()
}
}
var fileInfo = {"rc":rc, "filename":fileName, "filesize":filesize}
Logger.log(fileInfo)
}
I also created a trigger for the function On Edit (for some reason I couldn't set that in the sample Google Sheets, when I try it keeps running but don't conclude the task, but in my project it is set). The problem is that I don't know how to properly connect it to the code so it runs as I want.
So, downloading works fine, however I need to make the following adaptations:
- Run it only when column E ("Trigger") in the Google Sheets file is edited
- Get the URL from column A ("Link") in the same row as the triggered cell
- Get the fileName to save in Google Drive from column D ("FinalName") in the same row as the triggered cell
What would be the appropriate code adaption to include those features?