Implement a folder move function in Google Dirve

2019-02-21 05:56发布

I am very new to writing any types of scripts and am trying to write a Google Apps Script to search through folders for a particular string, and if found move the folder to another folder.

I can copy a folder, but not move it since the copied folder has the same ID as the original, and deleting one deletes the other. From what I have been able to figure out I can implement this by deleting the right child from the folder. I've tried this in the APIs explorer and it does what I expect.

The problem is that I can't for the life of me figure out how to implement the example given in the Google Drive SDK page in script.google.com. If I paste in the example code I get a Missing name after . operator error. None of the scripting commands let me delete a child.

Likely this is a simple issue and arises from my trying to learn some JavaScript by doing this project, but I would appreciate any help or advice on how to implement a move function in Google Script.

The specific question is: How do I implement a move folder script? This is what I am trying, but it removes both folders:

while (contents.hasNext()) {
  folder = contents.next();
  name = folder.getName();
  if (name.search("XYZ") != -1) {
    moveFolder = folder;
    targetFolder.addFolder(moveFolder) moveFolder.setName('DEL_' +
      moveFolder.getName());
    removeFolder(moveFolder);
  }
}

1条回答
男人必须洒脱
2楼-- · 2019-02-21 06:07

First, let's clear up some confusion. Two Drive-related "Services" are provided for use within Google Apps Script, Drive Services, and Docslist Services. The documentation you've referenced is for the Google Drive SDK, which is not the same thing. If you're programming in Google Apps Script, ignore the SDK documentation.

Next, let's talk about what a folder or file is on Drive, and how that affects the concepts of "copy", "move" and "delete". A file or folder may have a folder as a parent. In fact, it may have multiple parents. If we want to have the same file or folder "copied" to another folder, we just add it as a child to the target folder. At this point, it may look like we have two items with the same ID - but what we actually have is one item with two parents. If we then remove the file or folder from the original folder (disown it, in a way), it will be left with one parent, and this will appear like a "move". The file or folder will not have been "deleted". On the other hand, if instead of removing the parent / child relationship, we DO delete the file or folder, it will appear that we have "deleted both folders", as you have described.

So, your "move" script needs to add a new parent / child relationship, and remove the other.

The Drive starter script that is presented when you open a new script in the editor contains a function called moveFileToFolder().

Screenshot

This function does what you are looking for, except that the thing it's moving is a file. However, the methods that are used for a file have cousins for folders, so you can just do some replacement to end up with moveFolderToFolder():

/**
 * This script moves a specific folder into a given folder, and removes the folder
 * from all other folders that previously contained it. For more information on
 * interacting with files, see
 * https://developers.google.com/apps-script/drive/file
 */
function moveFolderToFolder(sourceFolderId, targetFolderId) {
  var targetFolder = DriveApp.getFolderById(targetFolderId);
  var sourceFolder = DriveApp.getFolderById(sourceFolderId);
  var currentFolders = sourceFolder.getParents();
  while (currentFolders.hasNext()) {
    var currentFolder = currentFolders.next();
    currentFolder.removeFolder(sourceFolder);
  }
  targetFolder.addFolder(sourceFolder);
};

If you want to deal with folder Names, then you need to use getFoldersByName() to collect all matching folders, then iterate using next() into the list. Here's how that function could be modified to use folder names. To simplify things, it assumes that there is just ONE folder with the given name, and throws an exception if that's not the case. Once we have a handle on the folders we're interested in, the balance of the function remains the same as the previous example.

function moveNamedFolderToFolder(sourceFolderName, targetFolderName) {
  var matchedFolders = DriveApp.getFoldersByName(sourceFolderName);
  if (matchedFolders.hasNext()) {
    var sourceFolder = matchedFolders.next();
    if (matchedFolders.hasNext()) throw new Error( "Source Folder Name not unique" );
  }
  matchedFolders= DriveApp.getFoldersByName(targetFolderName);
  if (matchedFolders.hasNext()) {
    var targetFolder = matchedFolders.next();
    if (matchedFolders.hasNext()) throw new Error( "Target Folder Name not unique" );
  }

  var currentFolders = sourceFolder.getParents();
  while (currentFolders.hasNext()) {
    var currentFolder = currentFolders.next();
    currentFolder.removeFolder(sourceFolder);
  }
  targetFolder.addFolder(sourceFolder);
};
查看更多
登录 后发表回答