<button id="reStartButton" disabled="disabled" onclick="start1()">Send files to your email</button>
<script>
try {
var newVal = await new Promise((resolve, reject) => {
google.script.run
.withSuccessHandler(resolve)
.withFailureHandler(reject) // Thêm reject để xử lý lỗi
.sendFileToEmail();
});
console.log("Email sent successfully:", newVal);
loinhac2.innerHTML = "Đề thi và đáp án đã được gửi đến email của bạn, hãy lấy chúng về và in ra giấy.";
} catch (error) {
console.error("Error sending email:", error);
alert("An error occurred while sending the email. Please try again later.");
}
</script>
应用程序脚本代码:
function sendFileToEmail() {
Logger.log("Function called.");
var myEmail = Session.getActiveUser().getEmail();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var solId = ss.getSheetByName('title').getRange('A77').getValue();
var sol = UrlFetchApp.fetch(`https://docs.google.com/spreadsheets/d/${solId}/export?format=xlsx&id=${solId}`, {
method: "GET",
headers: { "authorization": "Bearer " + ScriptApp.getOAuthToken() }
}).getBlob();
MailApp.sendEmail(myEmail, 'Quiz', 'See attached.', { attachments: [sol] });
}
问题:
Logger.log("Function called.") 消息永远不会出现,表明该函数未被调用。当直接在 Apps 脚本编辑器中运行时,该功能运行良好。适用范围: 我的 appsscript.json 包括:
"oauthScopes": [
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/script.send_mail",
"https://www.googleapis.com/auth/gmail.send"
]
什么会阻止 google.script.run 调用该函数?我该如何解决这个问题?
<input type="button" value="Send File To Email" onClick="start1();">
<script>
function start1() {
google.script.run.sendFileToEmail();
}
</script>