如何从Google Apps脚本中的网页中获取元素?

时间:2016-04-02 19:57:37

标签: html google-apps-script

我正在使用HTML和Google Apps脚本进行地理测验,我需要从网页上获取答案输入。我尝试获取链接并将元素解析为XML,但它说我必须关闭某种元标记。据我所知,我没有添加任何元标记。这是脚本:

function doGet() {
    var URL = UrlFetchApp.fetch('https://script.google.com/macros/s/AKfycbzxrT9Tf_W080kpYq26exEC5O3NrI-EUOZssckOVow-9_9yKkBW/exec').getContentText();
    var Parse = XmlService.parse(URL);
    var GetRoot = Parse.getRootElement();
    var ParseBack = HtmlService.parse(GetRoot);
    var ansInDiv = getElementById(URL, 'a1', 'a2', 'a3');
    return(ansInDiv);
}

// send
function send() {
    var EmailAddress = "#########@gmail.com";
    MailApp.sendEmail(EmailAddress, "Geo Quiz Answers", doGet());
}

这是HTML:

    <!DOCTYPE html>
<html>
    <title>
        Geography Quiz
    </title>
  <base target="_top">
  <font face="Verdana" size="20px">
    Welcome to the Geography Quiz.
  </font>
  <font face="Verdana">
    Begin whenever you are ready. 
  </font>
  <script>
    function RunSend() {
        google.script.run.send();
    }
  </script>
  <div id="answers">
    <br/>
    <br/>
    <p>
      q1
    </p>
    <input type="text" id="a1">
    <p>
      q2
    </p>
    <input type="text" id="a2">
    <p>
      q3
    </p>
    <input type="text" id="a3">
    <br/>
    <br/>
    <button onclick="RunSend()" value="Submit">
        Submit
    </button>
  </div>
</html>

如何让脚本获取元素并通过电子邮件发送?

提前致谢。

1 个答案:

答案 0 :(得分:0)

你使用的方法对我来说似乎有点奇怪,我会这样做:

function doGet() {
  return HtmlService.createTemplateFromFile('index').evaluate().setTitle('quizz').setSandboxMode(HtmlService.SandboxMode.IFRAME); 
}

function send(result) {
    var EmailAddress = "-------@gmail.com";
    MailApp.sendEmail(EmailAddress, "Geo Quiz Answers", result);
}

并在html中:

<!DOCTYPE html>
<html>
    <title>
        Geography Quiz
    </title>
  <base target="_top">
  <font face="Verdana" size="20px">
    Welcome to the Geography Quiz.
  </font>
  <font face="Verdana">
    Begin whenever you are ready. 
  </font>
  <script>
    function RunSend() {
      var a1 = document.getElementById("a1").value
      var a2 = document.getElementById("a2").value
      var a3 = document.getElementById("a3").value
      var result=a1+"-"+a2+"-"+a3  
      google.script.run.send(result);
    }

  </script>
  <div id="answers">
    <br/>
    <br/>
    <p>
      q1
    </p>
    <input type="text" id="a1">
    <p>
      q2
    </p>
    <input type="text" id="a2">
    <p>
      q3
    </p>
    <input type="text" id="a3">
    <br/>
    <br/>
    <button onclick="RunSend()" value="Submit">
        Submit
    </button>
  </div>
</html>

Demo here