s_HTMLTable
Download scriptThis script is designed to automate the creation of a structured HTML table and save it as a web-compatible file. Instead of manually coding the complex layout of a webpage, this tool allows for the systematic generation of organized data into a clean, visual format.
Core Functionality
1. Data Organization The logic begins by defining two primary components:
- Headers: A set of titles that name the columns of your table (e.g., "Column 1", "Column 2", etc.).
- Content: A structured grid where information is populated automatically. The script creates a sequence of rows and columns, ensuring that every cell is filled with organized labels based on its position.
2. Dynamic Table Construction The heart of the script is a specialized function that converts this raw data into HTML code. It handles all the "heavy lifting" associated with web design by applying the following rules:
- Styling: You can define specific visual preferences, such as the font type (e.g., Tahoma), font size, and text colors.
- Layout Controls: It manages spacing between cells, padding within cells, and border thicknesses.
- Visual Hierarchy: The script includes logic to automatically make the leftmost column bold for easier reading and distinguishes the header row from the rest of the data.
- Alternating Row Colors: To improve readability (similar to a spreadsheet), the tool can automatically cycle between two different background colors for every other row.
3. File Generation
Once the table is built in the background, the script performs the final step: it takes the completed HTML code and writes it into a physical file named HTML_Example.htm. This allows you to open the resulting file in any web browser to view your perfectly formatted table instantly.
Summary of Benefits
This tool transforms a list of raw data into a professional, styled webpage component. By automating the layout—including colors, fonts, and row alternating—it ensures that the final output is consistent and visually organized without requiring manual formatting.
#property copyright "Integer"
#property link "for-good-letters@yandex.ru"
int start(){
// Ïîäãîòîâêà ìàññèâà çàãîëîâêîâ
string Header[5]={" "/*ëåâàÿ êîëîíêà èñïîëüçóåòñÿ äëÿ íóìåðàöèè ñòðîê*/,"Êîëîíêà 1","Êîëîíêà 2","Êîëîíêà 3","Êîëîíêà 4"};
// Ïîäãîòîâêà ìàññèâà ñ ñîäåðæèìûì òàáëèöû
string Content[10][5];
for(int i=0;i<10;i++){
for(int j=1;j<5;j++){
Content[i][j]="Êîëîíêà - "+j+", ñòðîêà - "+i;
}
Content[i][0]=i+1; // íîìåðà ñòîðîê
}
// Ôîðìèðîâàíèå òàáëèöû
string str= fHTML_Table(Header,Content);
// Ñîõðàíåíèå ôàéëà
int h=FileOpen("HTML_Example.htm",FILE_CSV|FILE_WRITE);
FileWrite(h,str);
FileClose(h);
//----
return(0);
}
//+------------------------------------------------------------------+
string fHTML_Table( string aHeader[], // Ìàññèâ ñ çàãîëîâêàìè êîëîíîê. Ðàçìåð ìàññèâà îïðåäåëÿåò êîëè÷åñòâî êîëîíîê òàáëèöû
string aContent[][], // Ìàññèâ ñ ñîäåðæèìûì òàáëèöû. Ïåðâîå èçìåðåíèå - ñòðîêà, âòîðîå - êîëîíêà
bool aLeftBold = true, // Âûâîäèòü ëåâóþ êîëîíêó æèðíûì øðèôòîì
bool aHeaderFlag = true, // Âûâîäèòü çàãîëîâîê. Äàæå åñëè âûâîä çàãîëîâêà íå èñïîëüçóåòñÿ, âñåðàâíî íåîáõîäèìî îïðåäåëÿòü ðàçìåð ìàññèâà aHeader
bool aColorAltFlag = true, // ×åðåäîâàòü öâåòà ñòðîê òàáëèöû
string aFontFace = "Tahoma", // Øðèôò
int aFontSize = 1, // Ðàçìåð øðèôòà
int aBorderSize = 0, // Øèðèíà ðàìêè
int aCellPadding = 2, // Îòñóòï òåêñòà îò êðàÿ ÿ÷åéêè
int aCellSpacing = 1, // Ïðîñòðàíñòâî ìåæäó ÿ÷åéêàìè
string aFontColor = "#000000", // Öâåò òåêñòà
string aBorderColor = "#FFFFFF", // Öâåò ðàìêè
string aHeaderColor = "#C0C0C0", // Öâåò ôîíà çàãîëîâêà
string aRowColor_1 = "#FFFFFF", // Öâåò 1 ñòðîê òàáëèöû
string aRowColor_2 = "#E0E0E0" // Öâåò 2 ñòðîê òàáëèöû
){
string tRetStr="<table border=1 bordercolor="+aHeaderColor+" cellpadding=0 cellspacing=0><tr><td>\n";
tRetStr=tRetStr+"<table border="+aBorderSize+" bordercolor="+aBorderColor+" cellpadding="+aCellPadding+" cellspacing="+aCellSpacing+">\n";
if(aHeaderFlag){
tRetStr=tRetStr+"<tr bgcolor="+aHeaderColor+">";
for(int tj=0;tj<ArraySize(aHeader);tj++){
tRetStr=tRetStr+"<td><b><p align=center><font face="+aFontFace+" size="+aFontSize+" color="+aFontColor+">"+aHeader[tj]+"</font></p></b></td>\n";
}
tRetStr=tRetStr+"</tr>\n";
}
string tColor=aRowColor_1;
for(int ti=0;ti<ArraySize(aContent)/ArraySize(aHeader);ti++){
tRetStr=tRetStr+"<tr bgcolor="+tColor+">\n";
for(tj=0;tj<ArraySize(aHeader);tj++){
string tStr=aContent[ti][tj];
if(tj==0){
if(aLeftBold){
tStr="<b>"+tStr+"</b>\n";
}
}
tRetStr=tRetStr+"<td><p align=right><font face="+aFontFace+" size="+aFontSize+" color="+aFontColor+">"+tStr+"</font></p></td>\n";
}
tRetStr=tRetStr+"</tr>\n";
if(aColorAltFlag){
if(tColor==aRowColor_1){
tColor=aRowColor_2;
}
else{
tColor=aRowColor_1;
}
}
}
tRetStr=tRetStr+"</table></td></tr></table>\n";
return(tRetStr);
}
Comments