Migrate files from external collaborator

34 views
Skip to first unread message

Zsolt Ero

unread,
Oct 22, 2024, 5:21:01 PMOct 22
to Google Apps Script Community
Hi,

I'm a Workspace admin on my own solo company account. I worked with an external collaborator in Google Drive. She had to leave, and some of the files in the shared Google Drive folder were owned by her. She used her personal Gmail account to create the files.

I'd like to migrate all those files to my account, but it seems I cannot do it. The funny thing is that I can click "Make a copy" on  the files, but somehow I cannot set the ownership on existing files? In the UI it's not possible and in Google Scripts, I get "Exception: Invalid argument".

function transferOwnershipToMe(fileId) {
var file = DriveApp.getFileById(fileId);
var me = Session.getEffectiveUser();
file.setOwner(me);
}

Do you think I'm making some error in the script, or I just get this cryptic message for saying not enough permissions. What do you recommend me to do now? I can clone the files and delete the original, do you think I should do that? Basically recursively copy a folder and clone every item into a new one, this time owned by me?

I can also ask the collaborator to please change ownership, but I really want to figure this out. I don't want to depend on the goodwill of past collaborators in the future to solve this on my own domain.

Thanks and regards,
Zsolt

Andrew Roberts

unread,
Oct 22, 2024, 5:28:06 PMOct 22
to google-apps-sc...@googlegroups.com
The collaborator won't be able to transfer ownership from a consumer account to a workspace one. I would just make copies and delete the originals. That would be a simple enough script.

This is what Copilot came up with, just make sure you run it from the account you want to own the files and folders:

function cloneFolder(srcFolderId, destFolderId) {
  const srcFolder = DriveApp.getFolderById(srcFolderId);
  const destFolder = DriveApp.getFolderById(destFolderId);

  function copyFilesAndFolders(source, destination) {
    // Clone files
    const files = source.getFiles();
    while (files.hasNext()) {
      const file = files.next();
      file.makeCopy(file.getName(), destination);
    }

    // Clone folders
    const folders = source.getFolders();
    while (folders.hasNext()) {
      const folder = folders.next();
      const newFolder = destination.createFolder(folder.getName());
      copyFilesAndFolders(folder, newFolder);
    }
  }

  copyFilesAndFolders(srcFolder, destFolder);
}


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/f259d4c8-74c8-40c8-ad7a-724e6492043bn%40googlegroups.com.

Zsolt Ero

unread,
Oct 23, 2024, 7:07:42 AMOct 23
to google-apps-sc...@googlegroups.com
Thanks, I arrived at the same conclusion. Worked perfectly.

You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/jha_L8S8iwU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CAN1QPJTQ%3DAy%3DJWV%2BM0mm2tbJraDcMPCDUiGkCRiMNA3G58WS9A%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages