Returning a value using Date picker in HTMLService / Google Apps Script

前端 未结 1 931
忘了有多久
忘了有多久 2020-12-20 02:55

I\'m trying to capture a date range that will be used inside a Document container-based GAS. I\'ve been successful displaying a dialog box that shows two jquery datepicker

相关标签:
1条回答
  • 2020-12-20 03:29

    Prepare a server-side function to receive your input. This one only logs it:

    function submitDates(startDate,endDate) {
      Logger.log(JSON.stringify(arguments));
      // To send error messages, throw an exception.
      // e.g. if (invalid) throw new error("Invalid date")
    }
    

    Change the button handler in your html. Instead of closing the dialog, collect the input data and pass it to the server function using google.script.run. Attach handler to the runner; a success handler will close the window, for example. A failure handler will display server-side errors.

    <input type="button" value="Create" onclick="submitDates()" />
    

    Add this script to the bottom of your html:

    <script>
    // Pass input dates to server-side submitDates()
    function submitDates() {
      var startDate = $("#startdatepicker").val();
      var endDate = $("#enddatepicker").val();
    
      google.script.run
            .withSuccessHandler(
               // Dates delivered, close dialog
               function() {
                 google.script.host.close();
               })
               // Display failure messages
             .withFailureHandler(
               function() {
                 var div = $('<div id="error" class="error">' + msg + '</div>');
                 $(element).after($("#demo"));
               })
             .submitDates(startDate,endDate);
    }
    
    </script>
    
    0 讨论(0)
提交回复
热议问题