Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

I'm trying to pull data from gmail (ie. from, to, date, subject, body, etc.) but I'm getting an error saying the getBody() length is too long:

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

I'm trying to have a cut off of characters or just not pull the email with a Body over the character limit. Also, I'm pretty out of practice on this and it's not working...

//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);
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

One way is by using String.prototype.slice().

Replace

messages[j].getBody(),

by

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

Related

  • Substring vs Slice vs Other?
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!