最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google apps script - Gmail how create draft reply, witch is in these same thread - Stack Overflow

programmeradmin0浏览0评论

I created a script (via Google App Script) that simplifies replying to emails. The script replaces the following manual steps: (while in an email)

  1. Forward the email.
  2. Copy the "To" addresses from the original email.
  3. Paste the "To" addresses into the forwarded email.
  4. Copy the "CC" addresses from the original email.
  5. Paste the "CC" addresses into the forwarded email.
  6. Add a short text.
  7. The email is then saved in the "Drafts" folder.

Code:

function REP() {

// Activate temporary Gmail scopes, in this case to allow
// the add-on to read message metadata and content.
var accessToken = e.gmail.accessToken;
GmailApp.setCurrentMessageAccessToken(accessToken);

// Read message metadata and content. This requires the Gmail scope
// .addons.current.message.readonly.

var  d1 = CardService.newDivider();

var textToIn2 = CardService.newTextInput()
  .setFieldName("textTO2")
  .setMultiline(true);

var composeAction = CardService.newAction()
      .setFunctionName('createReplyDraft');
var composeButton = CardService.newTextButton()
    .setText('FK3')
    .setComposeAction(composeAction, CardService.ComposedEmailType.REPLY_AS_DRAFT);

var cardSection1 = CardService.newCardSection()
    //.setHeader('InFFo')
    //.setCollapsible(false)
    //.setNumUncollapsibleWidgets(0)
    .addWidget(textToIn2)
    .addWidget(d1)
    .addWidget(composeButton);

var card = CardService.newCardBuilder()
        .addSection(cardSection1)
        .build();

return card;
}

function createReplyDraft(e) {
 // Activate temporary Gmail scopes, in this case to allow
 // a reply to be drafted.
 //var accessToken = e.gmail.accessToken;
 //GmailApp.setCurrentMessageAccessToken(accessToken);

 // Creates a draft reply.
 var messageId = e.gmail.messageId;
 var message = GmailApp.getMessageById(messageId);

 
 var mto = message.getTo();
 //Logger.log("mto= " + mto);
 var mfrom = message.getFrom();
 //Logger.log("mfrom= " + mfrom);
 var mtoCC0 = message.getCc();
 //Logger.log("mtoCC0= " +mtoCC0);
 var mSub = message.getSubject();
 //Logger.log("mSub= " + mSub);

 var mBody = message.getBody();
 //var mAttach = m.getAttachments();
 var mAttach = message.getAttachments();

 // delete all spaces
 //var NmtoCC = mtoCC0.replace(/ /g, '');
 //var adressArr = NmtoCC.split(",");
 var f = "some e- mail adresss @aaaaaa"; //looking adress

 var Findf = false; 

     if (mtoCC0.indexOf(f) < 0) { Findf = true; }
    
     //Logger.log("Findf= " + Findf);
    
     if (Findf) { mtoCC0= mtoCC0 + " ," + f ;} 
     //Logger.log("2. mtoCC0= " +mtoCC0);
    
     var textget = e.formInputs.textTO2;
     //e.formInputs.textTO
     //Logger.log("TEXT= " + textget);
    
     mBody = "<br>" + textget + "<br><br><br>" + mBody;
    
      var draft = message.createDraftReply('', {  cc: mtoCC0,
                                                  htmlBody: mBody,
                                                  attachments: mAttach,
                                                  replyTo: mfrom,
                                                  subject: mSub
                                                                                            
                                                 }
                  );
    
      // Return a built draft response. This causes Gmail to present a
      // compose window to the user, pre-filled with the content specified
      // above.
      return CardService.newComposeActionResponseBuilder()
            .setGmailDraft(draft).build();
    
    
    } 

The problem is that my coworkers receive my replies as a new email, rather than as a continuation of the thread. What should I change to ensure they receive it as a continuation of the thread?

appsscript.json

{
  "oauthScopes": [
    ".container.ui",
    ".addons.execute",
    ".addons.current.message.metadata",
    ".modify",
    ".addons.current.actionpose",
    ".locale"

  ],

  "gmail": {
      "name": "FallowK",
      "logoUrl": ".png",
      "primaryColor": "#07b4ed",
      "secondaryColor": "#ed7207",
      "contextualTriggers": [
        {
          "unconditional": {},
          "onTriggerFunction": "REP"
        }
      ]
         
  },

  "timeZone": "Europe/Warsaw",
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8"
}

I created a script (via Google App Script) that simplifies replying to emails. The script replaces the following manual steps: (while in an email)

  1. Forward the email.
  2. Copy the "To" addresses from the original email.
  3. Paste the "To" addresses into the forwarded email.
  4. Copy the "CC" addresses from the original email.
  5. Paste the "CC" addresses into the forwarded email.
  6. Add a short text.
  7. The email is then saved in the "Drafts" folder.

Code:

function REP() {

// Activate temporary Gmail scopes, in this case to allow
// the add-on to read message metadata and content.
var accessToken = e.gmail.accessToken;
GmailApp.setCurrentMessageAccessToken(accessToken);

// Read message metadata and content. This requires the Gmail scope
// https://www.googleapis/auth/gmail.addons.current.message.readonly.

var  d1 = CardService.newDivider();

var textToIn2 = CardService.newTextInput()
  .setFieldName("textTO2")
  .setMultiline(true);

var composeAction = CardService.newAction()
      .setFunctionName('createReplyDraft');
var composeButton = CardService.newTextButton()
    .setText('FK3')
    .setComposeAction(composeAction, CardService.ComposedEmailType.REPLY_AS_DRAFT);

var cardSection1 = CardService.newCardSection()
    //.setHeader('InFFo')
    //.setCollapsible(false)
    //.setNumUncollapsibleWidgets(0)
    .addWidget(textToIn2)
    .addWidget(d1)
    .addWidget(composeButton);

var card = CardService.newCardBuilder()
        .addSection(cardSection1)
        .build();

return card;
}

function createReplyDraft(e) {
 // Activate temporary Gmail scopes, in this case to allow
 // a reply to be drafted.
 //var accessToken = e.gmail.accessToken;
 //GmailApp.setCurrentMessageAccessToken(accessToken);

 // Creates a draft reply.
 var messageId = e.gmail.messageId;
 var message = GmailApp.getMessageById(messageId);

 
 var mto = message.getTo();
 //Logger.log("mto= " + mto);
 var mfrom = message.getFrom();
 //Logger.log("mfrom= " + mfrom);
 var mtoCC0 = message.getCc();
 //Logger.log("mtoCC0= " +mtoCC0);
 var mSub = message.getSubject();
 //Logger.log("mSub= " + mSub);

 var mBody = message.getBody();
 //var mAttach = m.getAttachments();
 var mAttach = message.getAttachments();

 // delete all spaces
 //var NmtoCC = mtoCC0.replace(/ /g, '');
 //var adressArr = NmtoCC.split(",");
 var f = "some e- mail adresss @aaaaaa"; //looking adress

 var Findf = false; 

     if (mtoCC0.indexOf(f) < 0) { Findf = true; }
    
     //Logger.log("Findf= " + Findf);
    
     if (Findf) { mtoCC0= mtoCC0 + " ," + f ;} 
     //Logger.log("2. mtoCC0= " +mtoCC0);
    
     var textget = e.formInputs.textTO2;
     //e.formInputs.textTO
     //Logger.log("TEXT= " + textget);
    
     mBody = "<br>" + textget + "<br><br><br>" + mBody;
    
      var draft = message.createDraftReply('', {  cc: mtoCC0,
                                                  htmlBody: mBody,
                                                  attachments: mAttach,
                                                  replyTo: mfrom,
                                                  subject: mSub
                                                                                            
                                                 }
                  );
    
      // Return a built draft response. This causes Gmail to present a
      // compose window to the user, pre-filled with the content specified
      // above.
      return CardService.newComposeActionResponseBuilder()
            .setGmailDraft(draft).build();
    
    
    } 

The problem is that my coworkers receive my replies as a new email, rather than as a continuation of the thread. What should I change to ensure they receive it as a continuation of the thread?

appsscript.json

{
  "oauthScopes": [
    "https://www.googleapis/auth/script.container.ui",
    "https://www.googleapis/auth/gmail.addons.execute",
    "https://www.googleapis/auth/gmail.addons.current.message.metadata",
    "https://www.googleapis/auth/gmail.modify",
    "https://www.googleapis/auth/gmail.addons.current.actionpose",
    "https://www.googleapis/auth/script.locale"

  ],

  "gmail": {
      "name": "FallowK",
      "logoUrl": "https://www.gstatic/images/icons/material/system/1x/label_googblue_24dp.png",
      "primaryColor": "#07b4ed",
      "secondaryColor": "#ed7207",
      "contextualTriggers": [
        {
          "unconditional": {},
          "onTriggerFunction": "REP"
        }
      ]
         
  },

  "timeZone": "Europe/Warsaw",
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8"
}
Share Improve this question edited Jan 19 at 19:32 Wicket 38.5k9 gold badges78 silver badges193 bronze badges asked Jan 18 at 14:46 Chora PocztaChora Poczta 11 bronze badge 1
  • 1 The script seems to be working properly. I'm unable to reproduce the problem where the replies are a new email, rather than as a continuation of the thread. Kindly share a minimal, reproducible example—no sensitive data with your desired output—that the community can use to replicate and see what you'd like to do. – Saddles Commented Jan 20 at 18:41
Add a comment  | 

1 Answer 1

Reset to default -1

What is the e event in the createReplyDraft(e) function? How do you activated this function? What do you have in your manifest file?

If you activate this function using a contextual trigger, then from the e event you can extract information about the current message and thread, which you can use to reply.

For example:

var messageId = e.messageMetadata.messageId;
var threadId = GmailApp.getMessageById(messageId).getThread().getId();

Read Make email more actionable with Google Workspace Add-ons, it will help you.

发布评论

评论列表(0)

  1. 暂无评论