HTML-код:
Код: Выделить всё
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.");
}
Код: Выделить всё
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("Функция вызвана.") никогда не появляется, что указывает на то, что функция не вызывается. При запуске непосредственно в редакторе Apps Script функция работает нормально.
Области применения:
Мой файл 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"
]
Подробнее здесь: https://stackoverflow.com/questions/793 ... script-run