Google 脚本:"Your input contains more than the maximum of 50000 characters in a single cell"

Google Script: "Your input contains more than the maximum of 50000 characters in a single cell"

您好,我正在尝试提取我的电子邮件的 PlainBody 内容,并且正要完成提取我的电子邮件,直到在某个时候我遇到一个错误,说您的输入在一个单元格中包含超过 50000 个字符的最大值。

我如何限制在 Google 应用程序脚本中提取的字符数以避免此错误。?

  function getEmailsRange() {

  //Change sheet3 to sheet you would like the data to be pasted
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Raw");
  sheet.getRange('A1:D15000').clearContent();
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  //maximum emails to be extracted 500
  //is:unread in:MANILA
  var threads = GmailApp.search("after:2017/5/5 before:2017/5/6 in:REPORT",0,500);
  var menuEntries = [ {name: "Load Emails", functionName: "getEmails"} ];
  spreadsheet.addMenu("Email Extract", menuEntries);
  var row = 1;
  for (var i = 0; i < 50000; i++) {
    var messages=threads[i].getMessages();
    for (var m=0; m < messages.length; m++) {   

      sheet.getRange(row,4).setValue(messages[m].getDate());
      sheet.getRange(row,2).setValue(messages[m].getPlainBody());
      sheet.getRange(row,1).setValue(messages[m].getSubject());
      sheet.getRange(row,3).setValue(messages[m].getFrom());
      row++; 

    }
  }
}
//var ss = SpreadsheetApp.getActiveSpreadsheet();


function Deleterow() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName('Processed'); // change to your own
  var values = s.getDataRange().getValues();

  for (var row in values)
    if (values[row][5] == 'DELETE')
      s.deleteRow(parseInt(row)+1);
};

问题是因为您将电子邮件正文设置为电子表格中的单元格,而单元格不能超过 50000 个字符。

您可以更改以下行:

sheet.getRange(row,2).setValue(messages[m].getPlainBody());

  sheet.getRange(row,2).setValue(messages[m].getPlainBody().substring(0, 50000));

这只会将前 50000 个字符添加到单元格中。其他选项是将电子邮件提取到多个单元格或将电子邮件提取到 google 文档中。

您可以通过在 Sheet class.

上使用 appendRow 函数来解决这个问题
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");

var data = "some really long string greater than 50k characters";
// write content to a new row created, no 50k error!
sheet.appendRow([data]);

var lastRowRange = sheet.getRange(sheet.getLastRow(), 1);

// copy newly created content to desired range
var outputRange = sheet.getRange("A1");
lastRowRange.copyTo(outputRange);

// clear newly created row
lastRowRange.clearContent();