HTML-Code:
Code: Select all
Send files to your email
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.");
}
Code: Select all
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] });
}
Die Meldung Logger.log("Funktion aufgerufen.") wird nie angezeigt, was darauf hindeutet, dass die Funktion nicht aufgerufen wurde. Bei direkter Ausführung im Apps Script-Editor funktioniert die Funktion einwandfrei.
Umfang:
Meine appsscript.json enthält:
Code: Select all
"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"
]