Moving Files In Google Drive Using Google Script

2020-01-29 07:02发布

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 people to view.

At the moment I have the script taking all of the information from the Google Forms linked spreadsheet.

Using that information I'm using the following code to create the document:

  var targetFolder = DriveApp.getFolderById(TARGET_FOLDER_ID);
  var newDoc = DocumentApp.create(requestID + " - " + requestSummary);

This is creating the document successfully in my Google Drive root folder, but I can't seem to move it where I want to move it to.

I've seen a lot of posts suggesting use stuff like targetFolder.addFile(newDoc) but that doesn't work, similarly I've seen examples like newDoc.addToFolder(targetFolder) but again this isn't working for me.

It seems that all the online questions people have already asked about this are using the previous API versions that are no longer applicable and these methods do not apply to the new DriveApp functionality.

What I would like, if possible, is to create the new document as above so that I can edit the contents using the script, then be able to move that file to a shared folder. (From what I understand there is no 'move' function at present, so making a copy and deleting the old one will suffice).

6条回答
孤傲高冷的网名
2楼-- · 2020-01-29 07:30

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);
  file.getParents().next().removeFile(file);
  DriveApp.getFolderById(targetFolderId).addFile(file);
}
查看更多
贪生不怕死
3楼-- · 2020-01-29 07:37

Try this:

var file = DriveApp.getFileById(newDoc.getId());
targetFolder.addFile(file);
//DriveApp.getFolderById('root').removeFile(file); // remove from root
查看更多
闹够了就滚
4楼-- · 2020-01-29 07:43

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!

查看更多
Evening l夕情丶
5楼-- · 2020-01-29 07:45

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.

查看更多
聊天终结者
6楼-- · 2020-01-29 07:48

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);

}
查看更多
再贱就再见
7楼-- · 2020-01-29 07:51

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

查看更多
登录 后发表回答