Hi,How are you doing?
我是職場編碼(CodeVoc)。
在E000中,我們介紹了Node.js、Ruby、Electron等工具下載安裝。
這期,給你演示一下由Electron聯合Ruby製作的小工具。
播放
暫停
進入全屏
退出全屏
00:00
00:00
重播請
刷新
試試藉助Electron官方Demo,我們很容易製作一個工具展示平臺。
點擊“View Demo”會彈出我們的工具界面。
一、項目需求
這個工具的主要目的是為了把Excel題庫數據寫入Word生成試題。
它除了可以選擇源文件,還可以選擇目標文件。
點擊“寫入”按鈕,就可以把Excel題庫數據寫入Word生成試題。
二、界面設計
【html】
生成表單容器:
生成佈局標籤:
生成行內標籤:
生成單行輸入框:
生成文件選擇按鈕:
<code> <input
type
="file"
id="goal_file"
> <input
type
="file"
id="source_file"
> /<code>
生成普通按鈕:
【css】
關注四點前白後綠氣泡某杺平臺,搜索“職場編碼”查看源碼。
【javascript】
根據ID,選中source_file按鈕
<code>var
source_line=document
.getElementById('source_line'
) /<code>
給source_file按鈕,添加"change"事件
<code>source_line.value
=document.getElementById('source_file'
).files[0
].path /<code>
根據ID,選中goal_file按鈕,
<code>var
goal_line=document
.getElementById('goal_line'
) /<code>
給goal_file按鈕,添加"change"事件
<code>goal_file.value
=document.getElementById('goal_file'
).files[0
].path /<code>
execute按鈕添加單擊事件
<code>execute
.addEventListener("click"
,function
()
{獲取參數1
,獲取參數2
,調用}) /<code>
獲取參數1
<code>var
source_line=document.getElementById('source_line'
).value
/<code>
獲取參數2
<code>var
goal_line=document.getElementById('goal_line'
).value
/<code>
調用Ruby腳本
<code>const
{ spawn } =require
('child_process'
)const
ls = spawn('ruby'
, ['Ruby腳本完整路徑'
,參數1
,參數2
]) /<code>
三、邏輯梳理
=> 基礎語法
引用Ruby標準庫
<code>require
"win32ole"
/<code>
創建雙參數入口方法
<code>def
Excel_to_word
(pth_source,pth_goal)
/<code>
接收控制檯傳雙參
<code>Excel_to_word
(ARGV
[0]
,ARGV
[1]
)/<code>
創建二維數組
<code>arr
=Arrayarr
=ebk.worksheets(1
).range("a1:g150"
).value/<code>
設置循環
<code>(0
..149
).each{|i|
(0
..6
).each{|j|
} }/<code>
=> 對象模型
創建可視化Excel、Word應用
<code>@eap
=WIN32OLE
::new("excel.application"
);@eap
.visible=true@wap
=WIN32OLE
::new("word.application"
);@wap
.visible=true/<code>
打開Excel工作簿、Word文檔
<code>ebk
[email protected](pth_source)wdc
[email protected](pth_goal)/<code>
執行數組寫入Word段落操作
<code>wdc.paragraphs(m).range.text=arr[i
][j
]/<code>
關注四點前白後綠氣泡某杺平臺,搜索“職場編碼”查看源碼。