슈

엑셀 흉내내기

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

<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,585
16-01-11 조회 2,397
16-01-11 조회 2,615
16-01-08 조회 2,805
16-01-08 조회 2,562
16-01-07 조회 2,579
16-01-05 조회 2,611
16-01-05 조회 2,609
16-01-05 조회 2,911
16-01-05 조회 2,727
16-01-05 조회 2,768
16-01-04 조회 2,622
16-01-04 조회 2,635
16-01-04 조회 2,585
16-01-04 조회 2,495
16-01-03 조회 3,328
16-01-03 조회 2,641
16-01-03 조회 2,909
16-01-02 조회 2,838
16-01-02 조회 2,584
16-01-02 조회 2,694
16-01-02 조회 2,474
16-01-01 조회 2,773
16-01-01 조회 2,699
16-01-01 조회 2,776
15-12-31 조회 2,557
15-12-31 조회 3,266
15-12-31 조회 2,858
15-12-31 조회 2,881
15-12-31 조회 2,691
15-12-30 조회 2,637
15-12-30 조회 3,200
15-12-30 조회 2,839
15-12-30 조회 2,729
15-12-30 조회 2,643
15-12-30 조회 2,559
15-12-29 조회 2,586
15-12-29 조회 2,780
15-12-29 조회 2,881
15-12-29 조회 2,601
15-12-28 조회 2,617
15-12-28 조회 2,643
15-12-28 조회 2,672
15-12-28 조회 2,512
15-12-27 조회 3,181
15-12-27 조회 2,646
15-12-27 조회 2,547
15-12-27 조회 2,608
15-12-26 조회 2,568
15-12-26 조회 2,637
15-12-26 조회 2,655
15-12-25 조회 3,343
15-12-25 조회 2,534
15-12-25 조회 2,535
15-12-25 조회 2,492
15-12-24 조회 3,029
15-12-24 조회 2,654
15-12-24 조회 2,658
15-12-24 조회 2,680
15-12-24 조회 2,636
15-12-23 조회 2,803
15-12-23 조회 2,622
15-12-23 조회 2,491
15-12-23 조회 2,623
15-12-23 조회 2,571
15-12-22 조회 2,750
15-12-22 조회 2,610
15-12-22 조회 2,593
15-12-22 조회 2,498
15-12-21 조회 2,536
15-12-21 조회 2,572
15-12-21 조회 2,540
15-12-20 조회 2,587
15-12-20 조회 2,749
15-12-20 조회 2,527
15-12-19 조회 2,605
15-12-19 조회 2,595
15-12-19 조회 2,607
15-12-18 조회 2,907
15-12-18 조회 2,582
15-12-18 조회 2,566
15-12-18 조회 2,543
15-12-17 조회 2,614
15-12-17 조회 2,534
15-12-17 조회 2,539
15-12-17 조회 2,507
15-12-16 조회 2,521
15-12-16 조회 2,558
15-12-16 조회 2,500
15-12-16 조회 2,714
15-12-15 조회 2,849
15-12-15 조회 2,780
15-12-15 조회 2,510
15-12-15 조회 2,507
15-12-15 조회 2,501
15-12-14 조회 2,573
15-12-14 조회 2,657
15-12-14 조회 2,481
15-12-13 조회 2,449
15-12-13 조회 2,493