OK so i have developed an app with appmaker which can move files from personal drives to team drives and vice versa. This is less trivial than it sounds since the move/copy process is different with personal and team drives. It works fine except for files in google photos which are shown in drive.
through the app we get FileId and FolderId. I test where the current file and target folder is with file.teamDriveId
which is false-y on personal drive files/folders and then direct the code to the correct function to actually do the move see function moveFileToFolder
.
problem:
google photos drives test false for .teamDriveId
(ie. in personal drive which is where they are shown in the regular google interface) but when handled by the function D2T
the code breaks and gives an error that it is a Team Drive file Exception: Cannot use this operation on a Team Drive item
. however, the file doesnt have a teamDriveId
. checkmate!
one idea i had was to obtain an teamDriveId for the google photos and add to the file inside google photos then copy but cant seem to get that to work. If anyone has an idea, would be grateful.. thanks.
/*
moveFiletoFolder first redirects to the correct function
Drive to Drive : D2D
Team to Team: T2T
Drive To team: D2T
team to drive: T2D
*/
function moveT2T(fileId, newFolderId, Description){
var file = Drive.Files.get(fileId, {supportsTeamDrives: true});
var folder=Drive.Files.get(newFolderId, {supportsTeamDrives: true});
console.log(file.title +' moveT2T ' + folder.title);
//first PATCH the file - moves it to the new folder
Drive.Files.patch(file, fileId, {
removeParents: file.parents.map(function(f) { return f.id; }),
addParents: [newFolderId], //problem line for non-team drives
supportsTeamDrives: true
});
return (file.title + " moved to " + folder.title);
}
function moveD2D(sourceFileId, targetFolderId, Description) {
var file = DriveApp.getFileById(sourceFileId);
var targetfolder= DriveApp.getFolderById(targetFolderId);
console.log(file.getName() +' moveD2D ' + targetfolder.getName());
file.getParents().next().removeFile(file);
DriveApp.getFolderById(targetFolderId).addFile(file);
return (file.getName() + " moved to " + targetfolder.title);
}
function moveD2T(sourceFileId, targetFolderId, Description) {
var file = DriveApp.getFileById(sourceFileId);
var folder=Drive.Files.get(targetFolderId, {supportsTeamDrives: true});
console.log(file.getName() +' moveD2T ' + folder.title);
//file.getParents().next().removeFile(file); // this line was removed -
not needed when moving from personal to team drive!
DriveApp.getFolderById(targetFolderId).addFile(file);
return (file.getName() + " moved to " + folder.title);
}
function moveP2T(fileId, newFolderId, Description) {
/* doesnt work!
var file = Drive.Files.get(fileId, {supportsTeamDrives: true});
var folder=Drive.Files.get(newFolderId, {supportsTeamDrives: true});
newId=Drive.Files.generateIds().ids[0];
Drive.Files.insert(file, fileId,{
"supportsTeamDrives": true
});
\\ this crashes but is the correct code for moving from personal to team drive:
DriveApp.getFolderById(targetFolderId).addFile(file);
Drive.Files.patch(file, fileId, {
removeParents: file.parents.map(function(f) { return f.id; }),
addParents: [newFolderId], //problem line for non-team drives
supportsTeamDrives: true
});
*/
}
function moveFileToFolder(fileId, newFolderId, Description) {
var file = Drive.Files.get(fileId, {supportsTeamDrives: true});
var folder=Drive.Files.get(newFolderId, {supportsTeamDrives: true});
if (file.spaces.indexOf('photos')!= -1){
//console.log(file.teamDriveId);
// moveD2T(fileId, newFolderId, Description); //Exception: Cannot use this operation on a Team Drive item. line 95 DriveApp.getFolderById(targetFolderId).addFile(file);
moveP2T(fileId, newFolderId, Description);
return (file.title + " moved to " + folder.title) ;
}
if (file.teamDriveId ){
// file is in a Team Drive , it can be copied to Team
Folder or Drive Folder with moveT2T
// i.e. moveT2T = moveT2D
moveT2T(fileId, newFolderId, Description);
return (file.title + " moved to " + folder.title) ;
}
if (!file.teamDriveId && !folder.teamDriveId){
//file is being copied between Drive folders only
moveD2D(fileId, newFolderId, Description);
return (file.title + " moved to " + folder.title) ;
}
if (!file.teamDriveId && folder.teamDriveId){
//file is currently in Drive, needs to move to Team Drive folder
moveD2T(fileId, newFolderId, Description);
return (file.title + " moved to " + folder.title);
}
}