Share Drive Ownership

215 views
Skip to first unread message

Clovis Tristão

unread,
Feb 25, 2024, 3:37:06 PM2/25/24
to Google Apps Script Community
Hello, 

How do I see the owner of a shared drive using Google Script? 
I'm trying using the code below, but it presents little information. 

const folders = Drive.Drives.list(); 
console.log('Email: ' + folders.getEmail()

 Is there a way to resolve this issue? 
Thank you very much in advance, 

Clovis

DME

unread,
Mar 14, 2024, 4:32:46 AM3/14/24
to google-apps-sc...@googlegroups.com

To retrieve the owner of a shared drive using Google Apps Script, you can use the DriveApp service along with the getOwner() method. Here's how you can modify your code to achieve this:

javascript
function getSharedDriveOwner() { const folders = DriveApp.getFolders(); while (folders.hasNext()) { const folder = folders.next(); if (folder.isShared()) { const owner = folder.getOwner(); Logger.log('Owner: ' + owner.getEmail()); } } }

In this code:

  • We iterate through all the folders in your Google Drive using DriveApp.getFolders().
  • We check if each folder is shared using folder.isShared().
  • If a folder is shared, we retrieve the owner's email using folder.getOwner().getEmail().
  • Finally, we log the owner's email to the Apps Script log using Logger.log().

Make sure to run this function from the Apps Script editor (usually found in Google Sheets, Docs, or Slides by going to Extensions > Apps Script), and then check the execution log to see the owner's email address printed there.

Please note that this code will only work for folders (shared drives) and not for individual files. If you need to retrieve the owner of a specific file, you can use DriveApp.getFileById(fileId) to get the file object and then use file.getOwner().getEmail() to get the owner's email.


--
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/4630c02d-0113-45ce-bee8-0462086130cdn%40googlegroups.com.


--
Reply all
Reply to author
Forward
0 new messages