函数没有从jquery谷歌应用程序脚本调用

时间:2016-01-29 07:05:00

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

所以我有一个功能,当我运行时工作正常,因为我可以看到它输出数据到日志,现在我想让它更友好,并通过单击按钮调用该功能。我无法从ui中获得功能,我缺少什么?请帮忙。这是我的代码。

function showDialog()
          {
            var ui = HtmlService.createTemplateFromFile('Show')
            .evaluate().setWidth(300).setHeight(400);
            SpreadsheetApp.getUi().showModalDialog(ui, "Select a folder");
          }
    function generateFolderTree() {
      Logger.log("generateFolderTree called");
      try {


        var parentFolder = DriveApp.getRootFolder();

        getChildFolders(parentFolder);

      } catch (e) {

        Logger.log(e.toString());

      }

    }


    function getChildFolders(parent) {

      var childFolders = parent.getFolders();

      while (childFolders.hasNext()) {

        var childFolder = childFolders.next();

        Logger.log("Folder Name: " + childFolder.getName());

        var fold = childFolder.getName();
       }
    }




 Show.html

    <!-- USe a templated HTML printing scriphlet to import common stylesheet. -->
        <?!= HtmlService.createHtmlOutputFromFile("Stylesheet").getContent(); ?>
        <!-- Use a templated HTML printing scriptlet to import JavaScript. -->

        <div>
        <div class = "block" id = "dialog-elements">
        <button class = "selectFolder" id = "selectFolder" >Select a Folder</button>
        </div>
        <!-- This block is going to be hidden until the user selects a folder -->
        <div class = "block" id = "hiddenAttrib">
        <p><label for = "SelectedFolder"> Selected Folder: </label></p>
        <p><label id = "foldername"> Folder Name: </label></p>
        <p><label id = "folderid"> Folder ID: </label></p>
        </div>
        <div class = "folderTable" id = "folderTable">
        <p><label class = "showStatus" id = "dialog-status">Status: </label></p>
        <table style = "width:100%" id = "source">
        <!--<tr>
           <td> <button>Awesome</button></td>
        </tr>-->


        </table>


        </div>

        </div>
        <!-- Use a templated HTML printing scriptlet to import JavaScript. -->
        <?!= HtmlService.createHtmlOutputFromFile('ShowJavaScript').getContent(); ?>

jquery的

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
    $(function(){
    $("button").click(runMyFunction);
    });
    function runMyFunction(){

    //this.disabled = true;
    google.script.run
    .withSuccessHandler(generateFolderTree)
    .withFailureHandler(showError)
     $("#hiddenAttrib").hide();
        var counter = 4;
        var i = 0;
        for (i = 0; i<counter; i++){
          var row = $("<p><tr><td><button>This is a test</button></td></tr></p>");
          $("#source").append(row.html());
    }
    }

    function showError(error) {
        console.log(error);
        window.alert('An error has occurred, please try again.');
      }
    </script

&GT;

1 个答案:

答案 0 :(得分:0)

在你的'jquery'脚本中,你没有调用该函数。

调用它的模式应该是:

google.script.run
.withSuccessHandler(successCallback)
.withFailureHandler(errorCallback)
.appsscriptFunction()

所以对于你的情况应该是:

google.script.run
.withSuccessHandler(successCallback)
.withFailureHandler(showError)
.generateFolderTree()

您在withSuccessHandlerwithFailureHandler中使用的函数是客户端中的函数,与函数showError相同,但您还需要一个函数来调用成功的。

您可以在此document中看到这一点。