能否将Apps Script返回值(字符串)存为JS变量?调用遇undefined.mp3问题
undefined.mp3 with Your Apps Script + Client-Side JS Setup Hey there, let's walk through the most likely reasons you're seeing undefined.mp3—this almost always ties to how you're handling the gap between server-side Apps Script and client-side JavaScript. Here are the key issues to check:
1. You're Treating Asynchronous Calls Like Synchronous Code
The google.script.run API (used to call server-side Apps Script functions from your HTML) is asynchronous. That means you can't directly assign its result to a variable and use it immediately—by the time you try to append .mp3, the server hasn't sent back the value yet, so the variable is undefined.
Bad (What You Might Be Doing)
// In your Index.html client-side script function handleButtonClick() { let storedValue = google.script.run.getStoredValue(); let audioFile = storedValue + ".mp3"; // storedValue is undefined here console.log(audioFile); // Outputs: undefined.mp3 }
Good (Fix with a Success Handler)
You need to use withSuccessHandler() to wait for the server's response before using the value:
// In your Index.html client-side script function handleButtonClick() { google.script.run .withSuccessHandler(function(returnedValue) { // This code runs ONLY after the server sends back the value let audioFile = returnedValue + ".mp3"; console.log(audioFile); // Now uses the actual stored value }) .getStoredValue(); // Call your server-side function }
2. Your Server-Side Function Isn't Returning a Value
Double-check your Code.gs function—if you forget the return statement, it won't send any data back to the client, so the success handler will receive undefined.
Bad (Missing Return)
// In Code.gs function getStoredValue() { // This reads the property but doesn't send it back! PropertiesService.getUserProperties().getProperty("yourKeyName"); }
Good (Add Return)
// In Code.gs function getStoredValue() { // Return the property value to the client return PropertiesService.getUserProperties().getProperty("yourKeyName"); }
3. The Stored Property Doesn't Exist (Or Is Null)
If the property you're trying to fetch doesn't exist in PropertiesService, getProperty() returns null. While this would result in null.mp3 instead of undefined.mp3, it's still worth checking—you might want to add a fallback value to avoid unexpected results:
// In Code.gs (add a default value) function getStoredValue() { return PropertiesService.getUserProperties().getProperty("yourKeyName") || "default-audio"; }
4. Mismatched Function Names or Scope Issues
Make sure:
- The function name you're calling from the client (
getStoredValue) exactly matches the name inCode.gs(capitalization matters!). - Your server-side function is a top-level function (not nested inside another function)—nested functions aren't accessible via
google.script.run.
Quick Troubleshooting Tip
Add a withFailureHandler() to catch errors and debug what's going wrong:
google.script.run .withSuccessHandler(function(val) { /* ... */ }) .withFailureHandler(function(error) { console.error("Server error:", error); // Logs issues like missing functions }) .getStoredValue();
内容的提问来源于stack exchange,提问作者Matt Knoll

