0

好的,所以我用 appmaker 开发了一个应用程序,它可以将文件从个人驱动器移动到团队驱动器,反之亦然。这不像听起来那么简单,因为移动/复制过程与个人和团队驱动器不同。除了驱动器中显示的谷歌照片中的文件外,它工作正常。

通过应用我们得到 FileId 和 FolderId。我测试当前文件和目标文件夹file.teamDriveId在个人驱动器文件/文件夹上的位置是 false-y,然后将代码定向到正确的函数以实际执行移动查看function moveFileToFolder

问题:谷歌照片驱动器测试错误.teamDriveId即在常规谷歌界面中显示的个人驱动器中)但是当由function D2T代码处理时会中断并给出它是团队驱动器文件的错误Exception: Cannot use this operation on a Team Drive item。但是,该文件没有teamDriveId. 将死!

我的一个想法是为谷歌照片获取一个 teamDriveId 并添加到谷歌照片内的文件中,然后复制但似乎无法让它工作。如果有人有想法,将不胜感激..谢谢。

/* 
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);
        }

   }
4

0 回答 0