Google脚本中的Onchange更改表数据 [英] Onchange in Google script to change table data

查看:85
本文介绍了Google脚本中的Onchange更改表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的事情,我想将select的值获取到var lob,这样,如果我选择一个值,它将更改表上的数据:

Here's what I'm trying to do I want to get the value of the select to var lob so that if I selected a value it will change the data on the table:

<select id="mySelect" onchange="myFunction()">
<? var list = SpreadsheetApp
.openById('sheetID')
.getSheetByName("VL Slots")
.getDataRange()
.getValues();
var lane = [1];
?>
<? for (var l = 3; l < list.length; l++) { ?>
<option value="<?= list[l][lane] ?>"><?= list[l][lane] ?> </option>
<?}?>
</select>
<script>
function myFunction() {
var lob = document.getElementById("mySelect").value;
}
</script>
<? var lob; ?>
<table>
<? var data = SpreadsheetApp
.openById('sheetID')
.getSheetByName("VL Request")
.getDataRange()
.getValues();
var rid = [0];
var request = [1];
<? for (var i = 1; i < data.length; i++) { ?>
<tr>
<? if (data[i][rid] === lob) { ?>
<td>data[i][request] </td>
<? } ?>
</tr>
<? } ?>
</table>

我希望有人能帮助我正确地做事.

I hope someone help me on how to do it right.

推荐答案

  • 您正在使用脚本.根据文档:
    • You are using scriptlets. As per documentation:
    • 由于scriptlet代码在投放页面之前执行,因此可以 每页只运行一次;

      Because scriptlet code executes before the page is served, it can only run once per page;

      这意味着,如果您的电子表格数据发生更改,则html表将不会包含更改.

      This means that if your spreadsheet data changes, your html table will not incorporate the change.

      • Apps脚本 onChange 触发器可以运行由文档中的更改触发的Apps脚本代码.但是,我假设您正在部署Web应用程序,除非手动刷新浏览器,否则不会通过onChange触发器来更新WebApp.

      • The Apps Script onChange trigger can run an Apps Script code, fired by a change in the document. However, I assume that you are deploying a Web App, and the WebApp will not be updated by an onChange trigger, unless you refresh your browser manually.

      对于您的情况,最好的选择是使用轮询结合 google.script.run > a>

      For your situation you best bet would be to use polling for refreshing the data in your website in combination with google.script.run

      示例:

      code.gs

      code.gs

      function doGet()
      {
        var html=HtmlService.createTemplateFromFile('index');
        return html.evaluate();
      }
      
      function getSelect() {
        var list = SpreadsheetApp.openById('spreadssheetID').getSheetByName("VL Slots").getDataRange().getValues();
        var lane = 1;
        var select="";
        for (var l = 3; l < list.length; l++) {
          select+='<option value="' + list[l][lane] + '">'+ list[l][lane] + ' </option>';
        }
        return select;
      }
      function getTable(lob) {
        var data = SpreadsheetApp.openById('spreadssheetID').getSheetByName("VL Request").getDataRange().getValues();
        var rid = 0;
        var request = 1;
        var table="";
        table+='<tr>';
        for (var i = 1; i < data.length; i++) { 
          if (data[i][rid] == lob) {
            table+='<td>' + data[i][request] + '</td>';
          }
        }
        table+='</tr>';
        return  table;
      }
      
      

      index.html

      index.html

      <!DOCTYPE html>
      <html>
        <head>
          <base target="_top">
        </head>
        <script>
        function populateSelect(){
          google.script.run.withSuccessHandler(onSuccess).getSelect();
         }
        function onSuccess(select){
          document.getElementById("mySelect").innerHTML=select;
        }
        function polling(){
          setInterval(myFunction,2000);
         }
        function myFunction(){
          var lob = document.getElementById("mySelect").value;
          google.script.run.withSuccessHandler(onSuccess2).getTable(lob);    
      
        }
        function onSuccess2(table){
          document.getElementById("myTable").innerHTML=table;
        }
        </script>
        <body onload="populateSelect()">
          <select id="mySelect" onchange="polling()">
          </select>
          <table id="myTable">
          </table>
        </body>
      </html>
      

      setInterval(myFunction,2000);将按需要刷新您的数据 间隔(以毫秒为单位).

      setInterval(myFunction,2000); will refresh your data in desired intervals (in ms).

      这篇关于Google脚本中的Onchange更改表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆