You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Google Script提取邮件收件人并写入Google Sheet B列

Extending Google Script to Extract Email To Field to Google Sheet Column B

Alright, let's tweak your existing Google Apps Script to capture the recipient info from the email's To field and write it into column B of your Google Sheet. Here's the updated code with key changes highlighted:

// Modified from http://pipetree.com/qmacro/blog/2011/10/automated-email-to-task-mechanism-with-google-apps-script/
// Globals, constants
var LABEL_PENDING = "Example label/PENDING";
var LABEL_DONE = "Example label/DONE";

// processPending(sheet)
// Process any pending emails and then move them to done
function processPending_(sheet) {
  // Date format
  var d = new Date();
  var date = d.toLocaleDateString();

  // Get out labels by name
  var label_pending = GmailApp.getUserLabelByName(LABEL_PENDING);
  var label_done = GmailApp.getUserLabelByName(LABEL_DONE);

  // The threads currently assigned to the 'pending' label
  var threads = label_pending.getThreads();

  // Process each one in turn, assuming there's only a single
  // message in each thread
  for (var t in threads) {
    var thread = threads[t];
    // Gets the message object (not just body)
    var message = thread.getMessages()[0];
    var messageBody = message.getBody(); // Renamed for clarity
    
    // NEW: Extract recipient info from the To field
    var recipient = message.getTo();
    
    // Processes the messages here
    orderinfo = messageBody.split("example split");
    rowdata = orderinfo[1].split(" ");

    // NEW: Append both existing data (column A) and recipient (column B)
    sheet.appendRow([rowdata[1], recipient]);

    // Set to 'done' by exchanging labels
    thread.removeLabel(label_pending);
    thread.addLabel(label_done);
  }
}

// main()
// Starter function; to be scheduled regularly
function main_emailDataToSpreadsheet() {
  // Get the active spreadsheet and make sure the first
  // sheet is the active one
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.setActiveSheet(ss.getSheets()[0]);

  // Process the pending emails
  processPending_(sh);
}

Key Changes Explained:

  • Split the message object and body for clarity: Storing the full message object instead of just the body makes it easy to access other email properties like getTo().
  • Added var recipient = message.getTo();: This pulls the full content of the email's To field. If there are multiple recipients, they'll show up as a comma-separated string (e.g., "user1@example.com, user2@example.com").
  • Updated sheet.appendRow([rowdata[1], recipient]);: The array now includes two values—your original order data goes to column A, and the recipient info lands in column B.

If you need to handle multiple recipients individually (like splitting them into separate rows or cleaning up formatting), you could add extra logic to split the recipient string with split(",") and process each entry. But this version covers your core requirement perfectly.

内容的提问来源于stack exchange,提问作者David K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:22:48