如何使用Google Apps脚本获取草稿消息的threadID

时间:2019-04-18 20:48:47

标签: google-apps-script google-sheets gmail

我想获取使用以下代码创建的草稿的threadID。 在请求draftID时,它看起来像draftID!= threadID。

我想要threadID,以便在用户重定向到gmail页面时打开带有threadID的网址。

在下面附加代码。

我已经尝试了draftID,如下面的代码所示。

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries1 = [{
    name : "Email All Volunteers",
    functionName : "myFunction"
  }];
  sheet.addMenu("Volunteers", entries1);
};

function myFunction() {

   var spsheets = SpreadsheetApp.getActiveSpreadsheet();
   var masterSheet = spsheets.getSheetByName("Master");
   var lastColumnValue = masterSheet.getRange(3, 5, 50, 1).getValues(); //masterSheet.getLastRow()
   var emailSstring = lastColumnValue.join();
   Logger.log(emailSstring);

GmailApp.createDraft('', '', '', {
    bcc: emailSstring
});

  // Get the first draft message in your drafts folder
var draft = GmailApp.getDrafts()[0];
// Get its ID
var draftId = draft.getId();

// Now fetch the same draft using that ID.
var draftById = GmailApp.getDraft(draftId);
  Logger.log(draftById);
  var url = "https://mail.google.com/mail/u/0/#drafts/" + draftId;
//  var url = "https://www.googleapis.com/upload/gmail/v1/users/me/drafts"
  openUrl(url);
}
/**
 * Open a URL in a new tab.
 */
function openUrl( url ){
  var html = HtmlService.createHtmlOutput('<html><script>'
  +'window.close = function(){window.setTimeout(function(){google.script.host.close()},9)};'
  +'var a = document.createElement("a"); a.href="'+url+'"; a.target="_blank";'
  +'if(document.createEvent){'
  +'  var event=document.createEvent("MouseEvents");'
  +'  if(navigator.userAgent.toLowerCase().indexOf("firefox")>-1){window.document.body.append(a)}'                          
  +'  event.initEvent("click",true,true); a.dispatchEvent(event);'
  +'}else{ a.click() }'
  +'close();'
  +'</script>'
  // Offer URL as clickable link in case above code fails.
  +'<body style="word-break:break-word;font-family:sans-serif;">Failed to open automatically. <a href="'+url+'" target="_blank" onclick="window.close()">Click here to proceed</a>.</body>'
  +'<script>google.script.host.setHeight(40);google.script.host.setWidth(410)</script>'
  +'</html>')
  .setWidth( 90 ).setHeight( 1 );
  SpreadsheetApp.getUi().showModalDialog( html, "Opening ..." );
}

1 个答案:

答案 0 :(得分:0)

尝试一下:

function getDraftId() {
  var drafts=GmailApp.getDraftMessages();
  var html='<style>th,td{border:1px solid black}</style><table>';
  html+=Utilities.formatString('<tr><th>%s</th><th>%s</th></tr>', 'Subject','Id');
  for(var i=0;i<drafts.length;i++) {
    html+=Utilities.formatString('<tr><td>%s</td><td>%s</td></tr>', drafts[i].getSubject(),drafts[i].getId());
  }
  var ui=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModelessDialog(ui, 'My Drafts');
}