Skip to content

Instantly share code, notes, and snippets.

@suntong
Last active December 31, 2015 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suntong/7955642 to your computer and use it in GitHub Desktop.
Save suntong/7955642 to your computer and use it in GitHub Desktop.
move a Google Doc into another folder
/*
How do I move a file to a folder in Google Apps Script?
http://stackoverflow.com/questions/14696514/how-do-i-copy-move-a-file-to-a-folder-in-google-apps-script
Yeah, it is a bit odd that Google does not provide a move method.
But, considering how drive works and that a file can belong to
multiple folders, it makes some sense that you have write your own moves.
I should note that this simple function is exactly that ... simple.
It assumes you are okay with whacking all parent folders -- owned
or shared. This can have unexpected consequences with shared
files in various shared folders of various users if you are not
careful. It does not appear, however, to remove parent folders
that are not shared with the user of this script -- which is
good. Anyhow, one obvious alternative to control things better
would be to specify the "from" folder as well as the "to" folder.
-- juanitogan
*/
// Moves a file to a new folder by removing all current
// parent folders and then adding the new one.
function moveFileTo(fileObj, folderObj) {
// Attempt the move only if folderObj has a value.
// Otherwise, the file will be left without a folder.
// (Lost files can be found in the "All items" area.)
if (folderObj) {
var folders = fileObj.getParents();
for (var i = 0; i < folders.length; i++) {
fileObj.removeFromFolder(folders[i]);
}
fileObj.addToFolder(folderObj);
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment