Function not executing in google script HTML service

前端 未结 1 1526
面向向阳花
面向向阳花 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]);
    }
    
    
    
      
        

    CREATE DOC

    Email

    Doc name

    Subject

    message

    Fill blanks for subject:

    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)
提交回复
热议问题