title: Get gmail message hyperlink using Apps Script
body: <p>I'm trying to get the Url of Gmail messages and store them into a Google Sheet. I'm doing this because the sheet will serve as a dashboard and I want to be able to link to specific messages which can be quickly opened by clicking the message hyperlink.</p>
  <p>Gmail message urls will start with something like &quot;https://mail.google.com/mail/u/0/?pli=1#inbox/&quot; for messages in the 'inbox' followed by a string of around 70 characters representing a message ID such as &quot;ZhatKJWQfkVxNWLWSctJlTPzJAjLTGmHOFwlqPWVTvjkwVgsnhBsfmJrrvjRDAHfKozQnYn&quot;.</p>
  <p>As far as I can tell there is no way to get the full url from the <a href="https://developers.google.com/apps-script/reference/gmail/gmail-message" rel="nofollow noreferrer">GMail Message class</a>.  However you can retrieve the message id using getId().</p>
  <p>So my approach was to retrieve the ID, append it to the inbox prefix (&quot;https://mail.google.com/mail/u/0/?pli=1#inbox/&quot;) and that should create the link as follows:</p>
  <pre><code>for (var x = messageThread.length-1; x&gt;=0; x--) {
  for (var y = 0; y&lt; numMessagesInThread; y++) {
  var message = messageThread[x].getMessages()[y];
  var messageId = message.getId();
  var messageUrl = &quot;https://mail.google.com/mail/u/0/?pli=1#inbox/&quot; + messageId;
  ...
  </code></pre>
  <p>The problem is it turns out getId() doesn't return the 70 character Id in the original Url of the message but rather a 16 character Id such as &quot;2487g294ie3f3x8z&quot;.  Needless to say when used in the URL it doesn't work.<br />
  I'm stuck at this point and have been searching for a way to do this, if anyone has ideas would be greatly appreciated.</p>