bhagyad 1994
bhagyad 1994

Reputation: 1

Email is send on loading of the app script, but email needed to be send onclick

I have created an app script in which email can be send to the login user and to the email Ids which are mentioned in spreadsheet. The email is send to the active user and to all the mentioned email Ids. However I want email to be send to login user (which is done) and to the specified email Id when selected via app script.

But the mail is send to all the mentioned email IDs in spreadsheet when the app script is getting loaded, whereas the mail should be send onclick, which is not working.

function doGet() {
  return HtmlService
      .createTemplateFromFile('index')
      .evaluate();
}


function getData() {
  return SpreadsheetApp
      .openById('SS ID')
      .getDataRange()
      .getValues();
}

function sendMessage(ManagerID,proNo,proName){
 var email=Session.getActiveUser().getEmail();
  var eId=ManagerID;
  var subject="Feedback form initiated" ;
  var message="Feedback form initiated by "+eId;
  var message2="To fill the feedback form please follow the url: ";
  var url="googleform's ID";
  var subject2="Feedback Form";
  MailApp.sendEmail(eId, subject, message);

  MailApp.sendEmail(email,subject2,message2+url);
}





//index.html
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body onload="document.getElementById('one').style.visibility='hidden'">

  <div id="initiateFb">
  <form>
    <? var data = getData(); ?>
<table border='1px solid black'>
  <? for (var i = 0; i < data.length; i++) { ?>
    <tr>
      <? for (var j = 0; j < data[i].length; j++) { ?>
        <td><?= data[i][j] ?></td>
        <?if(j==(data[i].length-1)&&i!=0) { ?>
        <td><input type="submit" name="submit" value="Initiate" 
        onclick="document.getElementById('initiateFb').style.display = 'none';
        document.getElementById('one').style.visibility='visible';
        <?var ManagerID=data[i][6];?> <?var proNo=data[i][1];?> <?var proName=data[i][2];?>
        document.getElementById('div1').innerHTML='<?=data[i][1] ?>';
        document.getElementById('div2').innerHTML='<?=data[i][2] ?>';
        document.getElementById('div3').innerHTML='<?=data[i][3] ?>';
        document.getElementById('div4').innerHTML='<?=data[i][4] ?>';
        document.getElementById('div5').innerHTML='<?=data[i][5] ?>';
        document.getElementById('div6').innerHTML='<?=data[i][6] ?>';
        document.getElementById('div7').innerHTML='<?=data[i][7] ?>';
        document.getElementById('div8').innerHTML='<?=data[i][8] ?>';
        document.getElementById('div9').innerHTML='<?=data[i][9] ?>';
        return false;"/>
        </td>
        <? } ?>
      <? } ?>
    </tr>
  <? } ?>
</table>

</form>
</div>
 <div id="one">
 <form>
 <table>
 <tr>

 <td>ca :</td>
 <td id="div1"></td>
 </tr>
 <tr>
 <td>cb :</td>
 <td id="div2"></td>
 </tr>
 <tr>
 <td>cc:</td>
 <td id="div3"></td>
 </tr>
 <tr>
 <td>cd :</td>
 <td id="div4"></td>
 </tr>
 <tr>
 <td>ce:</td>
 <td id="div5"></td>
 </tr>
 <tr>
 <td>cf:</td>
 <td id="div6"></td>
 </tr>
 <tr>
 <td>cg :</td>
 <td id="div7"></td>
 </tr>
 <tr>
 <td>ch :</td>
 <td id="div8"></td>
 </tr>
 <tr>
 <td>ci:</td>
 <td id="div9"></td>
 </tr>
 </table>
 <input type="submit" name="submit" value="Initiate Feedback" onclick="<?sendMessage(ManagerID,proNo,proName);?>"/>
 </form>
 </div>


  </body>
</html>

I have attached my code.

Upvotes: 0

Views: 56

Answers (1)

Vytautas
Vytautas

Reputation: 2286

arturro is correcct in the question comment. To make an asynchronous call you must use

google.script.run.sendMessage(ManagerID,proNo,proName)

However make note that

<input type="submit" name="submit" value="Initiate Feedback" onclick="google.script.run.sendMessage(ManagerID,proNo,proName)"

will not work because you are doing that within the HTML body. You are better of using a javascript code between <script></script> which would fetch the variables you need and then you can use the google.script.run in order to send out the email.

So you would then have

<script>
    function sender() {
      var ManagerID, proNo, proName;

      //use Document.getElementById in order to populate the variables with
      //what you need from the form

      google.script.run.sendMessage(ManagerID, proNo, proName);
    }
</script>
<input type="submit" name="submit" value="Initiate Feedback" onclick="sender()">

The <script></script> part is supposed to be in your index.html as it is a client side script that will handle the calling of the server side script

I have done something similar and it works fine.

Upvotes: 1

Related Questions