简体   繁体   English

Google Apps脚本函数返回html中未定义的

[英]Google apps script function returns undefined in html

I am building an add-on for google docs (Just for practice) that will act like email. 我正在为Google文档构建一个插件(仅供练习),其作用类似于电子邮件。 I already incorporated sending, receiving, deleting, and viewing messages. 我已经合并了发送,接收,删除和查看消息。 I added the code needed for a UI modal dialog, but one of the functions is only returning undfined . 我添加了UI模态对话框所需的代码,但是其中一个功能仅返回undfined I tested this function in the code.gs file, and it worked perfectly. 我在code.gs文件中测试了此功能,并且运行良好。 Here is a section of 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;
    }
  }
}

Here is the html file: 这是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>

Update: I completely retyped the functions, but the program continues to return undefined. 更新:我完全重新键入了函数,但是程序继续返回未定义。 All inputs are the correct values, and the function returns information correctly in a ui.alert() box. 所有输入都是正确的值,并且该函数在ui.alert()框中正确返回信息。

I figured it out after completely reading the Google Apps Script Documentation. 我在完全阅读了Google Apps脚本文档后才弄清楚了。 The google.script.run.function() API does not return a value . google.script.run.function() API 不返回value In order to fetch data from a script, you must have the script generate raw HTML, and create a dialog with an HTML string. 为了从脚本中获取数据,您必须让脚本生成原始HTML,并创建带有HTML字符串的对话框。

Due to security considerations, scripts cannot directly return HTML to a browser. 出于安全考虑,脚本无法直接将HTML返回给浏览器。 Instead, they must sanitize it so that it cannot perform malicious actions. 相反,他们必须对其进行清理,以使其无法执行恶意操作。 You can return sanitized HTML using the createHtmlOutput API 您可以使用createHtmlOutput API返回经过清理的HTML

function doGet() {
  return HtmlService.createHtmlOutput('<b>Hello, world!</b>');
}

The code in the HtmlOutput can include embedded JavaScript and CSS. HtmlOutput中的代码可以包含嵌入式JavaScript和CSS。 (This is standard client-side JavaScript that manipulates the DOM, not Apps Script). (这是操作DOM的标准客户端JavaScript,而不是Apps脚本)。 All of this content is sanitized using Google Caja, which applies some limitations to your client-side code. 所有这些内容都使用Google Caja进行了清理,这对您的客户端代码施加了一些限制。 For more information, see the guide to restrictions in HTML service. 有关更多信息,请参见HTML服务限制指南。

https://developers.google.com/apps-script/reference/html/html-output# https://developers.google.com/apps-script/reference/html/html-output#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM