用户登录的GAS脚本,无法从电子表格中获取正确的值

时间:2015-11-20 08:36:12

标签: google-apps-script

我想使用Google Apps脚本(GAS)在访问以后的GAS程序/脚本之前验证用户。包含用户名和密码列表的电子表格,以及单元格“B2”中的简单公式,有效用户评估为“1”或无效用户评估为“2”,用于从使用Script_A创建的条目中获取数据。

Script_A& B已从“http://www.the-art-of-web.com/javascript/validate-password/”采用并修改,但有以下更改:(i)“POST”已更改为“GET” - 原因是我无法让脚本在按预期“发布”,(ii)添加一个功能以访问/将用户数据放入电子表格中。从Script_A调用Script_B。

Script_B访问电子表格以获得在单元格“B2”中评估的结果。问题 - 注意到从单元格“B2”获得的值总是始终为Script_A所做的更改,即没有得到正确的值:'1'或'2',因为它应该是

注 1.当在Script_A中单击“提交”时,观察到电子表格值发生变化 2.如果Script_B独立于Script_A运行,它会正确地从电子表格中获取单元格“B2”中的值(1或2,直接输入)。

需要帮助 (1)通过Script_B获取电子表格中的预期值的适当代码,在Script_A中单击“提交”,然后在Script_B中单击“继续”, (2)使用适当的代码,如果使用'POST'而不是'GET'用于两个脚本。

Script_A Code.gs

function doGet() { 
var html = HtmlService.createTemplateFromFile('html').evaluate()
  .setTitle('Login Page')
  .setSandboxMode(HtmlService.SandboxMode.IFRAME);
return html;
}

function checkAllow(myForm){    
  var mV1 = myForm.username;
  var mV2 = myForm.pwd1;
  var mV = mV1 + mV2;     
  var lock = LockService.getPublicLock();      
  lock.waitLock(30000);  // wait 30 seconds before conceding defeat.              
  var ss =SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxx/edit#gid=0')
      .getSheets()[0];
  var mX = ss.getRange("C2");
  mX.setValue(mV);
  var mY = ss.getRange("B1");
  var mZ = mY.getValue();
  var mA = ss.getRange("B2");
  mA.setValue(mZ);
  lock.releaseLock();
  return SpreadsheetApp;      
}

Script_A 中将Html.HTML

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>

<form id="myForm" method="GET" action="">
<p>Username: <input id="field_username" title="Username must not be blank  and contain only letters, numbers and underscores." type="text" required pattern="\w+" name="username"></p>
<p>Password: <input id="field_pwd1" title="Password must contain at least 6 characters, including UPPER/lowercase and numbers." type="password" required pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}" name="pwd1"></p>
<p>Confirm Password: <input id="field_pwd2" title="Please enter the same Password as above." type="password" required pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}" name="pwd2"></p>
<p><input type="submit" value="Submit"></p>
</form> 

<script type="text/javascript">

document.addEventListener("DOMContentLoaded", function() {

// JavaScript form validation

var checkPassword = function(str)
{
  var re = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}$/;
  return re.test(str);
};

var checkForm = function(e)
{
  if(this.username.value == "") {
    alert("Error: Username cannot be blank!");
    this.username.focus();
    e.preventDefault(); // equivalent to return false
    return;
  }
  re = /^\w+$/;
  if(!re.test(this.username.value)) {
    alert("Error: Username must contain only letters, numbers and underscores!");
    this.username.focus();
    e.preventDefault();
    return;
  }
  if(this.pwd1.value != "" && this.pwd1.value == this.pwd2.value) {
    if(!checkPassword(this.pwd1.value)) {
      alert("The password you have entered is not valid!");
      this.pwd1.focus();
      e.preventDefault();
      return;
    }
  } else {
    alert("Error: Please check that you've entered and confirmed your password!");
    this.pwd1.focus();
    e.preventDefault();
    return;
  }

  google.script.run.withSuccessHandler("myForm")
   .checkAllow(document.forms[0]);    
   document.getElementById("myForm").action="https://script.google.com/macros/s/xxxxxxxxxxxxxxx/exec"; // run Script_B
  };

var myForm = document.getElementById("myForm");
myForm.addEventListener("submit", checkForm, true);

// HTML5 form validation

var supports_input_validity = function()
{
  var i = document.createElement("input");
  return "setCustomValidity" in i;
}

if(supports_input_validity()) {
  var usernameInput = document.getElementById("field_username");
  usernameInput.setCustomValidity(usernameInput.title);

  var pwd1Input = document.getElementById("field_pwd1");
  pwd1Input.setCustomValidity(pwd1Input.title);

  var pwd2Input = document.getElementById("field_pwd2");

  // input key handlers

  usernameInput.addEventListener("keyup", function() {
    usernameInput.setCustomValidity(this.validity.patternMismatch ? usernameInput.title : "");
  }, false);

  pwd1Input.addEventListener("keyup", function() {
    this.setCustomValidity(this.validity.patternMismatch ? pwd1Input.title : "");
    if(this.checkValidity()) {
      pwd2Input.pattern = this.value;
      pwd2Input.setCustomValidity(pwd2Input.title);
    } else {
      pwd2Input.pattern = this.pattern;
      pwd2Input.setCustomValidity("");
    }
  }, false);

  pwd2Input.addEventListener("keyup", function() {
    this.setCustomValidity(this.validity.patternMismatch ? pwd2Input.title : "");
  }, false);

}

}, false);

</script>
</body>
</html>

Script_B Code.gs

function doGet() {
var html = HtmlService.createTemplateFromFile('html01').evaluate()
  .setTitle('User Validation')
  .setSandboxMode(HtmlService.SandboxMode.IFRAME);
return html;  
}

function getData() {
return SpreadsheetApp
  .openByUrl("https://docs.google.com/spreadsheets/d/xxxxxxxxxxxx/edit#gid=0")
  .getSheets()[0]
  .getRange("b2")
  .getValue();
}

Script_B html01.html

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
   <form id="myFrm" method="GET" action="">
       <p><input type="submit" value="Continue"></p>
   </form>  

   <script type="text/javascript">


   document.addEventListener("DOMContentLoaded", function() {
   // JavaScript form validation
   var chkForm = function(e) {

   <? var mD = getData(); ?> 
   var mT = <?= mD ?>;
   if (mT==1){
       alert(mT + " username and password are valid!");
       document.getElementById("myFrm").action="https://script.google.com/macros/s/xxxxxxxxxxxxxxxxxxx/exec"; // for valid user, run intended script

     } 
     else{ 
        alert(mT + " INVALID username and/or password!");
         document.getElementById("myFrm").action="https://script.google.com/macros/s/xxxxxxxxxxx/exec"; // invalid user, run Script_A  
     }
    return;
};
var mForm = document.getElementById("myFrm");
mForm.addEventListener("submit", chkForm, true);
}, false);    
</script>        
</body>
</html>

0 个答案:

没有答案