问题描述
我在电子表格中有这样的数据库
var myTimer;
function clock() {
myTimer = setInterval(myClock,1000);
var c = 10;
var pass1 = 123;
var input = document.getElementById("userInput").value;
function myClock() {
if (input == pass1) {
document.getElementById( 'login' ).style.display = 'none'}}}
<div id="login">
<center>
<form id="form" onsubmit="return false;">
<i class="fa fa-lock"></i>
<input type="password" id="userInput" />
<br />
<input type="submit" onclick="clock()" value="Start" id="popUpYes"/>
<!---<button onclick="clearInterval(myTimer)">Stop counter</button>--->
</form>
</center>
</div>
如何在Spreadheet中使用用户名和密码连接?我确定必须更改此代码。但是如何?
var pass1 = 123;
var input = document.getElementById("userInput").value;
解决方法
在Google表格中,转到工具> 脚本编辑器。在Apps脚本编辑器中添加此功能:
function getPass(user) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var pwr = ss.getRange("SheetName!A1:B100"); //Put sheet (tab) name here
for (var i = 0; i < pwr.getNumRows(); i++) {
if (pwr.getCell(i,1).getDisplayValue() == user) return pwr.getCell(i,2).getDisplayValue();
}
return "No user found";
}
转到文件> 管理版本,添加描述,然后按“ 保存新版本”。
然后,转到发布> 作为API可执行文件部署,在版本下拉列表中选择版本1,然后选择允许谁编辑项目。按“ 部署”,然后在“ 当前API ID ”下面复制字符串。
(如果这些选项不可见,则您尚未切换到GCP项目。请参见switching to a standard GCP project。)
转到控制台上的GCP项目enable Apps Script API,configure OAuth和create credentials。使用您复制的ID以及脚本所需的作用域(在 File > Project properties > Scopes 下可见)生成OAuth令牌,并保守秘密。
最后,使用this request body format在您的javascript项目中设置一个请求。
如果所有这些都有些复杂,我知道-安全问题使制造某些东西变得相当困难。 This quickstart guide和this JavaScript code可能会有帮助。
,-
您可以创建一个Apps Script WebApp,其中包含同一项目中的服务器端Apps脚本代码和客户端html / Javascript代码。
-
因此,google.script.run和Apps Script scriptlets之类的接口允许这两部分之间的通信。
样本
code.gs:
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
function logPassword(password) {
var sheet = SpreadsheetApp.openById("SPREADSHEETID").getSheetByName("SHEETNAME");
sheet.appendRow([password])
}
index.html:
<html>
<head>
<base target="_top">
</head>
<body>
<div id="login">
<center>
<form id="form" onsubmit="return false;">
<i class="fa fa-lock"></i>
<input type="password" id="userInput" />
<br />
<input type="submit" onclick="clock()" value="Start" id="popUpYes"/>
<!---<button onclick="clearInterval(myTimer)">Stop counter</button>--->
</form>
</center>
</div>
<script>
...
function clock() {
var input = document.getElementById("userInput").value;
google.script.run.logPassword(input);
...
}
...
</script>
</body>
</html>