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: '[email protected]',
preSubject: 'math for '
},
'la': {
email: '[email protected]',
preSubject: 'la for '
},
'science': {
email: '[email protected]',
preSubject: 'science for '
},
'is': {
email: '[email protected]',
preSubject: 'I&S for '
},
'spanish': {
email: '[email protected]',
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 protected]}
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!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…