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

Google Apps Script调用google.script.run返回undefined问题求解

问题原因

google.script.run 是异步调用接口,调用服务端函数时不会阻塞等待结果返回,直接赋值拿到的返回值固定为undefined,服务端的执行结果只能通过成功回调函数获取。

修复方案

通过withSuccessHandler绑定调用成功后的回调函数,服务端返回的listVlans会作为参数传入该回调,所有依赖返回值的逻辑(打印、渲染下拉选项等)都要写在回调内部。

修改后的前端代码示例:

<script>
    function getList() {
      google.script.run
        // 绑定成功回调,参数为服务端return的结果
        .withSuccessHandler(listVlans => {
          console.log(listVlans);
          // 后续更新下拉列表的逻辑写在此处
          // 示例:拆分字符串生成选项
          const vlanOptions = listVlans.split(',');
          const select = document.querySelector('#替换为你的下拉列表ID');
          select.innerHTML = '';
          vlanOptions.forEach(vlan => {
            const option = document.createElement('option');
            option.value = vlan;
            option.textContent = vlan;
            select.appendChild(option);
          });
        })
        // 可选:绑定失败回调处理异常
        .withFailureHandler(error => {
          console.error('获取VLAN列表失败:', error);
        })
        // 最后调用服务端函数
        .returnList();
    }
</script>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.09 23:57:01