简体   繁体   中英

JavaScript not changing DOM in my HTML Template after load in Apps Script

为什么我的第二行定位 div id="load1" 没有执行

I am trying to build a simple dashboard where if user access their dashboard it will show data from the google spreadsheet. But There is one problem I don't know why only my first div is getting target via JavaScript not the second one..

any Help and solution would apricated..

Why My Second Line Targeting div id="load1" is not executing????

Code.gs

`function loadDetails(id){
   var ss = SpreadsheetApp.openById("14RM_170AefKbQq82NXAdSOemQLyAGcv_BTwYWd1-qtM")
   var ws = ss.getSheetByName("Data")
   var data = ws.getDataRange().getValues()
     for(var i=0; i<data.length; i++){
         var row = data[i]
        if(row[0]== id){
           password = {
             psd: row[1],
             id: row[0]
            }
         }
      }
      if(row[0]!= id){
          password = "not found"
    }
      return password;
 }`

Dashboard.HTML

<!DOCTYPE html>
  <html>

  <head>
  <base target="_top">
  </head>

<body>
   <h1>Hey this is other Employee Dashboard</h1>
   <p>Your Employee Id: = </p>
   <p>Your Password : = </P>
   <div id="load">
   <div>
   <div id="load1">
   <div>

<script>
     window.onload = function(){
        var id = "MN00018"
        google.script.run.withSuccessHandler(function(output){
        document.getElementById("load").innerHTML = `passing objects are ${output.psd} & ${output.id}`
        document.getElementById("load1").innerHTML = `passing objects are ${output.id}`
    }).loadDetails(id)
   }
</script>

</body>
</html>

If you are actually testing your showing script, I think that there are 2 modification points.

HTML & Javascript side:

The tag of div is not enclosed. How about modifying as follows?

From:

<div id="load">
<div>
<div id="load1">
<div>

To:

<div id="load">
</div>
<div id="load1">
</div>
  • I think that this is the reason of your issue of Why My Second Line Targeting div id="load1" is not executing???? .

Google Apps Script side:

At Google Apps Script side, by if(row[0]!= id){password = "not found"} , even when id is found from the column "A", "not found" is returned. How about modifying as follows?

Modified script:

function loadDetails(id) {
  var ss = SpreadsheetApp.openById("###");
  var ws = ss.getSheetByName("Data")
  var data = ws.getDataRange().getValues()
  var password = {psd: "", id: ""};
  for (var i = 0; i < data.length; i++) {
    var row = data[i]
    if (row[0] == id) {
      password = {
        psd: row[1],
        id: row[0]
      }
      break;
    }
  }
  return password;
}

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