Google script: Download web image and save it in a specific drive folder

半城伤御伤魂 提交于 2020-01-23 12:34:53

问题


I need to download an image with GS and save it in a specific drive folder. I'm able to save the image in the root folder but i cannot save it in a specific folder:

    function downloadFile(fileURL,folder) {  

  var fileName = "";
  var fileSize = 0;

  var response = UrlFetchApp.fetch(fileURL, {muteHttpExceptions: true});
  var rc = response.getResponseCode();

  if (rc == 200) {
    var fileBlob = response.getBlob()
    var folder = DriveApp.getFoldersByName(folder);
    if (folder != null) {
      var file = DriveApp.createFile(fileBlob);
      fileName = file.getName();
      fileSize = file.getSize();
    }
  }

  var fileInfo = { "rc":rc, "fileName":fileName, "fileSize":fileSize };
  return fileInfo;
}

Question: what have I to add to use the variable "folder"?

I found a lot of examples with "DocList" Class that is not in use anymore

Many thanks


回答1:


Are you familiar with this app? It does exactly what you're asking for.

However, if you want to re-create this for your own purposes, I would change your declaration of variable file to read as such:

var file = folder.next().createFile(fileBlob);

when you create your variable folder, the method you use creates a FolderIterator, not a single folder. You have to call the next() method to get a Folder object.

To be precise with your script and avoid saving to an incorrect-but-similarly-named folder, I would recommend passing the folder ID to your script rather than the folder Name. If you pass the folder ID, you could declare folder as:

var folder = DriveApp.getFolderById(folder);

and then continue the script as you have it written. I hope that helps.




回答2:


Well, I guess GAS has make a lot of progress on developing its API, the function createFile(blob) of an object Folder will do the job: https://developers.google.com/apps-script/reference/drive/folder#createfileblob

// Create an image file in Google Drive using the Maps service.
var blob = Maps.newStaticMap().setCenter('76 9th Avenue, New York NY').getBlob();
DriveApp.getRootFolder().createFile(blob);

It's quite late for the answer but just incase some one runs into the situation.




回答3:


Working on similar problem, I came up with the solution below to save a file to a folder. If the folder doesn't exist it creates it, otherwise it saves the file specified by "FOLDER_NAME"

  var folderExists = checkFolderExists("FOLDER_NAME");

  if (folderExists) {
     saveFolder = DriveApp.getFolderById(folderExists);
  } else {
     saveFolder = DriveApp.createFolder("FOLDER_NAME");
  }

  // Make a copy of the file in the root drive.
  var file = DriveApp.getFileById(sheetID);
  // Take the copy of the file created above and move it into the folder:
  var newFile = DriveApp.getFolderById(saveFolder.getId()).addFile(file);

  // Remove the copy of the file in the root drive.
  var docfile = file.getParents().next().removeFile(file);

Further to Eric's answer, I have also provided a utility function that checks if the folder exists. It's reusable in any project.

function checkFolderExists(fName) {
  try {
    var folderId;
    var folders = DriveApp.getFolders();

    while (folders.hasNext()) {
      var folder = folders.next();
      folderName = folder.getName();
      if (folderName == fName) {
         folderId = folder.getId();
      }
    }
  } catch(e) {
    log("Services::checkFolderExists()" + e.toString());
    throw e;
  }  
  return folderId;
 }


来源:https://stackoverflow.com/questions/33647399/google-script-download-web-image-and-save-it-in-a-specific-drive-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!