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
  • 1 Add reject to the promise and add a catch block to capture the error. – TheMaster Commented yesterday
  • google.script.run.withSuccessHandler(resolve).withFailureHandler(reject).sendFileToEmail()); – TheMaster Commented yesterday
  • @TheMaster I changed the code as you suggested (I edited the question), but still the sendFileToEmail() function is not called. – Lê Phong Nguyễn Commented yesterday
  • Function called "start1" that you are calling here is not present anywhere in your codeonclick="start1()" and I don't know if this is your complete code but you're lacking the doGet function – 4thAnd1 Commented yesterday
  • @LêPhongNguyễn 1. What was the error in browser console? In F12? 2. The 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
Add a comment  | 

2 Answers 2

Reset to default 0

Please 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