슈

엑셀 흉내내기

· 2016-01-02 (토) 13:45:37 · 2780

<style>

#excel {width: 800px; height: 400px; overflow: auto; border: 1px solid #ccc}

#excel table { width: 2000px; border-spacing:0 }

#excel table th {font-weight: normal; background-color: #eee}

#excel table th, #excel table  td { height: 20px; border: 1px solid #ddd; }

#excel table td {text-align: right; padding: 0; }

#excel table input {border:none; border-right:0px; border-top:0px; boder-left:0px; boder-bottom:0px; outline-style: none; font-size:  1em }

</style>

<div id="excel"></div>

<script>

function Excel(label, rows, cols) {

this.div = document.getElementById(label);

this.table = document.createElement('table');

this.rows = rows;

this.cols = cols;

this.clicked = null;

this.cells = [];

}

Excel.prototype.init = function() {

var tr = {}, el = {}, r = 0, c = 0, tr = document.createElement('tr')

for (c=0;  c<this.cols; c++) {

el = document.createElement('th');

el.style.width = Math.round(100 / this.cols * 10) / 10 + '%'; 

if (c != 0) el.textContent = String.fromCharCode(64 + c);

tr.appendChild(el);

}

this.table.appendChild(tr);

for (r=1;  r<this.rows; r++) {

tr = document.createElement('tr');

for (c=0;  c<this.cols; c++) {

if (c !=0) el= document.createElement('td');

else {

el= document.createElement('th');

el.textContent = r;

}

tr.appendChild(el);

}

this.table.appendChild(tr);

}

this.div.appendChild(this.table);

};

Excel.prototype.getPos = function(obj) {

var pos = {};

pos.r = obj.parentNode.rowIndex;

pos.c = obj.cellIndex;

return pos;

}

Excel.prototype.click = function() {

var td = this, textbox, text, pos = excel.getPos(this);

if (excel.clicked != null) excel.clicked.style.border = '1px solid #ddd';

this.style.border = '1px solid blue';

excel.clicked = this;

textbox = document.createElement('input');

if (this.hasChildNodes()) {

if (this.firstChild.nodeName == 'INPUT') return;

else {

text = this.firstChild.textContent; 

textbox.value = excel.cells[pos.r + ',' + pos.c];

this.removeChild(this.firstChild);

}

}

textbox.type = 'text'; 

textbox.style.width = (this.offsetWidth - 2) + 'px';

textbox.style.height = (this.offsetHeight - 4) + 'px';

this.appendChild(textbox);

this.firstChild.focus();

textbox.onblur = function(){  

text = this.value;

this.remove(0);

excel.cells[pos.r + ',' + pos.c] = text; 

if (text.match(/^=/)) text = eval( text.replace('=', '') );

td.textContent = text;

}; 

}

Excel.prototype.edit = function() {

var table, tr = {}, el = {}, r = 0, c = 0,

table = this.div.childNodes[0];

for (r=1; r<this.rows; r++) {

for (c=1; c<this.cols; c++) { 

el = table.rows[r].cells[c];

el.onclick  = this.click;

el.onkeydown = this.keydown;

}

}

};

Excel.prototype.keydown = function(e) {

var pos = excel.getPos(this), e = e || window.event;

switch (e.keyCode) {

case 38 : pos.r -= 1; break;

case 40 : case 13: pos.r += 1; break;

case 37 : pos.c -= 1; break;

case 39 : pos.c += 1; break;

}

if (pos.r < 1 || pos.r > excel.rows-1 || pos.c < 1 || pos.c > excel.cols-1) return;

excel.table.rows[pos.r].cells[pos.c].click();

};

var excel = new Excel('excel', 40, 27);

excel.init();

excel.edit();

</script>

|
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,188건
+
제목 글쓴이 날짜 조회
16-01-11 조회 2,278
16-01-11 조회 2,353
16-01-11 조회 2,327
16-01-08 조회 2,762
16-01-08 조회 2,496
16-01-07 조회 2,510
16-01-05 조회 2,515
16-01-05 조회 2,526
16-01-05 조회 2,828
16-01-05 조회 2,668
16-01-05 조회 2,702
16-01-04 조회 2,488
16-01-04 조회 2,589
16-01-04 조회 2,541
16-01-04 조회 2,454
16-01-03 조회 3,249
16-01-03 조회 2,595
16-01-03 조회 2,780
16-01-02 조회 2,781
16-01-02 조회 2,528
16-01-02 조회 2,635
16-01-02 조회 2,433
16-01-01 조회 2,722
16-01-01 조회 2,630
16-01-01 조회 2,648
15-12-31 조회 2,494
15-12-31 조회 3,192
15-12-31 조회 2,755
15-12-31 조회 2,787
15-12-31 조회 2,633
15-12-30 조회 2,594
15-12-30 조회 3,081
15-12-30 조회 2,766
15-12-30 조회 2,661
15-12-30 조회 2,522
15-12-30 조회 2,504
15-12-29 조회 2,534
15-12-29 조회 2,702
15-12-29 조회 2,797
15-12-29 조회 2,549
15-12-28 조회 2,569
15-12-28 조회 2,578
15-12-28 조회 2,544
15-12-28 조회 2,461
15-12-27 조회 3,139
15-12-27 조회 2,600
15-12-27 조회 2,511
15-12-27 조회 2,556
15-12-26 조회 2,527
15-12-26 조회 2,581
15-12-26 조회 2,587
15-12-25 조회 3,254
15-12-25 조회 2,493
15-12-25 조회 2,489
15-12-25 조회 2,421
15-12-24 조회 2,987
15-12-24 조회 2,593
15-12-24 조회 2,603
15-12-24 조회 2,633
15-12-24 조회 2,591
15-12-23 조회 2,739
15-12-23 조회 2,568
15-12-23 조회 2,441
15-12-23 조회 2,570
15-12-23 조회 2,518
15-12-22 조회 2,709
15-12-22 조회 2,560
15-12-22 조회 2,554
15-12-22 조회 2,463
15-12-21 조회 2,488
15-12-21 조회 2,534
15-12-21 조회 2,495
15-12-20 조회 2,543
15-12-20 조회 2,678
15-12-20 조회 2,491
15-12-19 조회 2,560
15-12-19 조회 2,549
15-12-19 조회 2,561
15-12-18 조회 2,850
15-12-18 조회 2,543
15-12-18 조회 2,522
15-12-18 조회 2,516
15-12-17 조회 2,560
15-12-17 조회 2,495
15-12-17 조회 2,495
15-12-17 조회 2,463
15-12-16 조회 2,475
15-12-16 조회 2,521
15-12-16 조회 2,457
15-12-16 조회 2,654
15-12-15 조회 2,812
15-12-15 조회 2,736
15-12-15 조회 2,470
15-12-15 조회 2,463
15-12-15 조회 2,459
15-12-14 조회 2,538
15-12-14 조회 2,614
15-12-14 조회 2,448
15-12-13 조회 2,424
15-12-13 조회 2,451