EDIT
I have logged an issue with Google's Issue Tracker about the Folder.moveTo(destination)
function not working, as it appears to be a bug. Feel free to star the issue to give it a bump!
https://issuetracker.google.com/issues/163080678
Background
I am trying to provide a "one click" way of moving folders in Google Drive via an add-on (which appears in both Drive and Gmail). For context, each "folder" in this case is a matter, and the "one click" will move the folder (containing all the relevant documents to that matter) from the "open" folder to the "closed" folder. However, I am getting confusing and unhelpful errors every way I try to achieve this.
Note: all of these folders are in a Shared Team Drive. I'm aware that this places some limitations on what the DriveApp Service and Drive API can do, but I'm crossing all my fingers and toes that it's not just an inherent limitation and that there is a workaround!
I have already read the following:
- How can I use Google Apps Script to move files between personal Google Drives and Team Drives?.
- Google Script Move Folders
- Move a Google Drive folder to a new location using GAS
- https://tanaikech.github.io/2019/11/20/moving-file-to-specific-folder-using-google-apps-script/
- https://developers.google.com/apps-script/reference/drive/folder#movetodestination
- https://developers.google.com/drive/api/v2/reference/files/update
- https://developers.google.com/drive/api/v2/reference/parents/insert
But they did not solve my problem (noting that addFile()
, addFolder()
, removeFile()
and removeFolder()
have been deprecated recently and so are no longer available as the answers above would suggest).
Expected Result
My expectation is that when the user hits the "Close" button in the add-on, it will move the relevant folder (selected at a previous stage in the add-on) from the "open" folder, to the "closed" folder (which sit next to each other at the same level in the folder hierarchy). To be clear, I'm hoping that it will move the folder, not duplicate it in the target folder and then delete the original from the original parent folder.
Actual Result
See specific error messages below, but generally speaking, when the user clicks the "Close" button, I get an error right away, and nothing happens to the folder.
Attempt 1 - Folder.moveTo(destination)
Taking the Google documentation at face-value, I first attempted to use Folder.moveTo(destination)
as below:
function moveFolder() {
var folderToMove = DriveApp.getFolderById(MOVE_FOLDER_ID);
var destinationFolder = DriveApp.getFolderById(DESTINATION_FOLDER_ID);
folderToMove.moveTo(destinationFolder);
}
However, on running this, I get the error message: Exception: Unexpected error while getting the method or property moveTo on object DriveApp.Folder.
Attempt 2 - Drive.Files.update()
Failing to fix the above, I tried the Advanced Drive feature of Google Apps Script, and attempted to use Drive.Files.update()
(documentation here). My code looks like this:
function moveFolder() {
Drive.Files.update({parents: [{id: DESTINATION_FOLDER_ID}]}, MOVE_FOLDER_ID);
}
I also tried a variation including the optional paramter supportsAllDrives
, as below:
function moveFolder() {
Drive.Files.update({supportsAllDrives: true, parents: [{id: DESTINATION_FOLDER_ID}]}, MOVE_FOLDER_ID);
}
However, both variations give the error: GoogleJsonResponseException: API call to drive.files.update failed with error: File not found: [fileId]
, where [fileId]
is the actual Google Drive ID of the folder being moved.
I have confirmed in both attempts 1 and 2, by using various other functions I know do work (like adding customer properties with Drive.Properties
and renaming the folder using DriveApp), that the folders themselves are definitely being passed through correctly and are able to be manipulated programmatically. Ie, the folder definitely exists, and using the URL in the browser, can definitely be found using that Drive ID.