슈

엑셀 흉내내기

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

<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-05-12 조회 2,945
16-04-12 조회 3,109
16-02-03 조회 2,887
16-02-03 조회 2,957
16-02-02 조회 3,114
16-02-01 조회 2,615
16-02-01 조회 2,604
16-01-31 조회 2,636
16-01-31 조회 2,655
16-01-31 조회 2,655
16-01-30 조회 2,640
16-01-29 조회 2,635
16-01-29 조회 2,531
16-01-29 조회 2,517
16-01-28 조회 2,467
16-01-28 조회 2,377
16-01-27 조회 2,521
16-01-27 조회 2,525
16-01-26 조회 2,481
16-01-25 조회 2,571
16-01-25 조회 2,444
16-01-24 조회 2,445
16-01-24 조회 2,444
16-01-24 조회 2,362
16-01-23 조회 2,411
16-01-23 조회 2,438
16-01-23 조회 2,407
16-01-22 조회 2,579
16-01-21 조회 2,650
16-01-21 조회 2,514
16-01-21 조회 2,492
16-01-20 조회 2,429
16-01-20 조회 2,396
16-01-20 조회 2,387
16-01-19 조회 2,449
16-01-19 조회 2,488
16-01-18 조회 2,467
16-01-16 조회 2,406
16-01-16 조회 2,335
16-01-16 조회 2,403
16-01-15 조회 2,571
16-01-15 조회 2,467
16-01-15 조회 2,489
16-01-15 조회 2,430
16-01-14 조회 2,462
16-01-14 조회 2,700
16-01-14 조회 2,499
16-01-13 조회 2,498
16-01-13 조회 2,452
16-01-13 조회 2,435
16-01-12 조회 2,511
16-01-11 조회 2,672
16-01-11 조회 2,549
16-01-11 조회 2,384
16-01-02 조회 2,819
15-12-31 조회 2,530
15-12-27 조회 2,631
15-12-27 조회 2,534
15-12-27 조회 2,589
15-12-26 조회 2,549
15-12-26 조회 2,617
15-12-26 조회 2,627
15-12-25 조회 3,314
15-12-25 조회 2,514
15-12-25 조회 2,513
15-12-25 조회 2,477
15-12-24 조회 3,007
15-12-23 조회 2,475
15-12-23 조회 2,600
15-12-23 조회 2,546
15-12-22 조회 2,584
15-12-22 조회 2,578
15-12-22 조회 2,491
15-12-21 조회 2,519
15-12-21 조회 2,556
15-12-21 조회 2,527
15-12-20 조회 2,568
15-12-20 조회 2,735
15-12-20 조회 2,515
15-12-19 조회 2,586
15-12-19 조회 2,571
15-12-19 조회 2,584
15-12-18 조회 2,887
15-12-18 조회 2,571
15-12-18 조회 2,547
15-12-18 조회 2,532
15-12-17 조회 2,522
15-12-17 조회 2,521
15-12-17 조회 2,488
15-12-16 조회 2,501
15-12-16 조회 2,545
15-12-16 조회 2,478
15-12-15 조회 2,497
15-12-15 조회 2,493
15-12-15 조회 2,484
15-12-14 조회 2,564
15-12-14 조회 2,642
15-12-14 조회 2,467
15-12-13 조회 2,441
15-12-13 조회 2,472