Quiz and Exercises Google Apps Script GmailApp Class

๐Ÿš€ Exciting Journey into Google Apps Script – GmailApp Class! ๐Ÿ“งโœจ

๐Ÿ” Exploring the dynamic capabilities of the GmailApp class in Google Apps Script! ๐ŸŒ Whether it’s sending personalized emails, managing drafts, or navigating through your inbox, the possibilities are endless.

๐Ÿ’ก Key Takeaways:

  • Seamlessly interact with Gmail services.
  • Automate email workflows with ease.
  • Explore features like creating drafts, marking threads, and more.

Introduction to Google Apps Script – Gmail Service

Google Apps Script is a scripting language developed by Google to extend the capabilities of various Google Workspace applications, including Gmail. The Gmail service in Google Apps Script provides a way to automate tasks related to sending and receiving emails through your Gmail account.

GmailApp Class (Historical)

In the past, the GmailApp class was used for interacting with Gmail. It provided methods to compose and send emails, search for emails, and manage drafts. Here’s a brief overview:

Sending Emails:

  1. createDraft(recipient, subject, body): Creates a draft email with the given recipient, subject, and body.
  2. sendEmail(recipient, subject, body): Sends an email immediately.

Accessing Emails:

  1. getInboxThreads(): Retrieves a collection of threads in the inbox.
  2. getThreadById(threadId): Retrieves a specific thread by its ID.
  3. getInboxThreads(start, max): Retrieves a range of threads from the inbox.

Modifying Emails:

  1. markThreadRead(thread): Marks a thread as read.
  2. markThreadUnread(thread): Marks a thread as unread.
  3. moveThreadToArchive(thread): Moves a thread to the archive.

Working with Drafts:

  1. getDrafts(): Retrieves a collection of draft messages.
  2. getDraftById(draftId): Retrieves a specific draft by its ID.

Example:

function sendEmailExample() {

  var recipient = ‘example@email.com’;

  var subject = ‘Hello from Google Apps Script’;

  var body = ‘This is a test email sent using Google Apps Script!’;

  GmailApp.sendEmail(recipient, subject, body);

}

Coding Exercises: Gmail Automation with Google Apps Script

Exercise 1: Send Basic Email

Task: Write a Google Apps Script function to send a simple email.

Steps:

  1. Open Google Apps Script.
  2. Write a function to send an email.
  3. Run the function.

Code Example:

function sendBasicEmail() {

  GmailApp.sendEmail(‘recipient@example.com’, ‘Subject’, ‘Body’);

}

Exercise 2: Send Email with HTML Body

Task: Modify the previous script to send an email with an HTML body.

Steps:

  1. Update the function to use HTML content.
  2. Run the function.

Code Example:

function sendHtmlEmail() {

  var htmlBody = ‘<p>This is an <b>HTML</b> email.</p>’;

  GmailApp.sendEmail(‘recipient@example.com’, ‘HTML Email’, ”, { htmlBody: htmlBody });

}

Exercise 3: Create Draft Email

Task: Write a script to create a draft email.

Steps:

  1. Write a function to create a draft.
  2. Run the function.

Code Example:

function createDraftEmail() {

  var draft = GmailApp.createDraft(‘recipient@example.com’, ‘Draft Subject’, ‘Draft Body’);

  Logger.log(‘Draft URL: ‘ + draft.getMessage().getId());

}

Exercise 4: Access Inbox Threads

Task: Retrieve and log the first few threads from the inbox.

Steps:

  1. Write a function to get inbox threads.
  2. Log thread details.
  3. Run the function.

Code Example:

function accessInboxThreads() {

  var threads = GmailApp.getInboxThreads(0, 5);

  threads.forEach(function (thread) {

    Logger.log(‘Thread Subject: ‘ + thread.getFirstMessageSubject());

  });

}

Exercise 5: Mark Thread as Read

Task: Write a script to mark the first thread in the inbox as read.

Steps:

  1. Write a function to mark a thread as read.
  2. Run the function.

Code Example:

function markThreadAsRead() {

  var thread = GmailApp.getInboxThreads(0, 1)[0];

  thread.markRead();

}

Exercise 6: Move Thread to Archive

Task: Move the first thread in the inbox to the archive.

Steps:

  1. Write a function to move a thread to the archive.
  2. Run the function.

Code Example:

function moveThreadToArchive() {

  var thread = GmailApp.getInboxThreads(0, 1)[0];

  thread.moveToArchive();

}

Exercise 7: Access Drafts

Task: Retrieve and log the details of the first few drafts.

Steps:

  1. Write a function to get drafts.
  2. Log draft details.
  3. Run the function.

Code Example:

function accessDrafts() {

  var drafts = GmailApp.getDrafts();

  drafts.forEach(function (draft) {

    Logger.log(‘Draft Subject: ‘ + draft.getMessage().getSubject());

  });

}

Exercise 8: Access Draft by ID

Task: Retrieve and log details of a specific draft by its ID.

Steps:

  1. Write a function to get a draft by ID.
  2. Log draft details.
  3. Run the function.

Code Example:

function accessDraftById() {

  var draftId = ‘1234567890abcdef’; // Replace with an actual draft ID

  var draft = GmailApp.getDraftById(draftId);

  Logger.log(‘Draft Subject: ‘ + draft.getMessage().getSubject());

}

Exercise 9: Mark Draft as Unread

Task: Mark the first draft as unread.

Steps:

  1. Write a function to mark a draft as unread.
  2. Run the function.

Code Example:

function markDraftAsUnread() {

  var draft = GmailApp.getDrafts()[0];

  draft.markUnread();

}

Exercise 10: Send Email with Attachment

Task: Modify the email sending function to include an attachment.

Steps:

  1. Update the email sending function to include an attachment.
  2. Run the function.

Code Example:

function sendEmailWithAttachment() {

  var file = DriveApp.getFileById(‘abcdef1234567890’); // Replace with an actual file ID

  GmailApp.sendEmail(‘recipient@example.com’, ‘Email with Attachment’, ‘Body’, { attachments: [file] });

}

Note: Replace placeholders like ‘recipient@example.com’, ‘abcdef1234567890’, etc., with actual values.

Quiz Questions on GmailApp Class

  1. Question: What is the primary purpose of the GmailApp class in Google Apps Script?
    1. A) Creating spreadsheets
    2. B) Managing Gmail accounts
    3. C) Interacting with Gmail services
  2. Question: How can you send an email using the GmailApp class?
    1. A) sendMail()
    2. B) createEmail()
    3. C) sendEmail()
  3. Question: What is the purpose of the createDraft() method?
    1. A) Sends an email immediately
    2. B) Creates a draft email
    3. C) Deletes a draft email
  4. Question: How do you retrieve all drafts using the GmailApp class?
    1. A) getDrafts()
    2. B) retrieveDrafts()
    3. C) listDrafts()
  5. Question: What method is used to access threads in the inbox?
    1. A) getThreads()
    2. B) inboxThreads()
    3. C) getInboxThreads()
  6. Question: How can you mark a thread as read?
    1. A) markAsRead()
    2. B) markThreadRead()
    3. C) readThread()
  7. Question: What does moveToArchive() do in the GmailApp class?
    1. A) Deletes a thread
    2. B) Moves a thread to the archive
    3. C) Archives a thread
  8. Question: Which method is used to mark a draft as unread?
    1. A) markAsUnread()
    2. B) markDraftUnread()
    3. C) draftUnread()
  9. Question: How do you retrieve the subject of a draft?
    1. A) draft.getSubject()
    2. B) getDraftSubject()
    3. C) draft.subject()
  10. Question: What is the purpose of the createDraft() method’s return value?
    1. A) The draft’s ID
    2. B) The draft’s body
    3. C) The draft’s subject
  11. Question: How do you send an email with an HTML body?
    1. A) sendHtmlEmail()
    2. B) sendEmailWithHtmlBody()
    3. C) sendEmail() with HTML options
  12. Question: What is the purpose of getThreadById(threadId)?
    1. A) Retrieves a specific thread by ID
    2. B) Retrieves all threads in the inbox
    3. C) Retrieves the last thread
  13. Question: Which method retrieves the first message subject of a thread?
    1. A) thread.getFirstMessageSubject()
    2. B) getThreadSubject()
    3. C) firstMessageSubject()
  14. Question: How do you mark a draft as read using its ID?
    1. A) markDraftRead(draftId)
    2. B) getDraftById(draftId).markRead()
    3. C) readDraft(draftId)
  15. Question: What method is used to get the recipients of an email?
    1. A) getEmailRecipients()
    2. B) getRecipients()
    3. C) getToRecipients()
  16. Question: How do you access the body of the last received email?
    1. A) getLastEmailBody()
    2. B) getLatestEmailBody()
    3. C) GmailApp.getInboxThreads()[0].getMessages()[0].getBody()
  17. Question: What is the purpose of markThreadUnread(thread)?
    1. A) Marks the thread as unread
    2. B) Retrieves unread threads
    3. C) Unmarks the thread as read
  18. Question: Which method is used to send an email with attachments?
    1. A) sendEmailWithAttachments()
    2. B) sendEmail() with attachments option
    3. C) createEmailWithAttachments()
  19. Question: How do you retrieve the subject of the last draft?
    1. A) getLastDraftSubject()
    2. B) getLatestDraftSubject()
    3. C) GmailApp.getDrafts()[0].getSubject()
  20. Question: What is the purpose of getInboxThreads(start, max)?
    1. A) Retrieves threads starting from a specific position
    2. B) Retrieves all threads in the inbox
    3. C) Retrieves the last thread
  21. Question: How can you delete a draft using its ID?
    1. A) deleteDraftById(draftId)
    2. B) getDraftById(draftId).delete()
    3. C) delete(draftId)
  22. Question: Which method is used to mark a thread as unread?
    1. A) thread.markUnread()
    2. B) markThreadUnread(thread)
    3. C) unreadThread(thread)
  23. Question: What is the purpose of markDraftUnread()?
    1. A) Marks the draft as unread
    2. B) Retrieves unread drafts
    3. C) Unmarks the draft as read
  24. Question: How do you get the attachments of a specific email?
    1. A) getEmailAttachments(email)
    2. B) getAttachments(email)
    3. C) email.getAttachments()
  25. Question: What method is used to access the sender of an email?
    1. A) getSender()
    2. B) getEmailSender()
    3. C) getFrom()
  26. Question: How do you move the last received email to the trash?
    1. A) moveToTrash(getLastEmail())
    2. B) GmailApp.getInboxThreads()[0].moveToTrash()
    3. C) moveToTrash(getLastEmailThread())
  27. Question: What is the purpose of getDrafts()?
    1. A) Retrieves all drafts
    2. B) Retrieves unread drafts
    3. C) Retrieves the last draft
  28. Question: How can you mark a specific draft as read?
    1. A) markDraftRead(draft)
    2. B) getDraftById(draftId).markRead()
    3. C) readDraft(draft)
  29. Question: What method is used to access the date of the last sent email?
    1. A) getLastSentEmailDate()
    2. B) getLatestSentEmailDate()
    3. C) GmailApp.getSentMessages()[0].getDate()
  30. Question: How do you retrieve the plain text body of the last received email?
    1. A) getLatestEmailPlainTextBody()
    2. B) GmailApp.getInboxThreads()[0].getMessages()[0].getPlainBody()
    3. C) getLastEmail().getPlainTextBody()

Answers to Quiz Questions:

  1. C) Interacting with Gmail services
  2. C) sendEmail()
  3. B) Creates a draft email
  4. A) getDrafts()
  5. C) getInboxThreads()
  6. B) markThreadRead()
  7. B) Moves a thread to the archive
  8. A) markAsUnread()
  9. A) draft.getSubject()
  10. A) The draft’s ID
  11. C) sendEmail() with HTML options
  12. A) Retrieves a specific thread by ID
  13. A) thread.getFirstMessageSubject()
  14. B) getDraftById(draftId).markRead()
  15. B) getRecipients()
  16. C) GmailApp.getInboxThreads()[0].getMessages()[0].getBody()
  17. A) Marks the thread as unread
  18. B) sendEmail() with attachments option
  19. C) GmailApp.getDrafts()[0].getSubject()
  20. A) Retrieves threads starting from a specific position
  21. B) getDraftById(draftId).delete()
  22. B) markThreadUnread(thread)
  23. A) Marks the draft as unread
  24. C) email.getAttachments()
  25. C) getFrom()
  26. B) GmailApp.getInboxThreads()[0].moveToTrash()
  27. A) Retrieves all drafts
  28. A) markDraftRead(draft)
  29. C) GmailApp.getSentMessages()[0].getDate()
  30. B) GmailApp.getInboxThreads()[0].getMessages()[0].getPlainBody()