简体   繁体   中英

how to use google spreadsheet as login database in blogger?

I have database in spreadsheet like this

数据库

 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>

How to connect with user and password in spreadheet? I'm sure that this code must be change. but how?

var pass1 = 123;
      var input = document.getElementById("userInput").value;

In Google Sheets, go to Tools > Script editor . Add this function in the Apps Script editor:

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";
}

Go to File > Manage Versions , add a description, and press " Save new version ".

Then, go to Publish > Deploy as API executable , select version 1 in the version drop-down, and choose who's allowed to edit the project. Press " Deploy ", and copy the string underneath " Current API ID ".

(If these options aren't visible, you haven't switched to a GCP project. See switching to a standard GCP project .)

Go to the GCP project on the console, enable Apps Script API , configure OAuth , and create credentials . Generate an OAuth token with the ID you copied, and the required scopes for your script (visible under File > Project properties > Scopes ), and keep this secret.

Finally, setup a request in your javascript project with this request body format .


If all of this is a bit complicated, I know - security concerns make it quite difficult to make something. This quickstart guide and this JavaScript code might help.

Sample

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM