【问题标题】:fill sidebar form from sheet从工作表中填写侧边栏表单
【发布时间】:2016-11-28 12:59:27
【问题描述】:

在谷歌表格中,我想在侧边栏中填写一个表格,其中包含活动表格中的值。由于我不能使用单击单元格触发器,因此我想使用一个函数,该函数在单击侧边栏中的按钮时获取活动单元格的值。 我写了这段代码,但不幸的是它没有工作,

我是 Apps 脚本的新手,所以请原谅我的低级代码 :) 谢谢 !

test_sidebar.html

    <!DOCTYPE html>
<html>
  <head>
    <base target="_top">
   <script>

 function tSubmit(form){

      google.script.run.withSuccessHandler(updateT).getT(form);
 }

 function updateT(cellValue){
 var div=document.getEelementById('T');
 div.innerHTML='<input name="T" size = 10 value=' + cellValue + '>K'
 }

   </script>
  </head>
  <body>
  <form>
    <font color="red">Select conditions of interest:</font><p>
<ul><input value=T type="Button" onClick="tSubmit(this)"><div id="T"> = <input name="T" size = 10>K</div></ul>
<ul><i>P</i> = <input name="P" size = 10>bar</ul>
</form>
  </body>
</html>

code.gs:

function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .createMenu('Custom Menu')
      .addItem('Show sidebar', 'showSidebar')
      .addToUi();
}

function showSidebar() {
  var html = HtmlService.createHtmlOutputFromFile('test_sidebar')
      .setTitle('My custom sidebar')
      .setWidth(300);
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .showSidebar(html);
}

  function getT(form){
   var cellValue= SpreadsheetApp.getActiveSheet().getActiveCell().getValue();
  return cellValue;
   }

【问题讨论】:

    标签: html google-apps-script google-sheets


    【解决方案1】:

    您的 html 文件中有错字。去掉 getElementByID 中多余的 e 即可:

    var div=document.getElementById('T');
    

    另外,没有必要尝试传递表单,这也会导致问题,所以只需删除这些部分。您的最终代码将是:

    test_sidebar.html

    <!DOCTYPE html>
    <html>
      <head>
        <base target="_top">
       <script>
    
     function tSubmit(){
    
          google.script.run.withSuccessHandler(updateT).getT();
     }
    
     function updateT(cellValue){
     var div=document.getElementById('T');
     div.innerHTML='<input name="T" size = 10 value=' + cellValue + '>K'
     }
    
       </script>
      </head>
      <body>
      <form>
        <font color="red">Select conditions of interest:</font><p>
    <ul><input value=T type="Button" onClick="tSubmit()"><div id="T"> = <input name="T" size = 10>K</div></ul>
    <ul><i>P</i> = <input name="P" size = 10>bar</ul>
    </form>
      </body>
    </html>
    

    code.gs:

    function onOpen() {
      SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
          .createMenu('Custom Menu')
          .addItem('Show sidebar', 'showSidebar')
          .addToUi();
    }
    
    function showSidebar() {
      var html = HtmlService.createHtmlOutputFromFile('test_sidebar')
          .setTitle('My custom sidebar')
          .setWidth(300);
      SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
          .showSidebar(html);
    }
    
    function getT(){
      var cellValue= SpreadsheetApp.getActiveSheet().getActiveCell().getValue();
      return cellValue;
    }
    

    【讨论】:

      【解决方案2】:

      顺便说一句,您可以使用图像触发功能,并将该图像链接到功能。从单元格触发某些东西的“技巧”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-26
        • 2019-11-11
        相关资源
        最近更新 更多