Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The data you're looking for isn't really well-formed for automated extraction, but we can work with it. The <code>getElementByVal()</code> function from <a href="https://stackoverflow.com/a/16702114/1677912">this answer</a> can be refactored to create <code>getElementsByVal()</code>, which will return an array of all matching document elements, giving us something to do further searching in.</p> <pre><code>/** * Traverse the given XmlElement, and return an array of matches. * Note: 'class' is stripped during parsing and cannot be used for * searching, I don't know why. * &lt;pre&gt; * Example: getElementsByVal( body, 'input', 'value', 'Go' ); will find * * &lt;input type="submit" name="btn" value="Go" id="btn" class="submit buttonGradient" /&gt; * &lt;/pre&gt; * * @param {XmlElement} element XML document element to start search at. * @param {String} id HTML &lt;div&gt; id to find. * * @return {[XmlElements]} All matching elements (in doc order). */ function getElementsByVal( element, elementType, attr, val ) { var results = []; // If the current element matches, remember it. if (element[attr] &amp;&amp; element[attr] == val &amp;&amp; element.getName().getLocalName() == elementType) { results.push( element ); } // Check element's children var elList = element.getElements(); var i = elList.length; while (i--) { // (Recursive) Check each child, in document order. results = results.concat(getElementsByVal( elList[i], elementType, attr, val )); } // Return summary of matches return results; } </code></pre> <p>To make use of this new helper function, what if we create a function <code>getIndicators()</code> that accepts a String parameter containing the point in time we're interested in - <code>Mon. Jul 29, 2013 07:40:00</code> for example? The matching text will be found in an <code>area</code> element that's got <code>shape="rect"</code>, and we'll find it inside an attribute called <code>onmousemove</code>. Here's our function: </p> <pre><code>function getIndicators(timeString) { var txt = UrlFetchApp.fetch("http://www.barchart.com/chart.php?sym=DXU13&amp;t=BAR&amp;size=M&amp;v=2&amp;g=1&amp;p=I:5&amp;d=L&amp;qb=1&amp;style=technical&amp;template=").getContentText(); var doc = Xml.parse(txt,true); var body = doc.html.body; var indicators = "not found"; // Look for elements matching: &lt; area shape="rect" ... &gt; var chartPoints = getElementsByVal(body, 'area', 'shape', 'rect'); // Search for the chartPoint with tooltip containing the time we care about for (var i=0; i&lt;chartPoints.length; i++) { if (chartPoints[i].onmousemove.indexOf(timeString) &gt; -1) { // found our match indicators = chartPoints[i].onmousemove; } } return indicators } </code></pre> <p>As it stands, it will return the entire text value assigned to <code>onmousemove</code>; the exercise of parsing that intelligently is left to you.</p> <p>Here's a test function to help:</p> <pre><code>function test_getIndicators() { Logger.log( getIndicators("Mon. Jul 29, 2013 07:40:00" ) ); } </code></pre> <p>When run, here's the log (today, anyway...):</p> <pre><code>[13-07-29 16:47:47:266 EDT] showOHLCTooltip(event, 'B', '[Mon. Jul 29, 2013 07:40:00]', 'DXU13', '81.8050000', '81.8300000', '81.8000000', '81.8200000') </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload