R_life_R
R_life_R

Reputation: 816

Download file to google drive folder using app script

I am using this script to download a URL set in a google sheet cell to a specific folder in google drive called "game_thumb" If cell B1 is "yyyyy.com/picture.png" I expect picture.png to be downloaded to the google drive folder. I get an error"ReferenceError: "DocsList" not defined. (line 5). I also would like the file to be renamed to include Cell A1 (contentCellA1_picture.png) before it is downloaded to drive. The code I use:

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('game_thumb');
  var result = folder.createFile(fileBlob);
  debugger;  // Stop to observe if in debugger
}

Upvotes: 1

Views: 2926

Answers (1)

Tanaike
Tanaike

Reputation: 201683

  • You want to save the downloaded file in the specific folder which has the name of game_thumb.
  • You want to set the value of cell "A1" to the filename of downloaded file.

If my understanding is correct, how about this modification?

For your question 1:

From your question, it is found that although I'm not sure whether the data is the file blob you want, the data from the URL can be retrieved. So in order to remove the error, please modify as follows.

From:

var folder = DocsList.getFolder('game_thumb');

To:

var folder = DriveApp.getFoldersByName('game_thumb').next();
  • In this modification, it supposes that the folder which has the name of game_thumb is only one in your Drive. If there are the folders with several same names, please tell me.

For your question 2:

Please modify as follows.

From:

var result = folder.createFile(fileBlob);

To:

var name = SpreadsheetApp.getActiveSheet().getRange("A1").getValue();
var result = folder.createFile(fileBlob).setName(name);
  • From your question, I'm not sure whether you are using the container-bound script or standalone script, and also I'm not sure where sheet there is the cell "A1" is. So this modification supposes that the cell "A1" of the active sheet is used.

References:

If I misunderstood your question and this modification didn't work, I apologize. At that time, can you provide the information of the situation?

Upvotes: 1

Related Questions