我的WebApp,其中withSuccessHandler返回未定义

时间:2018-06-23 19:53:03

标签: javascript iframe google-apps-script

我需要帮助来解决这个问题。在收到错误的请求结果之前,一切似乎都可以正常工作。该项目的想法是一个Web App,其中用户为namedate of birth获得2个字段。填充它们后,通过在电子表格中检入相关信息来验证信息。如果一切正常,则程序接下来将使用getSheetId()获取适当的ID,并生成<iframe>代码以进行访问,并应使用HtmlService将其返回。但是,某些原因使其返回undefined

Code.gs

function doGet() {
  Logger.log("Login is loading")
  return HtmlService
      .createTemplateFromFile('index')
      .evaluate()
      //.setSandboxMode(HtmlService.SandboxMode.IFRAME);   
}

function evaluate(name, date) {
  var t = SpreadsheetApp.openById('1nQxfaQcNdM6S1roJs6gxTaZPtM5gOublch5jVKDhkho')
      .getSheetByName('key');
  Logger.log(t.getSheetId());
  Logger.log(name);
  Logger.log(date);
  var v = t.getRange("B2:D200");
  var i = 0;
  for (i = 1; i < 200; i++) {
    if (v.getCell(i, 2).getValue() == name) {
      Logger.log(v.getCell(i, 2).getValue());
      Logger.log(name & " = the name");
      Logger.log(new Date(v.getCell(i,3).getValue()).getTime());
      var pdate = date.split(".");
      Logger.log(new Date(pdate[2],pdate[1],pdate[0]).getTime());
      var t = v.getCell(i,3).getValue();
      Logger.log(pdate);
      Logger.log(t.getFullYear());
      Logger.log(t.getMonth());
      Logger.log(t.getDate());
      if (new Date(t.getFullYear(),t.getMonth()+1,t.getDate()).getTime() == new Date(pdate[2],pdate[1],pdate[0]).getTime()) {
        Logger.log("match found");
        include(v.getCell(i,1).getValue());
        break;
      } else {
        Logger.log("Bad date");
        return "<h2>Wrong Input. 404.</h2>";}
      }
  }
  if (i >= 199) {
    Logger.log("Bad name");
    return "<h2>Wrong Input. 404.</h2>";
  }  
}

var hhtml = "";

function include(name) {
  var html = '<iframe src="https://docs.google.com/spreadsheets/d/e/2PACX-1vTeFjXOSsBRAKjbFLUSBGZOXtmjZO_4RtxxrQtXbk9sxZkF5Kdjs9OIs0tSQwekjYbOTn7JJ-_iCdeD/pubhtml?gid='
  var t = SpreadsheetApp.openById('1nQxfaQcNdM6S1roJs6gxTaZPtM5gOublch5jVKDhkho').getSheetByName(name);
  if (t == null) {
    Logger.log("Page not found");
    return "<h1>Wrong Input. 404.</h1>" 
  } else {
    html += t.getSheetId();
  }
  Logger.log(name);

  html += '&amp;single=true&amp;widget=true&amp;headers=false" width="80%" height="600"></iframe>'

  html += '<br><br><br><h3>Рейтинг групи</h3><iframe src="https://docs.google.com/spreadsheets/d/e/2PACX-1vRF1MobEoKdxvO_SopGTvl-WzqEQ3nQXd6Jo_a7RTAg09yluO32AClwd4krWnVHXGQllPPwOsDeYYzN/pubhtml?gid=2096708929&amp;single=true&amp;widget=true&amp;headers=false" width="80%" height="600"></iframe>'
  Logger.log(html);
  hhtml = HtmlService.createHtmlOutput(html).getContent();
  return HtmlService.createHtmlOutput(html).getContent();        
}

function getHtmlCode() {
  return hhtml;
}

index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
  </head>
  <body>

    <div id="mainForm">
    <h2> Будь-ласка введіть своє прізвище українською мовою та дату народження у форматі день.місяць.рік (наприклад: 22.06.2018) </h2>
            <form>
                <div>
                    <div class="inline form-group">
                        <label for="name">Прізвище</label>
                        <input type="text" id="nameInput" style="width: 150px;">
                    </div>
                    <div class="inline form-group">
                        <label for="date">Дата Народження</label>
                        <input type="text" id="dateInput" style="width: 150px;">
                    </div>
                </div>

                <button class="action" type="button" id="submitButton">Submit</button>
            </form>
         </div>

    <style>
        .hidden {
            display:none;
        }
        .form-group {
            margin: 2px 0px;
        }

        #submitButton {
            margin: 4px 0px;
        }

        body {
          margin-left: 50px;
        }

    </style>


    <script>

$(function(){
  console.log('startup')
  $('#submitButton').on('click', function(){
    console.log("data get");

    function respondent(value)
    {
    $('#mainForm').toggleClass('hidden');
      console.log("script is success");
      console.log(value);
      document.getElementById('Response').innerHTML = value
    }

    google.script.run.withSuccessHandler(respondent).evaluate(document.getElementById('nameInput').value, document.getElementById('dateInput').value);

  })
})

    </script>


    <div id="Response">

    <h2>Waiting for data...</h2>



    </div>

  </body>
</html>

1 个答案:

答案 0 :(得分:0)

清理代码格式后,看起来您只是在evaluate()错误情况下从Wrong Input返回了一个值。否则不返回任何内容。

也许您打算在evaluate()的末尾添加一行:

return include(name);

此外,hhtml全局变量和getHtmlCode()函数在Apps脚本的上下文中没有用。每个执行都在一个单独的实例中,没有任何状态被延续。这意味着getHtmlCode()将始终返回一个空字符串。