如何使用 Google Apps 脚本检查表单中的值

How to check a value in a form using Google Apps Script

我正在研究 "deploy as a web app - Google Apps Script"。该脚本使用 'html service'。我的示例脚本确实将数据写入 Google Sheet,但我希望在您的帮助下做更多事情。

  1. 我想检查以确保电子邮件值包含有效的电子邮件模式 2。如果数据是有效的电子邮件模式,我只想将数据保存到电子表格。

下面列出了我完成此操作的失败尝试...电子表格密钥无效。

Code.gs低于

var submissioSSKey = '1xjsrUJaPxxxxlpxxtf_hoSYw6tkr4WzbEIHTrB6ysx4';
function doGet() {
   return HtmlService
      .createTemplateFromFile('index')
      .evaluate()
     .setSandboxMode(HtmlService.SandboxMode.IFRAME);

}

function processForm(myForm) {
  var email = myForm.email;
  var ss = SpreadsheetApp.openById(submissioSSKey); 
  var sheet = ss.getSheetByName('Data');

  sheet.getRange(sheet.getLastRow()+1, 1, 1, 2).setValues([[Date(), email]]);  
  }

index.html低于

    <script>
function DataSaved(){    
  var emailPattern = /^[a-zA-Z0-9._]+[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/;
  if (emailPattern.myForm(email) == false) {
         document.getElementById('submitMessage1').innerHTML = "* a valid email is required";
}

  // document.getElementById("myForm").reset();  // Reset

    return false; // Prevent page refresh

};
 </script>

<style>
body {
    background-color: #000;
}

.container {
width:800px;
    margin:0px auto;
    text-align:left;
    padding:15px;
    border:1px dashed #333;
    background-color:#ccc;
}

p {
  color: red;
}

#title {
  font-size: 1.3em;
  line-height: 50%;
  color: #fff;
  text-align: left;
  font-weight: bold;
  margin: 0px;
  margin-bottom: 10px;
}

label {
    color: #fff;
}
</style>

<div class="container">
<br /><div id="title">Simple Form</div><br />
<form id="myForm">
<label>Email</label> <br />
<input type="text"   tabindex="1"  id="email" name="email" size="25" required/>
<div id="submitMessage1"></div><br /><br />
<br /><br />
<input type="button" tabindex="2"  id="Submit" value="Submit"  
  onClick="google.script.run.withSuccessHandler(DataSaved).processForm(this.form)"/>
</form>
</div>

此致,

克里斯

现在,onclick 事件的代码在输入元素中:

<input type="button" tabindex="2"  id="Submit" value="Submit" onClick="google.script.run.withSuccessHandler(DataSaved).processForm(this.form)"/>

在将表单数据发送到服务器之前,不会检查电子邮件格式是否正确。我会改变代码的流程。先调用HTML脚本标签中的函数,如果邮件格式通过,则使用google.script.runAPI将表单数据发送到服务器。

<input type="button" tabindex="2"  id="Submit" value="Submit" onClick="validateEmailFormat(this.form)"/>

脚本标签

<script>
  function validateEmailFormat(argFormObject){
    var email = myForm.email;
    var emailPattern = /^[a-zA-Z0-9._]+[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/;
    if (emailPattern.myForm(email) === false) {
      document.getElementById('submitMessage1').innerHTML = "* a valid email is required";
    } else {
      google.script.run
        .withSuccessHandler(DataSaved)
        .processForm(email)

    };
  };

  function DataSaved() {
    document.getElementById('submitMessage1').innerHTML = "The email was saved"; 
  };
</script>

Code.gs

function processForm(argEmail) {
  var ss = SpreadsheetApp.openById(submissioSSKey); 
  var sheet = ss.getSheetByName('Data');

  sheet.getRange(sheet.getLastRow()+1, 1, 1, 2).setValues([[Date(), argEmail]]);  
}

此代码在将电子邮件发送到服务器之前从脚本标记中的表单对象中获取电子邮件。如果您只需要电子邮件值,则没有必要将整个表单对象发送到服务器。

我的主要概念是使用 Google 的“html 服务”验证 Google Apps 脚本表单数据,并在工作代码中写入 Google 电子表格感谢桑迪。我的工作代码可能对其他人有用,所以我在这里发布代码。您需要将 'submissioSSKey' 值替换为电子表格中的值 url 才能使其生效。您还需要 运行 获取并接受权限。

code.gs 应该是这样的

var submissioSSKey = '1xjsrUJaPxxxxlpxxtf_hoSYw6tkr4WzbEIHTrB6ysx4';
function doGet() {
   return HtmlService
      .createTemplateFromFile('index')
      .evaluate()
     .setSandboxMode(HtmlService.SandboxMode.IFRAME);

}

function processForm(myForm) {
  var email = myForm.email;
  var ss = SpreadsheetApp.openById(submissioSSKey); 
  var sheet = ss.getSheetByName('Data');

  sheet.getRange(sheet.getLastRow()+1, 1, 1, 2).setValues([[Date(), email]]);  
  }

index.html 应该如下所示

<script>

function validateEmailFormat(argFormObject){
    console.log('start')
    var emailEntry = document.forms["myForm"]["email"].value;
  if (emailEntry.length == 0) {
      document.getElementById('submitMessage1').innerHTML = "* a valid email is required";
    } else {      
      google.script.run
        .withSuccessHandler(DataSaved)
       .processForm(myForm)
    }
  };

  function DataSaved() {
    document.getElementById('submitMessage1').innerHTML = "The email was saved"; 
  };

 </script>

<style>
body {
    background-color: #000;
}

.container {
width:800px;
    margin:0px auto;
    text-align:left;
    padding:15px;
    border:1px dashed #333;
    background-color:#ccc;
}

p {
  color: red;
}

#title {
  font-size: 1.3em;
  line-height: 50%;
  color: #fff;
  text-align: left;
  font-weight: bold;
  margin: 0px;
  margin-bottom: 10px;
}

label {
    color: #fff;
}
</style>

<div class="container">
<br /><div id="title">Simple Form</div><br />
<form id="myForm">
<label>Email</label> <br />
<input type="text"   tabindex="1"  id="email" name="email" size="25" required/>
<div id="submitMessage1"></div><br /><br />
<br /><br /> 
  <input type="button" tabindex="2"  id="Submit" value="Submit" onClick="validateEmailFormat(this.form)"/>
</form>
</div>