Hàm không thực thi trong dịch vụ HTML script của google
Tôi đang viết mã một công cụ cho bản thân và các bạn cùng lớp sẽ tạo tài liệu google và gửi nó qua email cho giáo viên đã chọn, sử dụng tất cả các trường được điền và điền vào tất cả các trường không có giá trị mặc định cho môn học đã chọn, chẳng hạn như môn ngữ văn, nhưng chức năng lấy thông tin đã chọn và sử dụng nó để gửi email không thực thi. Tôi đã kiểm tra các lần thực thi cho dự án và hàm customDoc()chưa một lần được thực thi. Tôi nghi ngờ đó là vấn đề với HTML của mình, vì tôi không thấy bất kỳ thông báo lỗi nào khi tôi kiểm tra hàm trong trình chỉnh sửa để xem có bất kỳ lỗi cú pháp nào không, nhưng nó hoàn toàn sạch sẽ, chỉ là chưa bao giờ được thực thi. Đây là mã của tôi và mặc dù có thể xảy ra lỗi trong HTMl, tôi cũng sẽ cung cấp JS của mình.
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 = ['[email protected]', 'math for ']
var LA = ['[email protected]', 'la for ']
var science = ['[email protected]', 'science for ']
var is = ['[email protected]', 'I&S for ']
var span = ['[email protected]', '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>
Trả lời
Tóm lại, customDoc()là một chức năng máy chủ và bạn cần sử dụng google.script.runđể yêu cầu Apps Script chạy một chức năng máy chủ cụ thể. Vì vậy, thay vì gọi onclick="customDoc(this.id)", hãy thử onclick="google.script.run.customDoc(this.id)". Không bao gồm Code.gs trong tệp HTML của bạn vì đó là mã phía máy chủ và nó sẽ không hoạt động. Tôi thực sự khuyên bạn nên đọc hướng dẫn Giao tiếp từ Máy khách đến Máy chủ .
customDoc()Chức năng của bạn là một câu chuyện khác :) Dưới đây là một cách rất đơn giản để tổ chức lại các cài đặt trước khác nhau của bạn (ví dụ: chủ thể) bằng cách sử dụng các đối tượng . Tôi cũng đã thay thế mã định dạng ngày của bạn bằng Utilities.formatDate(), mã này có thể dễ hiểu hơn một chút.
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>
Hãy thử chạy ở trên và nhấp vào nút khoa học. Bạn sẽ nhận được một nhật ký thực thi như:
Today: 10/30/2020
Subject: science
{preSubject=science for , [email protected]}
Bây giờ điều đó customDoc()đang thực sự được thực thi, bạn có thể bắt đầu cố gắng sửa lỗi tạo Google Tài liệu. Đối với tôi, dường như bạn đang tạo một Tài liệu Google trống hoàn toàn, có thể không phải là thứ bạn muốn. Tôi nghĩ bạn cần phải làm việc thêm một chút nữa và sau đó quay lại nếu bạn có câu hỏi cụ thể hơn về việc tạo tài liệu. Chúc may mắn!