【问题标题】:How to call a modal dialog (with HTML file) from a sidebar (also HTML) with google scripts如何使用谷歌脚本从侧边栏(也是 HTML)调用模式对话框(带有 HTML 文件)
【发布时间】:2020-03-11 13:11:17
【问题描述】:

我需要一点关于如何使某些按钮起作用的知识。

我在一个 HTML 文件上创建了一个侧边栏,该文件从谷歌应用程序上的电子表格中的一个按钮调用。这是函数:

function Sidebar() {
  var Form = HtmlService.createTemplateFromFile("Sidebar");
  var ShowForm = Form.evaluate();

  ShowForm.setTitle("Sidebar");
  SpreadsheetApp.getUi().showSidebar(ShowForm);
}

这是我的 Sidebar.html 文件:

<!DOCTYPE html>
  <html>
     <head>
        <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize-icons.css">      
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
     </head>

    <!--WELCOME CARD-->

    <body style="background-color:#424242">
    <div class="card grey darken-2" style="top:3px">
        <div class="card-content white-text">
          <span class="card-title" style="font-size:20px;font-weight:bold;color:#80cbc4">MANAGER</span>
          <p style="font-size:14px">Welcome to the manager, choose an option below to start!</p>
        </div>
    </div>


    <!--BUTTONS-->

    <div class="fixed-action-btn" style="bottom: 45px; right: 10px;">
       <a class="btn-floating btn-large teal">START</a>
       <ul>
          <li><a class="btn-floating waves-effect waves-light teal darken-3 tooltipped" id= "btn1" data-position="left" data-tooltip="Button 1"><i class="material-icons">format_list_numbered</i></a></li>
          <li><a class="btn-floating waves-effect waves-light teal darken-3 tooltipped" id= "btn2" data-position="left" data-tooltip="Button 2"><i class="material-icons">assessment</i></a></li>
          <li><a class="btn-floating waves-effect waves-light teal darken-3 tooltipped" id= "btn3" data-position="left" data-tooltip="Button 3"><i class="material-icons">search</i></a></li>
          <li><a class="btn-floating waves-effect waves-light teal darken-3 tooltipped" id= "btn4" data-position="left" data-tooltip="Button 4"><i class="material-icons">add_box</i></a></li>
       </ul>
    </div>


  <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
  <script>
  $(document).ready(function(){
    $('.tooltipped').tooltip();
    $('.fixed-action-btn').floatingActionButton();
  });
  </script>

    </body>
  </html>

我已经创建了所有按钮(它是一个固定的操作按钮,显示了另外 4 个实际按钮)并且在视觉上工作,但我不知道如何让它们中的每一个都为模态对话框调用一个新的 html.file。 . 我已经为我的 .gs 文件中的每个按钮编写了函数,但不能让实际的按钮调用这些函数。这是我的整个 .gs 文件:

/** @OnlyCurrentDoc */


function Sidebar() {
  var Form = HtmlService.createTemplateFromFile("Sidebar");
  var ShowForm = Form.evaluate();

  ShowForm.setTitle("Sidebar");
  SpreadsheetApp.getUi().showSidebar(ShowForm);
}

function btn1(){
  var Form = HtmlService.createTemplateFromFile("btn1");
  var ShowForm = Form.evaluate();

  ShowForm.setTitle("btn1").setHeight(400).setWidth(1000);
  SpreadsheetApp.getUi().showModalDialog(ShowForm, "btn1");
}

function btn2(){
  var Form = HtmlService.createTemplateFromFile("btn2");
  var ShowForm = Form.evaluate();

  ShowForm.setTitle("btn2").setHeight(400).setWidth(1000);
  SpreadsheetApp.getUi().showModalDialog(ShowForm, "btn2");
}

function btn3(){
  var Form = HtmlService.createTemplateFromFile("btn3");
  var ShowForm = Form.evaluate();

  ShowForm.setTitle("btn3").setHeight(400).setWidth(1000);
  SpreadsheetApp.getUi().showModalDialog(ShowForm, "btn3");
}

function btn4(){
  var Form = HtmlService.createTemplateFromFile("btn4");
  var ShowForm = Form.evaluate();

  ShowForm.setTitle("btn4").setHeight(400).setWidth(1000);
  SpreadsheetApp.getUi().showModalDialog(ShowForm, "btn4");
}

如果有人能提供一些关于如何做到这一点的提示,我将非常感激.. 提前致谢!

  • 我正在使用 Materialize CSS。
  • 我的侧边栏是通过电子表格上的按钮调用的。
  • 一些印刷品: Sidebar // Buttons unfolded

【问题讨论】:

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


    【解决方案1】:

    您可以使用google.script.run 函数来实现。

    在以下示例中,您可以看到它仅适用于一个按钮,但对所有按钮都一样。

    /* Code.gs */
    
    function Sidebar() {
      var Form = HtmlService.createTemplateFromFile("Sidebar");
      var ShowForm = Form.evaluate();
    
      ShowForm.setTitle("Sidebar");
      SpreadsheetApp.getUi().showSidebar(ShowForm);
    }
    
    function btn1(){
      // Display a modal dialog box with custom HtmlService content.
      var htmlOutput = HtmlService
      .createHtmlOutput('<p>My modal content here...</p>')
      .setWidth(250)
      .setHeight(300);
      SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Btn 1 modal title');
    }
    

    您希望在单击边栏中的btn1 图标时调用函数btn1。 要做到这一点并仍然使用物化图标,您可以将&lt;input&gt; 标记更改为&lt;button&gt; 并将图标放置在其中,如下所示:

    <!-- Sidebar.html -->
    
    <!DOCTYPE html>
    <html>
    
    <head>
        <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    </head>
    
    <!--WELCOME CARD-->
    
    <body style="background-color:#424242">
        <div class="card grey darken-2" style="top:3px">
            <div class="card-content white-text">
                <span class="card-title" style="font-size:20px;font-weight:bold;color:#80cbc4">MANAGER</span>
                <p style="font-size:14px">Welcome to the manager, choose an option below to start!</p>
            </div>
        </div>
    
        <!--BUTTONS-->
    
        <div class="fixed-action-btn" style="bottom: 45px; right: 10px;">
            <a class="btn-floating btn-large teal">START</a>
            <ul>
                <li>
                    <button id="btn1" data-position="left" data-tooltip="Button 1" class="btn-floating waves-effect waves-light teal darken-3 tooltipped" value="format_list_numbered" onclick="google.script.run.withUserObject(this).btn1()"><i class="material-icons">format_list_numbered</i></button>
                </li>
            </ul>
        </div>
    
        <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
        <script>
            $(document).ready(function() {
                $('.tooltipped').tooltip();
                $('.fixed-action-btn').floatingActionButton();
            });
        </script>
    
    </body>
    
    </html>
    

    注意按钮的 onclick 属性

    onclick="google.script.run.withUserObject(this).btn1()"


    您可以使用 google.script.run 从客户端调用 .gs 脚本中的函数。

    注意:

    您也可以从文件中创建 htmlOutput。相应地更改btn1函数中的代码。

    function btn1(){
      // Display a modal dialog box with custom HtmlService content.
      var htmlOutput = HtmlService
      .createHtmlOutputFromFile('btn1')
      .setWidth(250)
      .setHeight(300);
      SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Btn 1 modal title');
    }
    

    这里是btn1.html的例子

    <p>This is HTML generated from file...</p>
    

    查看实际操作:

    延伸阅读:

    【讨论】:

    • onclick 属性有什么需要更改的吗?我已经为你的按钮切换了,并在 .gs 文件中添加了该功能,但是当我单击按钮时仍然没有发生任何事情..
    • 这可能是您的btn1 函数。请注意它与您的不同。由于您没有提供 btn1.html 模板
    • 这就是我所做的:在您的 btn1 函数中,我已更改:“.createHtmlOutput('

      My modal content here...

      ')”为“.createHtmlOutputFromFile(” btn1")" 然后创建了一个 btn1.html 文件。我已将电子表格中的一个按钮分配给此功能以对其进行测试并使其正常运行。只是当我尝试从侧边栏中的按钮调用它时它不起作用..知道为什么吗?似乎它应该工作正常..
    • 您的 btn1.html 中可能有错误。在浏览器检查器中检查 JS 控制台以获取错误消息。我添加到答案中,以便您可以看到它是从文件中呈现的。如果对您有用,请记住接受答案,如果您觉得有用,请点赞。谢谢
    • 每次我点击按钮时,我都会在浏览器控制台中看到“PERMISSION_DENIED”,所以我尝试删除谷歌帐户设置的权限,所以我可以再次授权,但即使我确实,控制台中仍然出现相同的错误..不知道发生了什么 kkkk
    【解决方案2】:

    试试google.script.run.AnyServerSideFunctionName()

    Client to Server Communications

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-06
      相关资源
      最近更新 更多