Haris ur Rehman
Haris ur Rehman

Reputation: 2663

Apps Script - Gmail addon - Get selected text in Gmail message

I am developing a Gmail addon and need to get selected/highlighted text in a Gmail message via Google Apps Script. I can get whole message body via following code, but can't seem to find a way to get selected text.

function onGmailMessage(e) {

  // Get the ID of the message the user has open.
  var messageId = e.gmail.messageId;

  // Get an access token scoped to the current message and use it for GmailApp
  // calls.
  var accessToken = e.gmail.accessToken;
  GmailApp.setCurrentMessageAccessToken(accessToken);

  // Get the subject of the email.
  var message = GmailApp.getMessageById(messageId);
} 

Upvotes: 1

Views: 471

Answers (1)

ziganotschka
ziganotschka

Reputation: 26796

It is not possible to get client-side selected text of a Gmail message server-side - with the Gmail API / Apps Script

A Gmail Add-on can give you the following event objects when a trigger fires:

  • gmail.accessToken
  • gmail.bccRecipients[]
  • gmail.ccRecipients[]
  • gmail.messageId
  • gmail.threadId
  • gmail.toRecipients[]

A Gmail message resource contains the following information:

{
  "id": string,
  "threadId": string,
  "labelIds": [
    string
  ],
  "snippet": string,
  "historyId": string,
  "internalDate": string,
  "payload": {
    object (MessagePart)
  },
  "sizeEstimate": integer,
  "raw": string
}

Whereby this information is only available for saved sent / reveived messages or SAVED drafts.

Unfortunately, a Gmail Add-on (unlike a Docs Add-on) does not have any access to the text that is typed / selected client-side into the draft module.

If this feature is crucial for your application you would need to change direction and consider e.g. a Chrome extension where it might be possible to scrape client-side content with HTML / Javascript.

Upvotes: 5

Related Questions