Table - potatoscript/javascript GitHub Wiki
Table
- Blinking Table Cell
- Create Table
- Copy Paste table to Excel
- Delete Table Row Onclick
- Excel Cell Properties
- Get Table row and Column index
- Merge Table Cell
- Multiple Select on Table's Row
- Protect Excel Sheets -
xls.WorkSheets(1).Protect(Password="potato");
- row onclick pure javascript
- row color onclick
- Trigger Checkbox
Blinking-Table-Cell
var bn = 0,arry,arry2,arry3=[];
Blink:function(id,row,col,c1,c2,t,bn){
document.getElementById(id).rows[row].cells[col].style.background=c1;
arry2[bn]==setTimeout(function(){
potato.Blink2(id,row,col,c1,c2,t,bn);
},t);
},
Blink2:function(id,row,col,c1,c2,t,bn){
document.getElementById(id).rows[row]cells[col].style.background=c2;
arry3[bn]=setTimeout(function(){
potato.Blink(id,row,col,c1,c2,t,bn);
},t);
},
Clear:function(){
for(var i=0;i<b__.length;i++){
clearTimeout(arry2[i]);
clearTimeout(arry3[i]);
}
}
-at application javascript
potato.Clear();
for(var i=1;i<table.rows.length;i++){
if(...){
potato.Blink("tableId",i,c,"yellow","white",1000,bn);
bn++;
}
}
Create-Table
var body =document.getElementsByTagName("body")[0];
var tbl = document.createElement("table");
var tblBody = document.createElement("tbody");
var row = document.createElement("tr");
var cell = document.createElement("th");
var col = document.createTextNode("Machine");
cell.appendChild(col);
row.appendChild(cell);
cell.style.textAlign = "center";
tblBody.appendChild(row);
tbl.appendChild(tblBody);
body.appendChild(tbl);
tbl.setAttribute("id","tableId");
Copy-Paste-to-Excel
Excel:function(id,w,p){
var excel = new ActiveXObject("Excel.Application");
var b = document.body,
r = b.createTextRange();
r.moveToElementText(document.getElementById(id));
r.execCommand("COPY");
if(p){
excel.visible=true;
var s = excel.Worksheets(1);
s.Paste;
for(var i=0;i<p.length;i++){
p[i]();
}
}else{
var x = this.ActiveXObject("Excel.Application");
x.visible = true;
if(w && w!=""){
var ad = String(location).split("?");
var fso = new ActiveXObject("Scripting.FileSystemObject");
if(!fso.FolderExists("c:\\potato")){
fso.createFolder("c:\\potato");
}
a = x.Workbooks.Open(ad.slice(0,1)+w);
}else{
a = x.Workbooks.Add;
}
var s = a.WorkSheets(1);
s.Paste;
}
window.clipboardData.clearData();
}
#Delete-Table-Row-Onclick
function deleteRow(r){
var i = r.parentNode.parentNode.rowIndex;
document.getElementById("tableId").deleteRow(i);
}
Excel-Cell-Properties
- only work in IE with ActiveXObject permission set to enabled.
var x = document.getElementById("tableId").rows;
var y = x[0].cells;
var xls = new ActiveXObject("Excel.Application");
xls.visible = true;
xls.Workbooks.Add;
xls.Range("A1").value = y[8].innerText;
xls.Range("A1").HorizontalAlignment = -4108 or -4131;
var book = xls.Workbooks.Open("c:\\Example.xls");
book.save();
book.Quit();
xls.cells(3,7).Font.Bold = true;
xls.cells(3,7).Font.size or .color or .underline...
var x = document.getElementById("tableId").rows;
var y = x[0].cells;
xls.Cells(2,2).value = y[0].innerText;
xls.Range("A1:AD50").Formula = e.Range("A1:AD50").Formula;
var e = xls.Worksheets("sheet2");
e.Activate;
xls.Worksheets(1).Range("A1:D1").HorizontalAlignment = -4108;
xls.activeSheet.Name = "XXX";
xls.activeSheet.columns.autofit;
xls.worksheets(1).Range("A1:D1").merge();
xls.cells(2,2).Borders.ColorIndex = "1";
xls.Worksheets(1).Range("A1").Font.Size = 20;
xls.Range("A1:A10").WrapText = true;
xls.Range("A1").interior.colorIndex = "35";
xls.Cells(3,7).NumberFormatLocal = "0.00%";
xls.Cells(3,7).style = "Percent";
xls.Cells(3,m).Value = "=SUM(R[1]C:R["+H+"]C)";
xls.Range("A3:J100").rowheight = 15;
xls.Range("A1").Border(10).LineStyle = 1;//xlcontinuous
.LineStyle = 5;//xlDashDotDot
.LineStyle = 4;//xlDashDot
.LineStyle = -4115;//xldash
.LineStyle = -4118;//xlDot
.LineStyle = -4119;//xlDouble
.LineStyle = -4142;//xlNone
xls.Range("A1").Border(10);//right
xls.Range("A1").Border(9);//bottom
xls.Range("A1").Border(8);//top
xls.Range("A1").Border(7);//left
xls.Range("A1").Border(10).Weight = 1;//xlHairline
xls.Range("A1").Border(10).Weight = 2;//xlThin
xls.Range("A1").Border(10).Weight = 4;//xlThick
xls.Range("A1").Border(10).Weight = -4138//xlMedium
Get-Data-from-Excel-Sheet
function GetData(cell,row){
var excel = new ActiveXObject("Excel.Application");
var excel_file = excel.Workbooks.Open("c:\\File.xls");
var excel_sheet = excel.Workbooks("Sheet1");
var data = excel.sheet.Cells(cell,row).value;
document.getElementById("div1").innerText = data;
}
Index
- Get the table row and column index
$("#tableId td").click(function(){
var ci = $(this).parent().children().index(this); //column index
var ri = $(this).parent().parent().children().index(this.parentNode); //row index
var table = document.getElementById("tableId");
if(ci==3){
alert(table.rows[ri].cells[ci].firstChild.nodeValue); // .firstChild.nodeValue or .innerText or .innerHTML
}
//set the table interval row background color on selected column index
for(var i=1; i<table.rows.length; i++){
if(i%2==0)table.rows[i].cells[0].style.background = "yellow";
}
});
Row-Color-OnClick
$("#tableId td")
.addClass("tableTd")
.click(function(){
$("td.tableTd".css("background","white"); //set all row to white background
var table = document.getElementById("tableId");
var row = table.rows[this.rowIndex];
row.cells[0].style.background = "yellow";
row.cells[0].style.textDecoration = "blink";
row.cells[0].style.fontStyle = "italic/normal/oblique";
});
Merge-Table-Cell
table.rows[1].cells[1].setAttribute("rowSpan",2);
table.rows[1].cells[1].setAttribute("colSpan",5);
Multiple-Select-Row
var array_data = new Array();
var n=0, rows = table.rows;
for(var k=1; k<table.rows.length; k++){
rows[k].onclick=function(){
var row = rows[this.rowIndex];
if(String(row.cells[1].innerHTML).indexOf("★")==-1){
array_data[n] = row.cells[1].innerHTML;
row.cells[1].innerHTML = "★"+row.cells[1].innerHTML;
n++;
}else{
var val = String(row.cells[1].innerHTML).split("★");
row.cells[1].innerHTML = val.slice(1,2);
for(var a=0;a<array_data.length;a++){
if(array_data[a] == row.cells[1].innerHTML){
array_data[a] = ""; //clear array_data and remove ★
}
}
}
}
}
- using function expression in potato.js
MultipleSelect : function(symbol,id,c,v,callback){
var table = document.getElementById(id);
var ci,ri,n = 0;
var array1,array2 = [];
$("#"+id + " td").click(function(){
ci = $(this).parent().children().index(this);
ri = $(this).parent().parent().children().index(this.parentNode);
});
for(var k=1;k<table.rows.length;k++){
table.rows[k].onclick=function(){
if(ci==c){
var row = table.rows[this.rowIndex];
if(String(row.cells[ci].innerHTML).indexOf(symbol)==-1){
array2[n]==row.cells[v].innerHTML;
n++;
row.cells[ci].innerHTML=symbol+row.cells[ci].innerHTML;
}else{
var val = String(row.cells[ci].innerHTML).split(symbol);
row.cells[ci].innerHTML = val.slice(1,2);
for(var a=0;a<array2.length;a++){
if(array2[a]==row.cells[v].innerText){
array2[a]="";
}
}
}
array1 = [];
var b = 0;
for(var a=0;a<array2.length;a++){
if(array2[a]!="" && array2[a]!="undefined"){
var check2 = 0;
for(var g=0;g<array1.length;g++){
if(array1[g]==array2[a]){
check2 = 1;
}
}
if(check2==0){
array1[b]=array2[a];
b++;
}
}
}
callback(array1);
}
}
}
}
Trigger-Checkbox
- Trigger all the checkbox on the row
$("#tableId tr").click(function(event){
//checkbox or radio
if(event.target.type!=="checkbox"){
$(":checkbox", this).trigger("click");
}
});
or
table.rows[0].cells[0].onclick=function(event){
if(event.target.type!=="checkbox"){
$(":checkbox",this).trigger("click");
}
};
Row-OnClick
var rows = document.getElementById("list").rows;
for (i = 0; i < rows.length; i++) {
rows[i].onclick = function(){
return function(){
var id = this.cells[0].innerHTML;
alert("id:" + id);
};
}(rows[i]);
}