Google 应用程序脚本函数在 html 中返回未定义

红狼计划

我正在为 google docs 构建一个附加组件(仅供练习),它的作用类似于电子邮件。我已经合并了发送、接收、删除和查看消息。我添加了 UI 模式对话框所需的代码,但其中一个函数仅返回undfined. 我在code.gs文件中测试了这个功能,它工作得很好。以下是部分内容code.gs

function onInstall() {
  var html = HtmlService.createHtmlOutputFromFile('Startup').setWidth(350).setHeight(170);
  DocumentApp.getUi().showModalDialog(html, 'New account:');
}

function testCheck() {
  var ui = DocumentApp.getUi();
  ui.alert(checkUsername(ui.prompt('').getResponseText(), ui.prompt('').getResponseText()));
}

function checkUsername(un, em) {
  var i; var a; var is;
  var props = PropertiesService.getScriptProperties();
  if (props.getProperty(un) == null) {
    is = true;
  } else {
    return 'This username is taken!';
  }
  if (em.length == 0) {
    return true;
  } else {
    var len = (em.match(/@/g) || []).length;
    if (len == 1) {
      if (props.getProperty(em) != null) {
        return 'Someone has already registered this email address as ' + props.getProperty(em);
      } else {
        return true;
      }
    } else {
      if (em.indexOf(', ') != -1) {
        em = em.split(', ');
      } else if (em.indexOf('; ') != -1) {
        em = em.split('; ');
      } else if (em.indexOf(' + ') != -1) {
        em = em.split(' + ');
      } else if (em.indexOf(';') != -1) {
        em = em.split(';');
      } else if (em.indexOf(',') != -1) {
        em = em.split(',');
      } else if (em.indexOf('+') != -1) {
        em = em.split('+');
      } else if (em.indexOf(' ') != -1) {
        em = em.split(' ');
      } else {
        return 'Please separate your email addresses with a comma, space, or semicolon.';
      }
      for (i = 0; i < em.length; i++) {
        a = em[i];
        if (props.getProperty(a) != null) {
          return 'Someone has already registered ' + a + ' as ' + props.getProperty(a);
        }
      }
      return true;
    }
  }
}

这是html文件:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
  </head>
  <body>
    Username:<br>
    <input type='text' id='user' style='width:350px' maxlength='12'/><br>
    Other email addresses:<br>
    <textarea id='extras' style='width:350px' rows='2'></textarea><br>
    <span class='error' id='err'></span><br>
    <button class='action' onClick='check()'>Next</button>
    <button onclick='group()'>Groups</button><br>
    <script>
      function check() {
        var un = document.getElementById('user').value;
        var em = document.getElementById('extras').value;
        var fail = document.getElementById('err');
        var is = google.script.run.checkUsername(un, em);
        if (typeof is == 'string') {
          fail.innerHTML = is;
        } else {
          google.script.host.close();
          google.script.run.setAccount(un, em);
        }
      }

      function group() {
        var un = document.getElementById('user').value;
        var em = document.getElementById('extras').value;
        var is = google.script.run.checkUsername(un, em);
        if (typeof is == 'boolean') {
          setGroupAddress(un, em);
        } else {
          document.getElementById('err').innerHtml = is;
        }
      }
    </script>
  </body>
</html>

更新:我完全重新输入了函数,但程序继续返回未定义。所有输入都是正确的值,函数在一个ui.alert()框中正确返回信息

红狼计划

我在完全阅读了 Google Apps 脚本文档后想通了。google.script.run.function()API不返回值为了从脚本中获取数据,您必须让脚本生成原始 HTML,并使用 HTML 字符串创建一个对话框。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Google Apps Script - 将应用程序脚本函数的输出返回到 html 文件 javascript 函数

从Google应用程序脚本中的dropdownlist值运行函数

Google应用程序脚本中的JSON .concat()函数

如何遍历数组并将其传递给Google应用程序脚本中的HTML模板

Google表格中GOOGLEMAPS自定义应用程序脚本函数的数组输入

HTML模板中的未定义函数

在Google Apps脚本中调用Key:Value对返回“未定义”而不是值

在应用程序脚本返回值之前加载 HTML 并触发函数

Google脚本-for in loop语句返回未定义的值

Google Apps脚本配额如何用于网络应用程序中的doGet和doPost函数?

我的 Google 应用程序脚本中的“TypeError:sheet.getMaxColumns 不是函数”

Google App Script - 函数返回未定义

Google Maps-从未定义的函数返回getPosition

Google地址解析函数返回未定义

如何在 Angular 应用程序的 html 块中查看脚本函数值?

谷歌地图错误-ReferenceError:Google在ionic 3应用程序中未定义

google脚本中未定义google-错误

使用 <a href=""> 链接到 google 应用程序脚本中的其他 html 文件时链接断开

如何确保在 google 应用程序脚本中执行下一行之前完成 google.script.run 函数

Google Apps脚本中的document.getElementById“未定义”

需要Google表格中的简单replaceInCell应用程序脚本

在 Google 应用程序脚本中执行节点 JS 代码

函数将未定义的函数传递给Google Apps脚本中的被调用函数

带有反应模板的电子伪造应用程序的index.html中的“未捕获ReferenceError:需求未定义”

Google表格自定义函数有时会返回未定义的值

在 HTML 中创建 div 时,它返回未定义

从 html google 应用程序脚本的表范围中排除列

HTML 移动应用程序 / Google API

Google Firebase错误(函数返回未定义,预期的承诺或价值)