Moving Files In Google Drive Using Google Script

前端 未结 8 1351
终归单人心
终归单人心 2020-12-03 05:15

I\'m trying to create documents using information posted through Google forms, then once the document is created I would like to move the document into a shared folder for p

相关标签:
8条回答
  • 2020-12-03 06:04

    A bit safer approach compared to the previous ones:

    1. If you remove link to the file first, then you will not be able to addFile.

    2. If file is already located in the target folder, then the approach provided by Amit (https://stackoverflow.com/a/38810986/11912486) only removes file.

    So, I suggest to use the following approach:

    function move_file(file_id, target_folder_id) {
      var source_file = DriveApp.getFileById(file_id);
      var source_folder = source_file.getParents().next();
      if (source_folder.getId() != target_folder_id) {
        DriveApp.getFolderById(target_folder_id).addFile(source_file);
        source_folder.removeFile(source_file);
      }
    }
    

    can be improved by:

    • javascript camel style

    • multiple locations validation

    0 讨论(0)
  • 2020-12-03 06:05

    This question has been answered, but here is a slightly different configuration:

    function moveFile(parameterObject) {
      var currentFolderID,file,fileToMoveID,sourceFolder,targetFolder,targetFolderID;
    
      fileToMoveID = parameterObject.fileToMoveID;
      currentFolderID = parameterObject.currentFolderID;
      targetFolderID = parameterObject.targetFolderID;
    
      file = DriveApp.getFileById(fileToMoveID);//Get the file to move
    
      if (!file) {
        functionToHandleThisKindOfThing("there is no file");
        return;
      }
    
      if (currentFolderID) {//The folder ID holding the current file was passed in
        sourceFolder = DriveApp.getFolderById(currentFolderID);
      } else {//No ID for the current folder
        sourceFolder = file.getParents();
        if (sourceFolder) {
          if (sourceFolder.hasNext()) {
            sourceFolder = sourceFolder.next();
          }
        }
      }
    
      targetFolder = DriveApp.getFolderById(targetFolderID);
    
      targetFolder.addFile(file);
      sourceFolder.removeFile(file);
    }
    
    function testCode() {
      var o;
    
      o = {
        'fileToMoveID':"File ID of file to Move",
        "targetFolderID":"ID of folder to Move to"
      }
    
      moveFile(o);
    
    }
    
    0 讨论(0)
  • 2020-12-03 06:06

    Try this:

    var file = DriveApp.getFileById(newDoc.getId());
    targetFolder.addFile(file);
    //DriveApp.getFolderById('root').removeFile(file); // remove from root
    
    0 讨论(0)
  • 2020-12-03 06:08

    There is no direct method in the File or Folder Classes to move files from one folder in Google Drive to another. As you mentioned you can copy the file to another folder with the method makeCopy() and then delete it with setTrashed(), the code should look like this:

      var targetFolder = DriveApp.getFolderById(TARGET_FOLDER_ID);
      var newDoc = DocumentApp.create(requestID + " - " + requestSummary); // Creates the Document in the user's Drive root folder
    
      // Modify the new document here, example:
      // var body = newDoc.getBody();
      // body.appendParagraph("A paragraph.");
      // newDoc.saveAndClose();
    
      var driveFile = DriveApp.getFileById(newDoc.getId()); // Gets the drive File
    
      driveFile.makeCopy(newDoc.getName(), targetFolder); // Create a copy of the newDoc in the shared folder
      driveFile.setTrashed(true);  //  sets the file in the trash of the user's Drive
    

    EDIT:

    In a second thought and taking into account Ruben's comments. I agree that it is a better practice to implement Amit's answer.

    0 讨论(0)
  • 2020-12-03 06:11

    This is my first post! I know this has been answered a few times, but I actually came across this question while working on my project, and while reviewing the Apps Script documentation, I figured out a concise way to do it. A variation of some1's answer.

    var file = DriveApp.getFileById(fileid);
    DriveApp.getFolderById(folderid).addFile(file);
    DriveApp.getRootFolder().removeFile(file);
    

    Hope it helps!

    0 讨论(0)
  • 2020-12-03 06:12

    If we make a copy of the file and trash the original, it would change the file URL and also the file sharing settings won't be preserved.

    In Drive, it is possible to add a file to multiple folders with the .addFolder() method of DriveApp service. You can add the file to the target folder and then remove the file from the immediate parent folder.

    function moveFiles(sourceFileId, targetFolderId) {
      var file = DriveApp.getFileById(sourceFileId);
      var folder = DriveApp.getFolderById(targetFolderId);
      file.moveTo(folder);
    }
    
    0 讨论(0)
提交回复
热议问题