如何在应用程序脚本中限制从 getBody 中提取的字符

How do I limit the characters pulled from getBody in apps script

我正在尝试从 gmail 中提取数据(即从、到、日期、主题、正文等),但我收到一条错误消息,提示 getBody() 长度太长:

your input contains more than the maximum of 50000 characters in a single cell

我正在尝试截断字符,或者只是不将正文超过字符限制的电子邮件拉出来。另外,我在这方面很缺乏实践,而且它不起作用...

//Allow user to select label(s) and date range
function getGmailEmails() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var label = sheet.getRange(1, 3).getValue();
  var after = sheet.getRange(2, 3).getDisplayValue();
  var before = sheet.getRange(3, 3).getDisplayValue();
  var threads = GmailApp.search("label:" + label + " AND " + "after:" + after + " AND " + "before:" + before);

  // Export emails into table of values in google sheet
  // Character length cutoff is 2000, the cell with get Body would tell you to go to the inbox
  var values = [];
  for (var i = 0; i < threads.length < 2000; i++) {
    var temp = [];
    var label = threads[i].getLabels().map(e => e.getName()).join(", ");
    let messages = threads[i].getMessages();
    for (var j = 0; j < messages.length; j++) {
      temp.push([
        label,
        messages[j].getFrom(),
        messages[j].getTo(),
        messages[j].getCc(),
        messages[j].getDate(),
        messages[j].getSubject(),
        messages[j].getBody(),
        messages[0].getId()
      ]);
    }
    values = values.concat(temp);
  }
  sheet.getRange(6, 1, values.length, values[0].length).setValues(values);
}

一种方法是使用 String.prototype.slice().

替换

messages[j].getBody(),

来自

messages[j].getBody().slice(0,50000),

相关