MailApp script and Team Drives

75 views
Skip to first unread message

Dawn

unread,
May 29, 2019, 9:41:29 PM5/29/19
to Google Apps Script Community
Hello All, Can you point me to documentation and/or tutorials for accessing images in a Team Drive folder by fileID to include in an email from Google Sheets via a custom function in Google Apps script? 

Andrea

unread,
May 31, 2019, 3:36:22 AM5/31/19
to google-apps-sc...@googlegroups.com
Ciao,
I'm very inexperienced, I'm sorry.

I hope this code can help you as a starting point


    var myFile = GetFiles( '0AHuBg3lY8BPOUk9PuE', 'MyFile.txt');
  
    GmailApp.sendEmail('reci...@mydmain.com', 'Subject', 'Body', {attachments:myFile}); 



function GetFiles(teamDriveId,FileName){
  
  var query = 'trashed = false and ' + //to exclude trashed files
      'not mimeType = "application/vnd.google-apps.folder"'; // To exclude folders
  var files, pageToken;
  do {
    files = Drive.Files.list({
      q: query,
      maxResults: 100,
      pageToken: pageToken,
      corpora: 'teamDrive',
      supportsTeamDrives: true,
      teamDriveId: teamDriveId,
      includeTeamDriveItems: true
    });
    
    if (files.items && files.items.length > 0) {
        for (var i = 0; i < files.items.length; i++) {
        var file = files.items[i];
        if (file.title == FileName){
          var myFile = DriveApp.getFileById(files.items[i].id)
          return myFile
};
        
      };
    } else {
      Logger.log('No files found.');
    }
    pageToken = files.nextPageToken;
  } while (pageToken);
};

Dawn Siemonsma

unread,
May 31, 2019, 2:36:31 PM5/31/19
to google-apps-sc...@googlegroups.com
Thank you for the example! 

On Fri, May 31, 2019, 2:36 AM Andrea <andrea....@consultant.solgroup.com> wrote:
Ciao,
I try to help you, but consider that I am very very inexperienced

I hope this code can help you as a starting point

      var ListFile = Drive.Files.list({
      q: 'trashed = false and not mimeType = "application/vnd.google-apps.folder"',
      maxResults: 1,
      corpora: 'teamDrive',
      supportsTeamDrives: true,
      teamDriveId: '1AlHQhWfln-keUk9PcA',
      includeTeamDriveItems: true
    });
    
    var file = DriveApp.getFileById(ListFile.items[0].id)
    
    GmailApp.sendEmail(Recipient, Subject, Body, {attachments:file}); 


Best regards
Andrea


On Thursday, 30 May 2019 03:41:29 UTC+2, Dawn wrote:
Hello All, Can you point me to documentation and/or tutorials for accessing images in a Team Drive folder by fileID to include in an email from Google Sheets via a custom function in Google Apps script? 


IMPORTANT:
This is a business and not a personal e-mail, this message and its attachments are confidential and may also be legally privileged.
If you are not the intended recipient, or have received this e-mail in error, please notify immediately the sender and delete this message and all its attachments.

Any unauthorized review, copying, disclosure, dissemination, or distribution of this message and/or its attachments is strictly forbidden.



--
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/T6jTcNwGnEA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-script-community.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/175aa9d8-3cae-40df-bcfe-b64b3535ea92%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Steve Webster

unread,
Jun 1, 2019, 10:08:58 AM6/1/19
to google-apps-sc...@googlegroups.com
Just want to share that "team drives" is now known as "shared drive". Please take a moment to explore this Drive API web page to learn more:

Here is a snippet from that web page that relates to the code previous provided by Andrea.

  • driveId — ID of shared drive to search.
  • includeItemsFromAllDrives — Whether shared drive items should be included in results. If not present or set to false, then shared drive items are not returned. Note: On June 1, 2020, this parameter will be ignored and shared drive items will be returned in results.
  • corpora — Bodies of items (files/documents) to which the query applies. Supported bodies are defaultdomaindrive, and allDrives. Prefer default or drive to allDrives for efficiency.
  • supportsAllDrives — Whether the requesting application supports both My Drives and shared drives. If false, then shared drive items are not included in the response. Note: On June 1, 2020, this parameter will be ignored and all applications are assumed to support shared drives

In other words, you could use the below:

var ListFile = Drive.Files.list({
      q: 'trashed = false and not mimeType = "application/vnd.google-apps.folder"',
      maxResults: 1,
      corpora: 'allDrives',
      supportsAllDrives: true,
      driveId: '1AlHQhWfln-keUk9PcA',
      includeItemsFromAllDrives: true
    });

Kind Regards,

Steve Webster
SW gApps, President 
Google Product Expert in: Google Apps Script, Drive, and Docs 
Google Vendor (2012-2013) || Google Apps Developer Blog Guest Blogger 
Add-ons: Text gBlaster and Remove Blank Rows


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.
Reply all
Reply to author
Forward
0 new messages