Returning a value using Date picker in HTMLService

2020-02-13 04:37发布

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 objects using Serge's great example in this post: Date picker in HTMLService / Google Apps Script . . ...but, how can I return the two date values back to my GAS code from the html logic?

Thanks in advance!

function onOpen() {
  var ui = DocumentApp.getUi();
  ui.createMenu('Create Calendar')
      .addItem('Provide Date Range', 'showDialog')
      .addToUi();
}

function showDialog() {
  var html = HtmlService.createHtmlOutputFromFile('dateDialog')
      .setWidth(500)
      .setHeight(400);
  DocumentApp.getUi() 
      .showModalDialog(html, 'Please provide a Date Range');
  Logger.log("HTML return = %s", html.getContent());     // What does html contain?
}

==================== dateDialog.html ======================

<div class="demo" >
<style type="text/css"> .demo { margin: 30px ; color : #AAA ; font-family : arial sans-serif ;font-size : 10pt } 
                            p { color : red ; font-size : 11pt } 
</style>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/themes/cupertino/jquery-ui.css">

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>

<p>Please select a date below :</p>

<p> Start Date : <input type="text" name="StartDate" id="startdatepicker" /> </p>
<p> End Date :   <input type="text" name="EndDate" id="enddatepicker" /> </p>
<script>
    $( "#startdatepicker" ).datepicker({
      showWeek: true,
      firstDay: 0,
     });
</script>
<script>
    $( "#enddatepicker" ).datepicker({
      showWeek: true,
      firstDay: 0,
     });
</script>
<input type="button" value="Close" onclick="google.script.host.close()" />
</div>

1条回答
Emotional °昔
2楼-- · 2020-02-13 05:04

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>
查看更多
登录 后发表回答