슈

엑셀 흉내내기

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

<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-06-03 조회 3,027
15-07-30 조회 3,417
15-06-08 조회 5,556
15-05-27 조회 4,415
15-01-08 조회 6,715
14-12-30 조회 5,447
14-10-10 조회 5,437
14-08-13 조회 6,355
14-08-11 조회 5,208
14-08-06 조회 6,548
14-03-19 조회 7,437
13-04-26 조회 9,596
12-12-15 조회 5,117
12-12-15 조회 7,352
12-12-11 조회 5,493
12-11-21 조회 6,024
12-10-11 조회 5,105
12-10-03 조회 4,796
12-09-20 조회 6,094
12-09-02 조회 4,272
12-08-29 조회 5,281
12-08-29 조회 4,163
12-08-24 조회 4,262
12-08-18 조회 4,482
12-08-17 조회 4,527
12-08-16 조회 3,782
12-08-06 조회 5,556
12-08-05 조회 4,844
12-08-03 조회 4,561
12-08-03 조회 4,707
12-07-29 조회 3,562
12-07-24 조회 4,150
12-07-22 조회 3,615
12-07-20 조회 4,376
12-07-19 조회 4,718
12-07-19 조회 3,950
12-07-19 조회 3,896
12-07-19 조회 5,561
12-07-19 조회 3,506
12-07-18 조회 5,190
12-07-15 조회 4,171
12-07-14 조회 3,842
12-07-12 조회 4,515
12-07-11 조회 3,788
12-07-11 조회 3,510
12-07-11 조회 3,486
12-07-10 조회 4,673
12-07-09 조회 3,421
12-07-09 조회 4,238
12-07-09 조회 4,658
12-07-08 조회 4,572
12-07-07 조회 5,495
12-07-06 조회 4,750
12-07-06 조회 4,015
12-07-06 조회 3,902
12-07-06 조회 3,841
12-07-02 조회 5,518
12-07-02 조회 4,215
12-06-29 조회 3,737
12-06-28 조회 4,363
12-06-26 조회 3,858
12-06-26 조회 4,977
12-06-21 조회 3,955
12-06-20 조회 4,657
12-06-20 조회 4,295
12-06-15 조회 4,647
12-06-15 조회 4,382
12-06-13 조회 7,398
12-06-05 조회 7,371
12-06-04 조회 3,947
12-06-03 조회 3,798
12-05-31 조회 3,793
12-05-28 조회 6,639
12-05-27 조회 3,873
12-05-25 조회 4,006
12-05-23 조회 4,416
12-05-21 조회 5,128
12-05-17 조회 4,064
12-05-17 조회 3,977
12-05-09 조회 4,055
12-05-07 조회 4,315
12-05-02 조회 4,499
12-05-01 조회 4,678
12-04-30 조회 3,426
12-04-30 조회 4,371
12-04-25 조회 4,458
12-04-25 조회 4,882
12-04-24 조회 3,845
12-04-24 조회 4,616
12-04-23 조회 3,611
12-04-20 조회 3,267
12-04-17 조회 5,451
12-04-16 조회 4,386
12-04-14 조회 3,671
12-04-14 조회 3,700
12-04-14 조회 3,744
12-04-13 조회 3,375
12-04-13 조회 4,560
12-04-13 조회 6,203
12-04-12 조회 3,367