Function not executing in google script HTML service

前端 未结 1 1525
面向向阳花
面向向阳花 2020-12-22 02:42

I am coding a tool for myself and my classmates that will create a google doc and email it to the selected teacher, using all fields that are filled in and filling all those

相关标签:
1条回答
  • 2020-12-22 02:56

    In short, customDoc() is a server function and you need to use google.script.run to tell Apps Script to run a specific server function. So instead of calling onclick="customDoc(this.id)", try onclick="google.script.run.customDoc(this.id)". Don't include Code.gs in your HTML file as that's server-side code and it won't work. I highly recommend reading the Client-to-Server Communication guide.

    Your customDoc() function is another story :) Below is a very simple way to reorganize your different presets (e.g. subjects) using objects. I also replaced your date formatting code with Utilities.formatDate(), which may be a little easier to comprehend.

    function doGet() {
      return HtmlService.createHtmlOutputFromFile('Index.html');
    }
    
    function customDoc(subject) {
      var subjects = {
        'math': {
          email: 'teacher@example.com',
          preSubject: 'math for '
        },
        'la': {
          email: 'teacher@example.com',
          preSubject: 'la for '
        },
        'science': {
          email: 'teacher@example.com',
          preSubject: 'science for '
        },
        'is': {
          email: 'teacher@example.com',
          preSubject: 'I&S for '
        },
        'spanish': {
          email: 'teacher@example.com',
          preSubject: 'Español para '
        }
      };
    
      var formattedDate = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'M/d/yyyy');
      
      console.log('Today: ' + formattedDate);
      console.log('Subject: ' + subject);
      console.log(subjects[subject]);
    }
    
    <!DOCTYPE html>
    <html>
      <body>
        <h1>CREATE DOC</h1>
        <p>Email</p>
        <input type="text" id="Email" value=" " style="border-radius: 20px; border-color: crimson; border-width: 20px" />
        
        <p style="font-family: Times New Roman, Times, serif">Doc name</p>
        <input type="text"  id="docName" value=" " style="border-radius: 20px; border-color: crimson; border-width: 20px" />
        
        <p>Subject</p>
        <input type="text" id="Sub" value=" " style="border-radius: 20px; border-color: crimson; border-width: 20px" />
        
        <p>message</p>
        <input type="text" id="message" value=" " style="border-radius: 20px; border-color: crimson; border-width: 20px" />
    
        <h2>Fill blanks for subject:</h2>
    
        <button id="la" onclick="google.script.run.customDoc(this.id)">LA</button>
        <button id="science" onclick="google.script.run.customDoc(this.id)">Science</button>
        <button id="is" onclick="google.script.run.customDoc(this.id)">Individuals and societies</button>
        <button id="spanish" onclick="google.script.run.customDoc(this.id)">Spanish</button>
        <button id="math" onclick="google.script.run.customDoc(this.id)">math</button>
      </body>
    </html>
    

    Try running the above and clicking the science button. You should get an execution log like:

    Today: 10/30/2020
    Subject: science
    {preSubject=science for , email=teacher@example.com}
    

    Now that customDoc() is actually executing, you can start trying to fix the Google Doc generation. It seems to me that you're creating a completely blank Google Doc, which probably isn't what you want. I think you need to work on it some more and then come back if you have more specific questions about generating the document. Good luck!

    0 讨论(0)
提交回复
热议问题