2012-02-08 81 views
1

我有面試問題問的Excel格式的輸出爲:
創建使用HTML和JavaScript

寫JS插件,可以採取細胞和值作爲輸入和呈現Excel的瀏覽器的格式輸出。例如,

例輸入(單元格和值):在瀏覽器

J3 = 5 
A2 = 20 
K1 = 10 

輸出應以Excel格式

A B C ....... J K ....... 
1       10 
2 20 
3     5 
.. 

我一直在尋找的問題,正確的解決方案。

我試圖解決這個問題(寫psudeo代碼)

var cell = {"J3": 5, "A2":20, "K1": 10} 

// Function they will call for generate excel style table 
generateExcel(cell, selector) {  
1. create blank table which has A-Z column (with selector as A-Z resp) and 1 to 100 rows (with selector as 1 to 100 resp) 
2. Loop through each cell and for each cell 
    2.1 find the column (J) and row (3) 
    2.2 Add/replace value in that TD 

3. Once all the information from cell in enter in table, print the table in the document at given selector   

}

他們說,這不會是高效爲單元格輸入數量巨大。我建議我們可以使用Matrix創建表格

A B... J K .... 
1 [    10  ] 
2 20 
3    5 
+0

你有什麼迄今訪問表格單元格?人們應該樂於幫助你優化你的代碼,但如果他們回答你的話,他們不應該得到這份工作=)? – mrtsherman 2012-02-08 03:11:38

+0

你在面試中給了你答案嗎?你有什麼嘗試? – 2012-02-08 03:11:40

回答

0

Excel電子表格是表格。你能用一張簡單的桌子嗎?如果是這樣,我會建議CSS border-collapse屬性使其看起來更好,以及可能減少單元格填充和邊距。

1

我覺得你開始的很好。首先創建一個包含元素的表格。這將是26列寬,並與最大的y值一樣高。將字母轉換爲數字。

對不起W3Schools的鏈接,我有責任得到downvoted甚至提到他們,但他們擁有的表對象,我可以谷歌爲你的最佳佈局文檔。如果有人有更好的東西,我會更新它。

http://www.w3schools.com/jsref/dom_obj_table.asp

MDN教程

https://developer.mozilla.org/en/Traversing_an_HTML_table_with_JavaScript_and_DOM_Interfaces

然後,您可以最有效地通過

var table = ;//get by id or create element, not sure what they expect 
table.rows[y].cells[x].appendChild(...);