IMPORT COM.sbi
filename = "C:\\ScriptBASIC\\examples\\excel_test.xls"
IF FileExists(filename) THEN
PRINT "File already exists deleting: ", filename,"\n"
DELETE filename
END IF
oExcelApp = COM::CREATE(:SET, "Excel.Application")
' oExcelApp = COM::CREATE(:SET, "{00024500-0000-0000-C000-000000000046}")
IF oExcelApp = 0 THEN
PRINT "Failed to create Excel Object do you have it installed?\n"
END
END IF
' VBS: Set ExcelWorkbook = ExcelApp.Workbooks.Add
oWorkBook = COM::CBN(oExcelApp, "Workbooks", :GET)
IF oWorkBook = 0 THEN
PRINT "Failed to create Workbook\n"
END
END IF
oExcelWorkbook = COM::CBN(oWorkBook, "Add")
COM::DI(oExcelWorkBook)
' VBS: Set ExcelSheet = ExcelWorkbook.Worksheets(1)
oExcelSheet = COM::CBN(oExcelWorkbook, "Worksheets", :GET, 1)
IF oExcelSheet = 0 THEN
PRINT "Failed to get oExcelSheet\n"
END
END IF
' Adding cells...
oRange = COM::CBN(oExcelSheet, "Range", :GET, "G3")
IF oRange = 0 THEN
PRINT "Failed to get oRange\n"
END
END IF
COM::CBN(oRange, "Value", :LET, "123")
COM::RELEASE(oRange)
oRange = COM::CBN(oExcelSheet, "Range", :GET, "B1:B5")
IF oRange = 0 THEN
PRINT "Failed to get oRange\n"
END
END IF
' Place BOLD border around range
CONST XlBorderWeight_xlMedium = -4138
COM::CBN(oRange, "BorderAround", :CALL, 1, XlBorderWeight_xlMedium, 3)
oInterior = COM::CBN(oRange, "Interior", :GET)
IF oInterior = 0 THEN
PRINT "Failed to get oInterior\n"
END
END IF
COM::CBN(oInterior, "ColorIndex", :LET, "38")
COM::CBN(oInterior, "Pattern", :LET, "xlSolid")
COM::RELEASE(oRange)
COM::RELEASE(oInterior)
FOR i=1 TO 10
FOR j=1 TO 10
' VBS: ExcelSheet.Cells(i, j).Value = "test-" & i & "-" & j
oCell = COM::CBN(oExcelSheet, "Cells", :GET, i, j)
COM::CBN(oCell, "Value", :LET, "test-" & i & "-" & j)
COM::RELEASE(oCell)
NEXT
NEXT
' Saving spreadsheet
COM::CBN(oExcelWorkbook, "SaveAs", :CALL, filename)
COM::CBN(oExcelWorkbook, "Close")
COM::CBN(oExcelApp, "Quit")
' Releasing objects from memory
COM::RELEASE(oExcelSheet)
COM::RELEASE(oExcelWorkbook)
COM::RELEASE(oWorkBook)
COM::RELEASE(oExcelApp)
PRINT "Spreadsheet Created.\n"
' MySQL Test Program
IMPORT mysql.bas
dbh = mysql::RealConnect("localhost","root","PASSWORD","classicmodels")
mysql::query(dbh,"SELECT * FROM products LIMIT 25")
WHILE mysql::FetchHash(dbh,column)
PRINT column{"productCode"}," - ",column{"productName"}," - ",FORMAT("%~$###.00~",column{"MSRP"}),"\n"
WEND
PRINTNL
PRINT "The database handle is: ",dbh,"\n"
PRINT "Affected rows by SELECT: ",mysql::AffectedRows(dbh),"\n"
PRINT "Character set name is: ",mysql::CharacterSetName(dbh),"\n"
PRINT "Last error is: ",mysql::ErrorMessage(dbh),"\n"
PRINT "Client info is: ",mysql::GetClientInfo(),"\n"
PRINT "Host info is: ",mysql::GetHostInfo(dbh),"\n"
PRINT "Proto info is: ",mysql::GetProtoInfo(dbh),"\n"
PRINT "Server info is: ",mysql::GetServerInfo(dbh),"\n"
PRINT "PING result: ",mysql::Ping(dbh),"\n"
PRINT "Thread ID: ",mysql::ThreadId(dbh),"\n"
PRINT "Status is: ",mysql::Stat(dbh),"\n"
mysql::Close(dbh)
' ScriptBasic AJAX & MySQL
IMPORT cgi.bas
cgi::Header 200,"text/html"
cgi::FinishHeader
PRINT """
<html>
<head>
<script>
function showItems(str) {
if (str == "") {
document.getElementById("results").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("results").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","/home/qbo/getitems.sb?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form>
<select name="items" onchange="showItems(this.value)">
<option value="">Product Line</option>
<option value="Classic Cars">Classic Cars</option>
<option value="Motorcycles">Motorcycles</option>
<option value="Planes">Planes</option>
<option value="Ships">Ships</option>
<option value="Trains">Trains</option>
<option value="Trucks and Buses">Trucks and Buses</option>
<option value="Vintage Cars">Vintage Cars</option>
</select>
</form>
<br>
<div id="results"></div>
</body>
</html>
"""
' AJAX - getitems.sb
IMPORT cgi.bas
IMPORT mysql.bas
cgi::Header 200,"text/html"
PRINT """
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
"""
product_line = cgi::GetParam("q")
dbh = mysql::RealConnect("localhost","USER","PASSWORD","classicmodels")
mysql::query(dbh,"SELECT * FROM products WHERE productLine = '" & product_line & "'")
PRINT """
<table>
<tr>
<th>Product Code</th>
<th>Product Line</th>
<th>Product Vendor</th>
<th>Product Name</th>
<th>In Stock</th>
<th>Cost</th>
<th>MSRP</th>
</tr>
"""
WHILE mysql::FetchHash(dbh,column)
PRINT " <tr>\n"
PRINT " <td>", column{"productCode"}, "</td>\n"
PRINT " <td>", column{"productLine"}, "</td>\n"
PRINT " <td>", column{"productVendor"}, "</td>\n"
PRINT " <td>", column{"productName"}, "</td>\n"
PRINT " <td align=\"right\">", column{"quantityInStock"}, "</td>\n"
PRINT " <td align=\"right\">", FORMAT("%~$###.00~",column{"buyPrice"}), "</td>\n"
PRINT " <td align=\"right\">", FORMAT("%~$###.00~",column{"MSRP"}), "</td>\n"
PRINT " </tr>\n"
WEND
PRINT """
</table>
</body>
</html>
"""
mysql::Close(dbh)
' SBT Demo
IMPORT sbt.sbi
sb_code = """
FUNCTION prtvars(a, b, c)
PRINT a,"\\n"
PRINT FORMAT("%g\\n", b)
PRINT c,"\\n"
prtvars = "Function Return"
END FUNCTION
a = 0
b = 0
c = ""
"""
sb = SB_New()
SB_Configure sb, "C:/Windows/SCRIBA.INI_32"
SB_Loadstr sb, sb_code
SB_NoRun sb
' Call function before running script
funcrtn = SB_CallSubArgs(sb,"main::prtvars", 123, 1.23, "One,Two,Three")
PRINT funcrtn,"\n"
' Run script initializing globals
SB_Run sb, ""
' Assign variables values
SB_SetInt sb, "main::a", 321
SB_SetDbl sb, "main::b", 32.1
SB_SetStr sb, "main::c", "Three,Two,One" & CHR(0)
' Call function again with variables assigned in the previous step
SB_CallSubArgs sb, "main::prtvars", _
SB_GetVar(sb, "main::a"), _
SB_GetVar(sb, "main::b"), _
SB_GetVar(sb, "main::c")
SB_Destroy sb
' SBT Main
IMPORT mt.sbi
IMPORT sbt.sbi
SB_ThreadStart("sbt_thread.sb", "","C:/Windows/SCRIBA.INI")
FOR x = 1 TO 10
PRINT "M:",x,"\n"
sb_msSleep(20)
NEXT
SB_msSleep(1000)
PRINT "Thread ",mt::GetVariable("thread_status"),"\n"
' SBT Thread
IMPORT mt.sbi
IMPORT sbt.sbi
FOR x = 1 TO 10
PRINT "T:",x,"\n"
SB_msSleep(20)
NEXT
mt::SetVariable "thread_status","Completed"
SB_ThreadEnd
' OpenWeather - cURL & JSON Example
IMPORT curl.sbi
IMPORT webext.sbi
place = COMMAND()
ch = curl::init()
curl::option(ch, "URL", "http://api.openweathermap.org/data/2.5/weather?q=" & place & "&units=imperial&appid=APP KEY")
curl::option(ch, "CUSTOMREQUEST", "GET")
json = curl::perform(ch)
curl::finish(ch)
PRINT json,"\n\n"
web::json2sba(json)
web::sbadump(json)
PRINT "\nTempreture: ", json{"main"}{"temp"}, " F\n"
PRINT "Date: ", FORMATDATE("MM/DD/YEAR 0H:0m:0s",json{"dt"} + json{"timezone"}),"\n"
END