Search code examples
javascripthtmlgoogle-apps-scriptweb-applicationsclient-server

How to pass several parameters in a google.script.run() onClick command in HTML


I'm trying to execute a Google Script function called send Email with the parameters cc, bcc, subject, and body, as follows

CODE.GS:

function sendEmail(cc, bcc , subject, body){
 Logger.log("Reached sendEmail");
 MailApp.sendEmail(cc, subject, body, {
 bcc: bcc
 });

 }

HTML:

<!DOCTYPE html>
    <html>
    <head>
    <base target="_top">
     </head>
     <body> 
     <div class = "formfield">
       <textarea id= "cc-input-box" placeholder = "CC" rows="1" cols="80"><?= email ?></textarea>
       <textarea id= "bcc-input-box" placeholder = "BCC" rows="1" cols="78"></textarea>
       <textarea id= "subject-input-box" placeholder = "Subject" rows="1" cols="78"><?= subject ?></textarea>
       <textarea id="body-input-box" placeholder = "Body"rows="34" cols="78"><?= body ?></textarea>
     </div>
     <input type="button" value="Submit" 
     onclick="google.script.run.sendEmail(document.getElementById('cc-input-box') , document.getElementById('bcc-input-box') , document.getElementById('subject-input-box') , document.getElementById('body-input-box')); google.script.host.close();" />
     <input type="button" value="Cancel" onclick="google.script.host.close();" 
     />
      </body>
      </html>

I'm trying to pass all the respective TextAreas as parameters using document.getElementById(). However, it doesn't seem to be working. I don't know how to pass several parameters through onClick, essentially.


Solution

  • Send the element's value instead of the element itself. DOM elements are illegal as parameters between client and server(except a single form element).

    google.script.run.sendEmail(document.getElementById('cc-input-box').value, /*...*/)