2017-09-26 61 views
0

我已经创建了一个包含数据的Google Web应用程序 - 单击此存储桶文本时,弹出式窗口中的工单号应该获得副本。将TEXT复制到剪贴板上Button-Click不工作表

复制脚本表第一排,第二排开始它不工作

我附上下面的脚本工作正常,请大家帮我解决这个问题

function doGet() { 
 
    return HtmlService 
 
     .createTemplateFromFile('index') 
 
    .evaluate().setTitle("Home Page").setSandboxMode(HtmlService.SandboxMode.IFRAME); 
 
    return html; 
 
} 
 

 
function getData() { 
 
    return SpreadsheetApp 
 
     .openById('1Nf080XKVLtt2AWr469aJrh6vhjdeinxBP1ehV3qBA64') 
 
     .getDataRange() 
 
     .getValues(); 
 
}

 
<div class="w3-container"> 
 
    <h2>Open Requsets</h2> 
 

 
    <input class="w3-input w3-border w3-padding" type="text" placeholder="Search for names.." id="myInput" onkeyup="myFunction()"> 
 
    <? var data = getData(); ?> 
 
    <table class="w3-table-all w3-margin-top " id="myTable"> 
 
    <tr class="w3-pink"> 
 
     <th style="width:10%;">Requset ID</th> 
 
     <th style="width:10%;">Requester Name</th> 
 
     <th style="width:10%;">Requset Type</th> 
 
    </tr> 
 
     <? for (var i = 0; i < data.length; i++) { ?> 
 
     <tr> 
 
      <? for (var j = 0; j < data[i].length; j++) { ?> 
 
      <td><button onclick="copy('demo')"><a href="" target="_blank"><span id="demo"><?= data[i][j] ?></span></a></button> </td> 
 
      <? } ?> 
 
     </tr> 
 
     <? } ?> 
 
    </table> 
 
</div> 
 
    
 

 
    
 
    
 
    <script> 
 
    function copy(element_id){ 
 
    var aux = document.createElement("div"); 
 
    aux.setAttribute("contentEditable", true); 
 
    aux.innerHTML = document.getElementById(element_id).innerHTML; 
 
    aux.setAttribute("onfocus", "document.execCommand('selectAll',false,null)"); 
 
    document.body.appendChild(aux); 
 
    aux.focus(); 
 
    document.execCommand("copy"); 
 
    document.body.removeChild(aux); 
 
} 
 
    
 
    </script> 
 
    
 
    <hr> 
 

 

 

 

 
    
 
<!-- for Accordion --> 
 

 
<!-- for Accordion end--> 
 
</body> 
 
</html>

回答

2

您应该为每个td字段使用唯一的id值,如下面的示例所示。您不能对每个td使用相同的id

<div class="w3-container"> 
 
    <h2>Open Requsets</h2> 
 

 
    <input class="w3-input w3-border w3-padding" type="text" placeholder="Search for names.." id="myInput" onkeyup="myFunction()"> 
 
    <? var data = getData(); ?> 
 
    <table class="w3-table-all w3-margin-top " id="myTable"> 
 
     <tr class="w3-pink"> 
 
     <th style="width:10%;">Requset ID</th> 
 
     <th style="width:10%;">Requester Name</th> 
 
     <th style="width:10%;">Requset Type</th> 
 
     </tr> 
 
     <? for (var i = 0; i < data.length; i++) { ?> 
 
     <tr> 
 
      <? for (var j = 0; j < data[i].length; j++) { ?> 
 
      <td><button onclick="copy(event)"><a href="" target="_blank"><span><?= data[i][j] ?></span></a></button> </td> 
 
      <? } ?> 
 
     </tr> 
 
     <? } ?> 
 
    </table> 
 
</div> 
 

 

 

 

 
<script> 
 
    function copy($event) { 
 
    var aux = document.createElement("div"); 
 
    aux.setAttribute("contentEditable", true); 
 

 
    //Notice how we retreived the innerHTML 
 
    aux.innerHTML = $event.target.innerHTML; 
 

 
    aux.setAttribute("onfocus", "document.execCommand('selectAll',false,null)"); 
 
    document.body.appendChild(aux); 
 
    aux.focus(); 
 
    document.execCommand("copy"); 
 
    document.body.removeChild(aux); 
 
    }

+0

获得一些HTML错误 格式错误的HTML内容:​​<按钮的onclick = “拷贝( 'demo_' + I + '_' + j)的”>。 (第3行,“代码”文件) – KiKu

+0

嗯..你用什么语言进行动态模板化 – Thusitha

+0

基本上你应该改变每个td的id。你的模板语言应该为你提供一些选项来动态地生成元素的id。 – Thusitha