I am working on a "deploy as a web app - Google Apps Script". The script uses the 'html service'. My sample script does write data to a Google Sheet but I hope to do more with your help.
- I want to check to make sure the email value contains a valid email pattern 2. I want to only save data to the spreadsheet if it is a valid email pattern.
My failed attempt to accomplish this is listed below... the spreadsheet key is not valid.
Code.gs below
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 below
<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>
Regards,
Chris