슈

엑셀 흉내내기

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

<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-02-06 조회 3,603
16-02-04 조회 3,160
16-02-04 조회 3,455
16-02-04 조회 3,087
16-02-03 조회 3,223
16-02-03 조회 2,959
16-02-03 조회 3,008
16-02-03 조회 2,929
16-02-03 조회 3,046
16-02-02 조회 3,085
16-02-02 조회 2,973
16-02-02 조회 2,999
16-02-02 조회 2,879
16-02-02 조회 3,304
16-02-01 조회 2,881
16-02-01 조회 2,689
16-02-01 조회 2,672
16-01-31 조회 2,697
16-01-31 조회 2,701
16-01-31 조회 2,714
16-01-30 조회 3,107
16-01-30 조회 2,708
16-01-30 조회 2,699
16-01-30 조회 2,896
16-01-30 조회 3,043
16-01-30 조회 2,840
16-01-29 조회 2,701
16-01-29 조회 2,702
16-01-29 조회 2,592
16-01-29 조회 2,578
16-01-28 조회 3,061
16-01-28 조회 2,920
16-01-28 조회 2,531
16-01-28 조회 2,435
16-01-28 조회 2,676
16-01-27 조회 2,603
16-01-27 조회 2,582
16-01-27 조회 2,594
16-01-26 조회 2,591
16-01-26 조회 2,565
16-01-26 조회 2,572
16-01-26 조회 2,891
16-01-25 조회 2,588
16-01-25 조회 2,635
16-01-25 조회 2,641
16-01-25 조회 2,513
16-01-24 조회 2,513
16-01-24 조회 2,508
16-01-24 조회 2,426
16-01-23 조회 2,479
16-01-23 조회 2,491
16-01-23 조회 2,461
16-01-22 조회 2,600
16-01-22 조회 2,711
16-01-22 조회 2,619
16-01-22 조회 2,695
16-01-21 조회 2,733
16-01-21 조회 2,607
16-01-21 조회 2,593
16-01-21 조회 2,605
16-01-21 조회 2,563
16-01-20 조회 2,584
16-01-20 조회 2,509
16-01-20 조회 2,466
16-01-20 조회 2,477
16-01-19 조회 2,499
16-01-19 조회 2,537
16-01-19 조회 2,520
16-01-19 조회 2,559
16-01-19 조회 2,566
16-01-18 조회 2,544
16-01-18 조회 2,521
16-01-18 조회 2,546
16-01-18 조회 2,694
16-01-17 조회 2,711
16-01-17 조회 2,561
16-01-17 조회 2,633
16-01-16 조회 2,478
16-01-16 조회 2,413
16-01-16 조회 2,463
16-01-15 조회 2,619
16-01-15 조회 2,622
16-01-15 조회 2,545
16-01-15 조회 2,481
16-01-14 조회 2,631
16-01-14 조회 2,760
16-01-14 조회 3,211
16-01-14 조회 2,524
16-01-14 조회 2,762
16-01-14 조회 2,566
16-01-13 조회 2,836
16-01-13 조회 2,616
16-01-13 조회 2,582
16-01-13 조회 2,604
16-01-13 조회 2,496
16-01-12 조회 2,924
16-01-12 조회 2,663
16-01-12 조회 2,680
16-01-12 조회 2,602
16-01-11 조회 2,725