Function not executing in google script HTML service

我是研究僧i 提交于 2020-11-25 03:39:23

问题


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 that are not with the defaults for the selected subject, say language arts, but the function that takes the selected information and uses it to send the email is not executing. I've checked the executions for the project, and the function, customDoc(), has not once been executed. I suspect it's a problem with my HTML, as I have not seen any error messages when I tested the function in the editor to see if there were any syntax errors, but it was spot clean, just never executed. Here's my code, and while the error is likely in the HTMl I will provide my JS too.

function doGet() {
  return HtmlService.createHtmlOutputFromFile('Index.html');
}

function showDialoge() {
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutputFromFile('Index.html'), 'Test');
}

function customDoc(clicked_id) {
  var d = new Date();
  var s = (d.getDate()) + '/' + (d.getMonth() + 1) + '/' + d.getFullYear();
  console.log(s);
  var cycler = clicked_id
  var math = ['esmith3@op97.org', 'math for ']
  var LA = ['cborah@op97.org', 'la for ']
  var science = ['lgrimaldi@op97.org', 'science for ']
  var is = ['charrington@op97.org', 'I&S for ']
  var span = ['vlagioia@op97.org', 'Espanol para ']
  var presets = [math, LA, science, is, span]
  var email1 = document.getElementById('Email')
  var subject1 = document.getElementById('Sub')
  var docName1 = document.getElementById('docName')
  var message1 = document.getElementById('message')
  var email = null
  if (email1 != ' ') {
    email = email1
  } else {
    email = presets[cycler];
    [1];
  }
  var subject = null
  if (subject1 != ' ') {
    subject = subject1
  } else {
    subject = presets[cycler];
    [2]; + s
  }
  var doc = null
  if (docName1 != ' ') {
    doc = docName1
  } else {
    doc = presets[cycler];
    [2]; + s
  }
  var document = documentApp.create(doc)
  var url = document.getUrl();
  var message = null
  if (message1 != ' ') {
    message = message1 + '' + url
  } else {
    message = url
  }

  GmailApp.sendEmail(email, subject, message);
}
<!DOCTYPE html>
<script src="Code.gs"></script>
<html>
<h1>CREATE DOC</h1>

<body>


</body>
<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='2' onclick=c ustomDoc(this.id)> LA </button>
<button id='3' onclick=c ustomDoc(this.id)> Science </button>
<button id='4' onclick=c ustomDoc(this.id)> Individuals and societies </button>
<button id='5' onclick=c ustomDoc(this.id)> Spanish  </button>
<button id='1' onclick=c ustomDoc(this.id)> math </button>


</html>

回答1:


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!



来源:https://stackoverflow.com/questions/64613579/function-not-executing-in-google-script-html-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!