admin管理员组文章数量:1122846
I'm creating a Google Apps Script project to send quiz files via email. The sendFileToEmail() function works when run directly in the Apps Script editor, but it doesn't execute when called through google.script.run from my HTML interface. HTML Code:
<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>
Apps Script Code:
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(`/${solId}/export?format=xlsx&id=${solId}`, {
method: "GET",
headers: { "authorization": "Bearer " + ScriptApp.getOAuthToken() }
}).getBlob();
MailApp.sendEmail(myEmail, 'Quiz', 'See attached.', { attachments: [sol] });
}
Issue: The Logger.log("Function called.") message never appears, suggesting the function isn't called. When run directly in the Apps Script editor, the function works fine.
Scopes: My appsscript.json includes:
"oauthScopes": [
";,
";,
".send_mail",
".send"
]
What could prevent google.script.run from invoking the function? How can I resolve this?
I'm creating a Google Apps Script project to send quiz files via email. The sendFileToEmail() function works when run directly in the Apps Script editor, but it doesn't execute when called through google.script.run from my HTML interface. HTML Code:
<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>
Apps Script Code:
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] });
}
Issue: The Logger.log("Function called.") message never appears, suggesting the function isn't called. When run directly in the Apps Script editor, the function works fine.
Scopes: My appsscript.json includes:
"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"
]
What could prevent google.script.run from invoking the function? How can I resolve this?
Share Improve this question edited yesterday Wicket 37.9k9 gold badges77 silver badges189 bronze badges asked yesterday Lê Phong NguyễnLê Phong Nguyễn 971 silver badge6 bronze badges 5 |2 Answers
Reset to default 0Please remember that only the executions of functions run from the Editor are shown on the bottom panel of the editor page. All the execution logs from the last seven days are available on the Execution Logs page.
On Google Apps Script web apps using the Google Cloud default project, logs are printed to the execution log for deployments set to run as the project owner and set to allow access only to them.
On projects that allow anyone to access, the logs are not printed to the execution log. Related issue: Google Apps Script logs (web app) don't show up in the new interface.
The more straightforward workaround is to set a custom logging handler, i.e., to print the logs to a spreadsheet. Another option is to use Cloud logging.
Try it this way:
<input type="button" value="Send File To Email" onClick="start1();">
<script>
function start1() {
google.script.run.sendFileToEmail();
}
</script>
本文标签: javascriptsendFileToEmail() not being called via googlescriptrunStack Overflow
版权声明:本文标题:javascript - sendFileToEmail() not being called via google.script.run - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736283708a1927056.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
reject
to the promise and add acatch
block to capture the error. – TheMaster Commented yesterdaygoogle.script.run.withSuccessHandler(resolve).withFailureHandler(reject).sendFileToEmail());
– TheMaster Commented yesterdaysendFileToEmail()
function is not called. – Lê Phong Nguyễn Commented yesterdayonclick="start1()"
and I don't know if this is your complete code but you're lacking the doGet function – 4thAnd1 Commented yesterdayThe Logger.log("Function called.") message never appears, suggesting the function isn't called.
No, it doesn't suggest that. Logs maybe missing sometimes in webapp. You should check "executions" to see if some function ran and at what time.. – TheMaster Commented yesterday