I am trying to implement a Google Apps Script function that will help me update the subject, content, cc, bcc, etc., of an email. However, I have encountered an issue where, despite having written the code, it is not updating anything when the button is clicked. I am unsure whether the problem lies within the code itself or with the appscript.json
file that I am using.
Additionally, I have a couple of other questions:
Will this function update the currently open compose window in Gmail, and if so, will the changes be visible without the need for a reload?
Is there a specific function, action, or API that allows me to open a compose window programmatically using Google Add-ons, Gmail Add-ons, or the Gmail API?
/**
* Compose trigger function that fires when the compose UI is
* requested. Builds and returns a compose UI for inserting images.
*
* @param {event} e The compose trigger event object. Not used in
* this example.
* @return {Card[]}
*/
function startApp(e) {
return [buildComposeCard()];
}
/**
* Build a card to display interactive buttons to allow the user to
* update the subject, and To, Cc, Bcc recipients.
*
* @return {Card}
*/
function buildComposeCard() {
var card = CardService.newCardBuilder();
var cardSection = CardService.newCardSection().setHeader('Update email');
cardSection.addWidget(CardService.newTextButton().setText('Update email').setOnClickAction(CardService.newAction().setFunctionName('applyUpdateEmail')));
return card.addSection(cardSection).build();
}
function applyUpdateEmail() {
var subject = ['this is a subject'];
var toRecipients = ['johhny.appleseed@gmail.com'];
var ccRecipients = ['big.blue@montana.com'];
var bccRecipients = ['spacer@gmail.com'];
return CardService.newUpdateDraftActionResponseBuilder()
.setUpdateDraftSubjectAction(CardService.newUpdateDraftSubjectAction().addUpdateSubject(subject))
.setUpdateDraftToRecipientsAction(CardService.newUpdateDraftToRecipientsAction().addUpdateToRecipients(toRecipients))
.setUpdateDraftCcRecipientsAction(CardService.newUpdateDraftCcRecipientsAction().addUpdateCcRecipients(ccRecipients))
.setUpdateDraftBccRecipientsAction(CardService.newUpdateDraftBccRecipientsAction().addUpdateBccRecipients(bccRecipients))
.build();
}
/**
* Updates the subject field of the current email when the user clicks
* on "Update subject" in the compose UI.
*
* Note: This is not the compose action that builds a compose UI, but
* rather an action taken when the user interacts with the compose UI.
*
* @return {UpdateDraftActionResponse}
*/
function applyUpdateSubjectAction() {
// Get the new subject field of the email.
// This function is not shown in this example.
var subject = ['this is a subject'];
var response = CardService.newUpdateDraftActionResponseBuilder()
.setUpdateDraftSubjectAction(CardService.newUpdateDraftSubjectAction()
.addUpdateSubject(subject))
.build();
return response;
}
/**
* Updates the To recipients of the current email when the user clicks
* on "Update To recipients" in the compose UI.
*
* Note: This is not the compose action that builds a compose UI, but
* rather an action taken when the user interacts with the compose UI.
*
* @return {UpdateDraftActionResponse}
*/
function applyUpdateToRecipientsAction() {
// Get the new To recipients of the email.
// This function is not shown in this example.
var toRecipients = ['johhny.appleseed@gmail.com'];
var response = CardService.newUpdateDraftActionResponseBuilder()
.setUpdateDraftToRecipientsAction(CardService.newUpdateDraftToRecipientsAction()
.addUpdateToRecipients(toRecipients))
.build();
return response;
}
/**
* Updates the Cc recipients of the current email when the user clicks
* on "Update Cc recipients" in the compose UI.
*
* Note: This is not the compose action that builds a compose UI, but
* rather an action taken when the user interacts with the compose UI.
*
* @return {UpdateDraftActionResponse}
*/
function applyUpdateCcRecipientsAction() {
// Get the new Cc recipients of the email.
// This function is not shown in this example.
var ccRecipients = ['big.blue@montana.com'];
var response = CardService.newUpdateDraftActionResponseBuilder()
.setUpdateDraftCcRecipientsAction(CardService.newUpdateDraftCcRecipientsAction()
.addUpdateCcRecipients(ccRecipients))
.build();
return response;
}
/**
* Updates the Bcc recipients of the current email when the user clicks
* on "Update Bcc recipients" in the compose UI.
*
* Note: This is not the compose action that builds a compose UI, but
* rather an action taken when the user interacts with the compose UI.
*
* @return {UpdateDraftActionResponse}
*/
function applyUpdateBccRecipientsAction() {
// Get the new Bcc recipients of the email.
// This function is not shown in this example.
var bccRecipients = ['spacer@gmail.com'];
var response = CardService.newUpdateDraftActionResponseBuilder()
.setUpdateDraftBccRecipientsAction(CardService.newUpdateDraftBccRecipientsAction()
.addUpdateBccRecipients(bccRecipients))
.build();
return response;
}
appsscript
{
"timeZone": "Asia/Kolkata",
"dependencies": {},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"oauthScopes": [
"https://www.googleapis.com/auth/gmail.addons.current.action.compose",
"https://www.googleapis.com/auth/gmail.addons.current.message.readonly",
"https://www.googleapis.com/auth/gmail.addons.execute",
"https://www.googleapis.com/auth/script.locale",
"https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/gmail.readonly",
"https://www.googleapis.com/auth/gmail.compose",
"https://www.googleapis.com/auth/gmail.modify",
"https://mail.google.com/",
"https://www.googleapis.com/auth/gmail.addons.current.message.metadata",
"https://www.googleapis.com/auth/userinfo.email"
],
"addOns": {
"common": {
"name": "Example 1",
"logoUrl": "https://www.gstatic.com/images/branding/product/2x/contacts_48dp.png",
"useLocaleFromApp": true,
"homepageTrigger": {
"runFunction": "startApp",
"enabled": true
},
"universalActions": [
{
"label": "Google",
"openLink": "www.google.com"
}
]
},
"gmail": {
"contextualTriggers": [
{
"unconditional": {},
"onTriggerFunction": "startApp"
}
],
"composeTrigger": {
"selectActions": [
{
"text": "Login",
"runFunction": "startApp"
}
],
"draftAccess": "METADATA"
}
}
}
}
Compose UI screenshot...
Email Draft screenshot...
The "Compose UI screenshot" you have posted has nothing to do with the "Email Draft screenshot". Let's discover the manifest file. It is easy to find "Example 1" title of the posted "compose" screenshot and what we see is this UI related to common
section of the add-on manifest, particularly the call to startApp
function is from homepageTrigger
. Homepage of the add-on has nothing to do with context of gmail
. So you click add-on's "home" button and expect to manipulate in gmail draft context. This won't work. Usually, ppl put on the homepage add-on information, user settings, etc. This is what this page for.
To work with contextual triggers of the gmail
draft message, you specified composeTrigger
with the text Login
. Find the button on the "gmail compose window" with the same icon as "homepage" and title "Login". Click it when composing a message and you will be prompted with the card in a floating dialog box (not a taskpane like you posted screenshot for homepage). This dialog will be tight to the message you are currently composing and the applyUpdateEmail
will start updating your draft message.
If you still have a questions, please refer to official documentation Extending Gmail with Google Workspace add-ons and particularly on Extending the message UI