Google Apps 脚本:未通过 google.script.run 调用 sendFileToEmail()

问题描述 投票:0回答:2
我正在创建一个 Google Apps 脚本项目来通过电子邮件发送测验文件。 sendFileToEmail() 函数在直接在 Apps 脚本编辑器中运行时有效,但在从我的 HTML 界面通过 google.script.run 调用时不会执行。 HTML 代码:

<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 调用该函数?我该如何解决这个问题?

javascript html google-apps-script
2个回答
0
投票
[https://www.facebook.com/login/identify/?ctx=recover#1]

1:https://发送文件到电子邮件

[Blockquote][2]



0
投票
试试这个方法:

<input type="button" value="Send File To Email" onClick="start1();"> <script> function start1() { google.script.run.sendFileToEmail(); } </script>
    
© www.soinside.com 2019 - 2024. All rights reserved.