如何在使用AppScript时获取Gmail中的当前消息ID?

huangapple go评论70阅读模式
英文:

How to get current message Id in Gmail using AppScript?

问题

所以,我在互联网上寻找代码以获取当前消息ID或仅获取当前消息,但似乎找不到。我实际想要的是,当我点击邮件并打开它时,我正在开发的插件应该能够获取消息或消息ID,以便我可以从该邮件中获取一些信息。我该如何做呢?我尝试了一些方法,但似乎无法使其正常工作。有什么建议吗?非常感谢。

英文:

So, I was looking around the internet for a code to get the current message Id or just current message but I cannot seem to find it. What I actually want is, when I click on a mail and open it, the add-on I am working on should be able to get the message or the message id so that I can get hold of some information from that mail. How do I do it? I tried few things but cannot seem to get it working. Any idea? any suggestion is greatly appreciated, Thanks.

答案1

得分: 1

如果您使用 [tag:google-workspace-add-ons],您可以使用 上下文触发器 来在打开邮件时(并点击您的插件图标或插件主页已经打开时)自动运行一个函数。设置为触发的函数将接收一个 事件对象,其中包含邮件的消息 ID。

const functionThatIsSetToRunWhenUserOpensAGmail = (eventObject) => {
  const messageId = eventObject.gmail.messageId;
}
英文:

If you use [tag:google-workspace-add-ons], you can use Contextual triggers to automatically run a function, when opening a message (and clicking your addon icon or of addon homepage is already open). The function that is set to trigger will receive a event object, which will contain the message id.

const functionThatIsSetToRunWhenUserOpensAGmail = (eventObject) => {
  const messageId = eventObject.gmail.messageId;
}

答案2

得分: 0

以下是翻译好的部分:

"So, I did solve it and all thanks to @TheMaster for his help. So, What I did was I edited the manifest file and then added the code in my code.gs file. This is what I added in my Manifest file,

"addOns": {
    "common": {
      "name": "Attachment Downloader",
      "logoUrl": "URL FOR LOGO"
    },
    "gmail": {
      "contextualTriggers": [
        {
          "unconditional": {},
          "onTriggerFunction": "onGmailMessageOpen"
        }
      ]
    }
  }

You can enable the manifest file to be displayed along with other files by going to project settings and enabling it there."

"then the onGMailMessageOpen function inside the code.gs file. So when the user opens any message, this function is automatically called and an eventobject is passed to it. So using this event object we can access the EMail/Message that is currently open, but first you will need to get the access token like the code below and get hold of the current message and its Id.

function onGmailMessageOpen(eventObject){
  // Activate temporary Gmail scopes, in this case to allow
  // message metadata to be read.
  var accessToken = eventObject.gmail.accessToken;
  GmailApp.setCurrentMessageAccessToken(accessToken);

  var messageId = eventObject.gmail.messageId;
}

you can also get all of these from the official documentation, through this link,

https://developers.google.com/apps-script/reference/gmail ,

you might have to find what you're looking for as there is a lot to take in."

英文:

So, I did solve it and all thanks to @TheMaster for his help. So, What I did was I edited the manifest file and then added the code in my code.gs file.
This is what I added in my Manifest file,

"addOns": {
    "common": {
      "name": "Attatchment Downloader",
      "logoUrl": "URL FOR LOGO"
    },
    "gmail": {
      "contextualTriggers": [
        {
          "unconditional": {},
          "onTriggerFunction": "onGmailMessageOpen"
        }
      ]
    }
  }

You can enable the manifest file to be displayed along with other files by going to project settings and enabling it there.

and then the onGMailMessageOpen function inside the code.gs file.
So when the user opens any message, this function is automatically called and an eventobject is passed to it. So using this event object we can access the EMail/Message that is currently open, but first you will need to get the access token like the code below and get hold of the current message and its Id.

function onGmailMessageOpen(eventObject){
  // Activate temporary Gmail scopes, in this case to allow
  // message metadata to be read.
  var accessToken = eventObject.gmail.accessToken;
  GmailApp.setCurrentMessageAccessToken(accessToken);

  var messageId = eventObject.gmail.messageId;
}

you can also get all of these from the official documentation, through this link,

https://developers.google.com/apps-script/reference/gmail ,

you might have to find what your looking for as there is lot to take in.

huangapple
  • 本文由 发表于 2023年4月10日 23:05:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定