BadFish523
BadFish523

Reputation: 43

Google Sheets script that sends email when form is submitted. Need slight adjustment

I have this script that sends an email when a new form submission is received. I need to see if there is an easy way to edit this to only send the email on a form submission when value 4 = Fail Can anybody help me? I can't even guess where to begin as this script was written for me by someone here.

 function triggermenu() {
  SpreadsheetApp.getUi().createMenu('Trigger Menu')
  .addItem('Create Trigger', 'createFormSubmitTrigger')
  .addToUi();
}

function createFormSubmitTrigger(funcname) {
  var funcname=funcname||'form1Submit';
  if(!isTrigger(funcname)) {
    ScriptApp.newTrigger(funcname).forSpreadsheet(SpreadsheetApp.getActive()).onFormSubmit().create();
  }
}

function form1Submit(e) {
      if(e.values && e.values[1] && e.values[2]) {
        var html='<style>td,th{padding:overflow-wrap: break-word;}table{table-layout:fixed;width:100%;}</style><table>';
        html+=Utilities.formatString('<tr><td>%s</td><td style="width:20%;" colspan ="2"><font size="4" color="red"><b>%s</b></font></td><td style="width:80%;">%s</td></tr>','&nbsp;','**** DO NOT REPLY TO THIS EMAIL ****','&nbsp;' );
        html+=Utilities.formatString('<tr><td>%s</td><td style="width:20%;">%s</td><td style="width:80%;">%s</td></tr>','&nbsp;','&nbsp;','&nbsp;' );//empty line
        html+=Utilities.formatString('<tr><td>%s</td><td style="width:20%;">%s</td><td style="width:80%;">%s</td></tr>','&nbsp;','&nbsp;','&nbsp;' );//empty line
        html+=Utilities.formatString('<tr><td>%s</td><td style="width:20%;" colspan ="2">%s</td><td style="width:80%;">%s</td></tr>','&nbsp;','Please review findings','&nbsp;' );
        html+=Utilities.formatString('<tr><td>%s</td><td style="width:20%;" colspan ="2"><font size="5"><b>%s</b></font></td><td style="width:80%;">%s</td></tr>','&nbsp;','VISUAL WELDING AUDIT','&nbsp;' );
        html+=Utilities.formatString('<tr><td>%s</td><td style="width:20%;"><strong>%s</strong></td><td style="width:80%;">%s</td></tr>','&nbsp;','Date and Time of Audit:',e.values[0]);
        html+=Utilities.formatString('<tr><td>%s</td><td style="width:20%;"><strong>%s</strong></td><td style="width:80%;">%s</td></tr>','&nbsp;','Auditor:',e.values[1]); 
        html+=Utilities.formatString('<tr><td>%s</td><td style="width:20%;"><strong>%s</strong></td><td style="width:80%;">%s</td></tr>','&nbsp;','Line/Location Audited:',e.values[2]);
        html+=Utilities.formatString('<tr><td>%s</td><td style="width:20%;"><strong>%s</strong></td><td style="width:80%;">%s</td></tr>','&nbsp;','Area Responsible:',e.values[3]);
        html+=Utilities.formatString('<tr><td>%s</td><td style="width:20%;">%s</td><td style="width:80%;">%s</td></tr>','&nbsp;',RoGpf(e.values[4]),RoG(e.values[4]));
        html+=Utilities.formatString('<tr><td>%s</td><td style="width:20%;"><strong>%s</strong></td><td style="width:80%;">%s</td></tr>','&nbsp;','Person Notified:',e.values[9]);
        html+=Utilities.formatString('<tr><td>%s</td><td style="width:20%;"><strong>%s</strong></td><td style="width:80%;">%s</td></tr>','&nbsp;','Disposition:',e.values[5]);
        html+=Utilities.formatString('<tr><td>%s</td><td style="width:20%;"><strong>%s</strong></td><td style="width:80%;">%s</td></tr>','&nbsp;','Discontinuity Found:',e.values[6]);
        html+=Utilities.formatString('<tr><td>%s</td><td style="width:20%;"><strong>%s</strong></td><td style="width:80%;">%s</td></tr>','&nbsp;','Safety Findings Comment:',e.values[7]);
        html+=Utilities.formatString('<tr><td>%s</td><td style="width:20%;"><strong>%s</strong></td><td style="width:80%;">%s</td></tr>','&nbsp;','Additonal Comments:',e.values[8]);
        html+=Utilities.formatString('<tr><td>%s</td><td style="width:20%;">%s</td><td style="width:80%;">%s</td></tr>','&nbsp;','&nbsp;','&nbsp;' );//empty line
        html+=Utilities.formatString('<tr><td>%s</td><td style="width:20%;">%s</td><td style="width:80%;">%s</td></tr>','&nbsp;','&nbsp;','&nbsp;' );//empty line
        html+=Utilities.formatString('<tr><td>%s</td><td><a href="https://docs.google.com/spreadsheets/d/1tsBcTDVcI1p7GtgTbtgQGdRwerZqlpyWKdEHcT5nNT0/edit?usp=sharing">Link to Dashboard</a></td><td>%s</td></tr>','&nbsp;','&nbsp;','&nbsp;' );
        html+='</table>';
    Logger.log(html);
    GmailApp.sendEmail(getGlobal('form1Email'), getGlobal('form1Subject'), '', {htmlBody:html});
  }
}

function RoG(s) {
  if(s) {
    if(s=='Pass') {
      return '<span style="color:#007800;">Pass</span';
    }else if(s=='Fail') {
      return '<span style="color:#d20000;">Fail</span>';
    }
  }
  return s;
}

function RoGpf(s) {
  if(s) {
    if(s=='Pass') {
      return '<span style="color:#007800;font-weight:bold">Pass/Fail</span';
    }else if(s=='Fail') {
      return '<span style="color:#d20000;font-weight:bold">Pass/Fail</span>';
    }
  }
  return s;
}

Thank you in advance!

Upvotes: 1

Views: 70

Answers (1)

BadFish523
BadFish523

Reputation: 43

This is what worked for me. The IF( at the beginning of the form1sub function now looks like this.

if(e.values && e.values[1] && e.values[2] && e.values[3]=='Fail')

Upvotes: 2

Related Questions