Home > other >  How to "clip" similar emails and import only part of them in Google Sheets
How to "clip" similar emails and import only part of them in Google Sheets

Time:08-17

I use a script to import the body of emails to a sheet.

This is the part of the script that imports the body of emails to a sheet

  var newReceipts = [];
  for (var i = 0; i < foundThreads.length; i  ) {
    var messages = foundThreads[i].getMessages();
    for (var m = 0; m < messages.length; m  ) {
      var subject = messages[m].getSubject(); //subject field
      var body = messages[m].getPlainBody(); //body field
      var from = messages[m].getFrom(); //from field
      newReceipts.push([subject,body,from]);

A sample text (body) would be:

some text I need to keep,  
more text I need....  

Did you know that:  

bla bla bla and more and more I DON'T need

Another email could be:

Our meeting will take place on Monday

Did you know that:  
You cannot be late?

A third email could be:

There is great strain in our group.
The following people please report to the human resources department.
John
Mary
Paul

Did you know that:
A raise is coming next month?
Pay increases tend to vary based on inflation, location, sector, and job performance.
Most employers give their employees an average increase of 3% per year.
Consistent job switching may have an impact on the rate at which your salary increases.

So, there is always a top part, followed by the constant Did you know that:, followed by a bottom part.

I only need to keep the top part of the email before the Did you know that:

CodePudding user response:

In your situation, how about the following sample script?

Sample script:

const check = "Did you know that";
const sample1 = `some text I need to keep,  
more text I need....  

Did you know that:  

bla bla bla and more and more I DON'T need`;
const sample2 = `Our meeting will take place on Monday

Did you know that:  
You cannot be late?`;
const sample3 = `There is great strain in our group.
The following people please report to the human resources department.
John
Mary
Paul

Did you know that:
A raise is coming next month?
Pay increases tend to vary based on inflation, location, sector, and job performance.
Most employers give their employees an average increase of 3% per year.
Consistent job switching may have an impact on the rate at which your salary increases.`;
const values = [sample1, sample2, sample3];
const res = values.map(e => e.includes(check) ? e.split(check)[0].trim() : "");
console.log(res)

  • When this script is run, the following result is obtained.

      [
        "some text I need to keep,  \nmore text I need....",
        "Our meeting will take place on Monday",
        "There is great strain in our group.\nThe following people please report to the human resources department.\nJohn\nMary\nPaul"
      ]
    
  • When the value of check is not included in the text, "" is returned.

  • When your showing script is modified, how about the following modification?

      var newReceipts = [];
      for (var i = 0; i < foundThreads.length; i  ) {
        var messages = foundThreads[i].getMessages();
        for (var m = 0; m < messages.length; m  ) {
          var subject = messages[m].getSubject(); //subject field
          var body = messages[m].getPlainBody(); //body field
    
          // --- Added
          const check = "Did you know that";
          if (body.includes(check)) {
            body = body.split(check)[0].trim();
          }
          // ---
    
          var from = messages[m].getFrom(); //from field
          newReceipts.push([subject,body,from]);
    

References:

  • Related