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

198
Views
Breaking google sheet table into smaller tables and send gmail to corresponding emails(google script)

Is there a way that we can break the main table below into smaller tables and email (gmail) them to the corresponding email on the first column?

Bank's Email Client name Age Bank with
bank1@hotmail.com Client 1 19 Bank 1
bank1@hotmail.com Client 2 32 Bank 1
bank1@hotmail.com Client 3 22 Bank 1
bank2@hotmail.com Client 4 34 Bank 2
bank2@hotmail.com Client 5 22 Bank 2
bank1@hotmail.com Client 5 28 Bank 3

For example, send the below table as gmail to bank 1's email ?

Client name Age Bank with
Client 1 19 Bank 1
Client 2 32 Bank 1
Client 3 22 Bank 1

and below to bank 2's email (same with bank 3)?

Client name Age Bank with
Client 4 34 Bank 2
Client 5 22 Bank 2

Thanks!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can try this. I leave it up to you to adjust the styling to suit your taste. I edited this to add the first part. Sorting the clients.

function test() {
  try {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sh = ss.getSheetByName("Sheet2");
    // dont get header
    var data = sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn()).getValues();
    var banks = [];
    data.forEach( row => banks.push(row[0]) );
    banks = [...new Set(banks)];
    var results = [];
    function addClient(row) {
      var i = banks.indexOf(row[0]);
      if( results[i] === undefined ) results[i] = [];
      row.shift();
      results[i].push(row);
    }
    data.forEach( addClient );
    results.forEach( (bank,index) => createTable(banks[index],bank) );
  }
  catch(err) {
    console.log(err);
  }
}

function createTable(email,data) {
  try {
    var table = "<table style='border-width:thin;border-style:solid;border-color:black'>";
    function addCell(value) {
      table = table.concat("<td style='border-width:thin;border-style:solid;border-color:black;width:20px'>");
      table = table.concat(value.toString());
      table = table.concat("</td>");
    }
    function addRow(row) {
      table = table.concat("<tr>");
      row.forEach( addCell );
      table = table.concat("</tr>");
    }
    data.forEach( addRow )
    table = table.concat("</table>")
    console.log(table);
    MailApp.sendEmail( {
      to: "thewized@gmail.com",
      subject: "Test",
      htmlBody: table });
  }
  catch(err) {
    console.log(err);
  }
}
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!