Google Apps Script Web App从电子表格校验数据后向HTML返回布尔值的结果传递问题
google.script.run for Further Use I see exactly where you're stuck here—let's break this down clearly. The core issue is that google.script.run works asynchronously, so you can't just assign its return value directly to a variable like you would with a regular synchronous function. You shouldn't remove withSuccessHandler—it's actually the correct way to get your server-side result. Here's how to fix this:
Why Your Current Code Isn't Working
When you write var findLogin = google.script.run.withSuccessHandler(onSuccess).searchLogins(ev.target.value);, the findLogin variable isn't receiving the boolean result from your server script. Instead, it's just holding a reference to the google.script.run object, because the server call hasn't finished executing yet. The actual result only gets passed to your onSuccess callback once the server responds.
Fixed Client-Side Script
Let's adjust your code to properly capture and use the result:
// Define a variable to store the validation result (optional, but useful for global access) let isLoginValid = false; // Target the login input explicitly (your original code used undefined 'inputs') document.getElementById('tLogin').addEventListener('keydown', keyPressFunction); function keyPressFunction(ev) { if (ev.code !== 'Enter') return; if (ev.target.id === "tLogin"){ // Trigger the server call, and let the success handler handle the result google.script.run.withSuccessHandler(onSuccess).searchLogins(ev.target.value); console.log('Checking login against server...'); } } function onSuccess(findLogin) { // Assign the server result to our variable isLoginValid = findLogin; console.log('Login validation result:', isLoginValid); // Put ALL logic that depends on this result here if (isLoginValid) { // Login is valid: proceed to the next input field document.getElementById('tTable').focus(); } else { // Login is invalid: show feedback and reset the input alert('Invalid login. Please try again.'); const loginInput = document.getElementById('tLogin'); loginInput.focus(); loginInput.select(); } }
Optional Server-Side Optimization
Your server code works, but we can make it cleaner and more efficient using modern array methods:
function searchLogins(login){ const sheetApp = SpreadsheetApp.getActiveSpreadsheet(); const sheetTo = sheetApp.getSheetByName("SISTEM"); // Convert the 2D range array to a 1D array and check for the login const loginList = sheetTo.getRange("A:A").getValues().flat(); return loginList.includes(login); }
Key Notes to Remember
- Async Operations Wait for the Callback: Any code that needs to use the
isLoginValidresult must run inside theonSuccessfunction (or be triggered from it). You can't use the variable immediately after callinggoogle.script.runbecause the server hasn't responded yet. withSuccessHandleris Essential: This method is how you receive data back from the server. Removing it would mean you never get the boolean result at all.- Fix Undefined Variables: Your original code used
inputsandformControlwhich weren't defined—explicitly targeting elements avoids runtime errors.
内容的提问来源于stack exchange,提问作者Dmitriy Rudakov

