简体   繁体   中英

How to make apps script to send email with the information matching with survey

What I am trying to do is, If someone completed the survey, then according to his survey, send several values from the datasets. But I could not make this condition in JS version..

Like in picture 1, send email to the first person

  1. How many pets are matching with his preference,

    count if where status = Adoptable.

    Preferred Pet species = Dog

    Age = Adult or kitten

    Gender = Male

    Size = Small

  2. The information of adoptable pet Name, Primary Color, Characteristics from the 1.

The google form survey result

The data of pet

function sendEmail() {
  var ss = SpreadsheetApp.getActiveSpreadsheet()


  // get sheets
  var response_sheet = ss.getSheetByName('survey_sheet');
  var score_sheet = ss.getSheetByName('pet_data');

  var Avals = response_sheet.getRange("A1:A").getValues();
  var Alast = Avals.filter(String).length;

  // get email address
  var emailAddress = response_sheet.getRange(Alast,2).getValue();
  
  // count value of matching data
  var count_pet = ()

  // create message
  var subject = "Adoptable Pet List"
  // greetings
  var message = "Hello, This is the adoptable pet list for you.\n\n There are count_pet are 
  available matching with your preference \n There are [Name], [Primary Color], 
  [Characteristics ]"
  var text = sheet.getRange(idx,2).getValue();
    
  message = message + text + '\n'
  // Send Email
  MailApp.sendEmail(emailAddress, subject, message);
}

This answer provides a summary of the matches currently available in tabular format.

GS:

function onFormSubmitForSpreadsheet(e) {
  const ss = SpreadsheetApp.getActive();
  const dsh = ss.getSheetByName('pet data')
  const vs = dsh.getRange(2, 1, dsh.getLastRow() - 1, dsh.getLastColumn()).getValues().filter(r => r[3] == e.values[2] && r[7] == e.values[3] && r[8] == e.values[4] && r[9] == e.values[5]).filter(e => e);
  if (vs && vs.length > 0) {
    const emailAddress = e.values[1];
    const subject = "Adoptable Pet List";
    let t = HtmlService.createTemplateFromFile('ah1');
    t.tabledata = vs;
    t.lr = vs.length;
    MailApp.sendEmail(emailAddress, subject, "", { htmlBody: t.evaluate().getContent() });
  }
}

HTML:

<!DOCTYPE html>
<html>
<head>
  <base target="_top">
</head>
<body>
  <p>Your can write an introduction here</p>
  <p>The following table is a summary of the current <?= lr ?> matches
  <div id="tabledata">
       <? var vs = getMyData(); ?>
       <table>
         <? vs.forEach((r,i)=>{ ?>
           <tr>
           <? r.forEach((c,j)=>{ ?>
             <? if(i == 0) { ?>
            <th style="padding:2px 5px;font-weight:bold;border:1px solid black;"><?= c ?> </th>           
           <? } else { ?>
             <td style="padding:2px 5px;border:1px solid black;"><?= vs[i][j] ?> </td>
           <? } ?>
         <?  }); ?>
           </tr>
         <? }); ?>
       </table>
     </div>
     <p>Your can write an ending here</p>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM