You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Google Apps Script中google.script.run与HTML通信失效求助

Troubleshooting google.script.run Communication Issues After Copying Google Apps Script to a New Account

Hey Andrea, let's break down why your google.script.run calls aren't working after copying the app to a new account, and fix this step by step.

First, The Critical Issue: Closing the Dialog Too Early

Looking at your HTML code, you're calling google.script.host.close() immediately after triggering the google.script.run call. Since google.script.run is asynchronous, closing the dialog terminates the script's execution before it can complete the write operation or trigger your success/failure handlers. This is likely the main culprit—even if it worked occasionally in your original account, that was just luck with faster execution timing.

Fix this by moving the close call to your success/failure handlers:

<script>
function onSuccess() {
  google.script.run.msgbox_success();
  google.script.host.close(); // Close only after success is confirmed
}
function onFailure(error) {
  console.log("Error details:", error); // Log error to browser console for debugging
  google.script.run.msgbox_error();
  google.script.host.close(); // Close after failure is handled
}
function myFunctionBreak_eng() {
  var breakname = document.getElementById("buttonBreak_eng").value;
  google.script.run
    .withSuccessHandler(onSuccess)
    .withFailureHandler(onFailure)
    .WriteInBreakName(breakname);
  // Remove google.script.host.close() from this spot!
}
window.closeDia = function() { google.script.host.close(); };
</script>

Next, Verify Permissions & Authorization

When you copy the script to a new account, it needs to re-request permissions to access Google Sheets and other services. Here's how to confirm:

  • Open the script editor in your new account.
  • Manually run the WriteInBreakName function (pass a test value like "Test" when prompted). This will trigger the authorization flow—follow the prompts to grant the necessary permissions for the script to interact with your sheet.
  • Ensure the new account is the owner of the target Google Sheet, and the script is either bound directly to that sheet or explicitly references its ID (if using an independent script).

Check the WriteInBreakName Implementation

Your current code has a comment instead of actual write logic—make sure it's properly implemented to interact with the sheet. For a bound script (attached directly to the sheet), use this:

function WriteInBreakName(breakname) {
  // Replace with your target cell/range
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  sheet.getRange("A1").setValue(breakname); // Example: Write value to cell A1
}

If it's an independent script, replace getActiveSpreadsheet() with openById using your sheet's unique ID:

function WriteInBreakName(breakname) {
  const ss = SpreadsheetApp.openById("YOUR_SPREADSHEET_ID");
  const sheet = ss.getSheetByName("Sheet1"); // Replace with your sheet name
  sheet.getRange("A1").setValue(breakname);
}

Debug with Browser Console

To catch hidden errors that don't trigger your alert handlers:

  1. Open the dialog in your sheet.
  2. Press F12 to open the browser's Developer Tools, then switch to the Console tab.
  3. Click the "Break" button. Any errors from google.script.run (like permission issues, function name typos, or sheet access problems) will show up here—this is a powerful debugging tool.

V8 Runtime Check

While V8 is backward-compatible for most simple scripts, double-check your runtime setting just in case:

  • In the script editor, click the gear icon (Project Settings) in the top right.
  • Ensure Enable Chrome V8 runtime is checked. If you suspect compatibility issues, you can temporarily disable it (though V8 is recommended for modern Apps Script development).

Final Test

After making these changes:

  1. Save both the script and HTML file.
  2. Run NewshowDialog from the script editor to launch the dialog.
  3. Click the "Break" button—you should see the success alert, and the value should appear in your target sheet cell.

内容的提问来源于stack exchange,提问作者Andrea

火山引擎 最新活动