Dynamically Generating Excel Files from Web Data using SpreadScript
A sample application written
in Tcl using SpreadScript to dynamically generate
a native Excel file from Yahoo Finance quote data. This particular script
downloads the Yahoo quote page and inserts a link that will dynamically create
the Excel file.
exec /usr/local/bin/tclsh "$0" "$@"
set QuoteServer finance.yahoo.com
set QuoteCGI q
proc do_http_get { theServer theFile theParams } {
set len [string length $theParams]
set websock [socket $theServer 80]
puts $websock "GET /$theFile?$theParams HTTP/1.0\n"
flush $websock
set result [read $websock]
close $websock
return $result
}
set page [do_http_get $QuoteServer $QuoteCGI $env(QUERY_STRING)]
regexp "\[^\n\]*\n(.*)" $page match page
regsub {<html>} $page {<html><BASE HREF="http://finance.yahoo.com/">} page
regsub {<body>} $page {<body>\
<b>This live page from Yahoo Finance was modified by\
<a href="http://www.greytrout.com/">GreyTrout Software </a>\
to demonstrate the power of\
<a href="http://www.greytrout.com/spreadscript.html">SpreadScript</a>.</b>\
<p>\
Select "Standard CSV" below\
to download a comma separated values (CSV) file as provided by Yahoo.\
<br>Select <b><font color=red>"Native Excel File"</font></b> below\
to download a native Excel XLS spreadsheet as generated using\
<a href="http://www.greytrout.com/spreadscript.html">SpreadScript</a>\
using the Yahoo data.<p><hr size=4><p>} page
regsub {<a href="/d/quotes.csv\?(s=.*\.csv)">} \
$page \
{<b>Standard Yahoo CSV \></b> \
\0Download Spreadsheet</a></td></tr> \
<tr><td></td><td align=right><font face=arial> \
<font color=red><b>Native Excel File \></b></font> \
<a href="http://www.greytrout.com/cgi-bin/quotes.xls?\1">} \
page
puts $page