SoftwareTester
SoftwareTester

Reputation: 1090

Select an option while performing a fetch using Google Apps Script

Probably a trivial question for those who know and one that has been asked before (but I can't find it).

A website contains a listbox to select a specific date. By default the date has been set to the next date a meeting will take place.
the next date a meeting will take place

Using

var site     = <a site>
var response = UrlFetchApp.fetch(site);
var data     = response.getContentText();

I can get the content of the page. The page source, amongst a lot of other data, contains

<h1 class="object_title">Vergaderingen gemeenteraad en commissies</h1>
    <!-- Content -->
    <div id="content" class="contentarea">
        <div id="phContent_pnlSelectDatum">

            <div class="formulier-grijs">
                <fieldset>          
                        <div class="rij">
                            <label for="Zoekterm" class="width115">Vergaderdatum</label>
                          <select name="ctl00$phContent$ddlYear" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;ctl00$phContent$ddlYear\&#39;,\&#39;\&#39;)&#39;, 0)" id="phContent_ddlYear">
        <option value="18-12-2014">18 december 2014</option>
        <option value="17-12-2014">17 december 2014</option>
        <option value="13-11-2014">13 november 2014</option>
        <option value="12-11-2014">12 november 2014</option>
        <option selected="selected" value="25-09-2014">25 september 2014</option>
        <option value="09-07-2014">9 juli 2014</option>
        <option value="05-06-2014">5 juni 2014</option>
        <option value="06-05-2014">6 mei 2014</option>
        <option value="27-03-2014">27 maart 2014</option>
        <option value="26-03-2014">26 maart 2014</option>
        <option value="25-02-2014">25 februari 2014</option>
        <option value="30-01-2014">30 januari 2014</option>
        <option value="12-12-2013">12 december 2013</option>
                                    </select> 
                      </div>
                </fieldset>
            </div>

            </div>
   </div>

Depending on the date chosen the rest of the page will be updated
Obviously, apart from the 25 september 2014 I would like to retrieve data from other meetings.

So I would like to know what (parameter ?) I should add to my code to select another date (= option) from the listbox while performing the fetch .
In general I don't know what (and how many) options exist in the listboc, so I would like to loop through all of them.

I'm using Google Apps Script for a UiApp

Upvotes: 0

Views: 122

Answers (1)

Mr5o1
Mr5o1

Reputation: 1818

This is the part which is retrieving the new page, when the user updates the select box:

__doPostBack(\&#39;ctl00$phContent$ddlYear\&#39;,\&#39;\&#39;)

You'd need to examine the js source to figure out exactly what that function does, because there might be other options sent in the post data. But it's possible that it just submits a post request with that value, and if so something like this might work:

var url = 'http://domain.com/pageUrl.html';
var options = {
  method = 'POST',
  payload = {
    'ctl00$phContent$ddlYear': '18-12-2014'
  }
};
var response = UrlFetchApp.fetch(url, options);

That's just a post request which submits the 'December 2014' value of that select box.

As I said, if that doesn't work then you really need look at the source of __doPostBack() in the js to figure out exactly what it's posting to get the required response.

Upvotes: 1

Related Questions