You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Apps Script Web App从电子表格校验数据后向HTML返回布尔值的结果传递问题

How to Capture Boolean Result from 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

  1. Async Operations Wait for the Callback: Any code that needs to use the isLoginValid result must run inside the onSuccess function (or be triggered from it). You can't use the variable immediately after calling google.script.run because the server hasn't responded yet.
  2. withSuccessHandler is Essential: This method is how you receive data back from the server. Removing it would mean you never get the boolean result at all.
  3. Fix Undefined Variables: Your original code used inputs and formControl which weren't defined—explicitly targeting elements avoids runtime errors.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 07:09:08