Google照片-没有teamDriveId,但API认为它是团队驱动文件

时间:2018-10-24 05:03:43

标签: google-app-maker google-drive-team-drive google-photos-api

好的,所以我与appmaker开发了一个应用程序,可以将文件从个人驱动器移动到团队驱动器,反之亦然。由于移动/复制过程与个人驱动和团队驱动不同,因此这听起来不那么简单。除了在驱动器中显示的Google照片中的文件之外,该文件都正常运行。

通过应用程序

我们获得FileId和FolderId。我用file.teamDriveId测试了当前文件和目标文件夹的位置,该位置在个人驱动器文件/文件夹上为false,然后将代码定向到正确的功能以实际执行移动,请参见function moveFileToFolder

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

我的一个想法是为Google照片获取teamDriveId并将其添加到Google照片中的文件中,然后进行复制,但似乎无法正常工作。如果有人有想法,将不胜感激..谢谢。

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

   }

0 个答案:

没有答案