Hi,How are you doing?
我是職場編碼(CodeVoc)。
在E000中,我們介紹了Node.js、Ruby、Electron等工具下載安裝。
這期,給你演示一下由Electron聯合Ruby製作的小工具。
播放
暫停
進入全屏
退出全屏
00:00
00:00
重播請
刷新
試試藉助Electron官方Demo,我們很容易製作一個工具展示平臺。
點擊“View Demo”會彈出我們的工具界面。
一、項目需求
這個工具的主要目的是為了把文字內容寫入Excel表格、Word文檔、Ppt演示文稿。
它可以選擇待寫入文件類型,選擇文件類型後,在文本框輸入文字信息。
點擊“寫入”按鈕,依次打開Excel、Word、Ppt文件,顯示我們寫入的文字內容。
二、界面設計
【html】
生成表單容器:
生成佈局標籤:
生成下拉選擇按鈕:
生成文本框:
生成普通按鈕:
【css】
關注四點前白後綠氣泡某詞平臺,搜索“職場編碼”查看源碼。
【javascript】
execute按鈕添加單擊事件
<code>execute
.addEventListener("click"
,function
()
{獲取參數1
,獲取參數2
,調用}) /<code>
獲取參數1
<code>var
select_name=document.getElementById('select_name'
).value
/<code>
獲取參數2
<code>var
input_word=document.getElementById('input_word'
).value
/<code>
調用Ruby腳本
<code>const
{ spawn } =require
('child_process'
)const
ls = spawn('ruby'
, ['Ruby腳本完整路徑'
,參數1
,參數2
]) /<code>
三、邏輯梳理
=> 基礎語法
引用Ruby標準庫
<code>require
"win32ole"
/<code>
創建主入口方法、分入口方法
<code>def
Main
(name,txt)
def
New_excel
(txt)
def
New_word
(txt)
def
New_ppt
(txt)
/<code>
接收控制檯傳雙參
<code>Main
(ARGV
[0]
,ARGV
[1]
)/<code>
對參數ARGV[0] => name進行判斷,根據判斷結果執行分入口方法
<code>case
namewhen
"excel"
then
New_excel(txt)when
"word"
then
New_word(txt)when
"ppt"
then
New_ppt(txt) /<code>
=> 對象模型
創建可視化Excel、Word、Ppt應用
<code>eap
=WIN32OLE::new("excel.application"
)wap
=WIN32OLE::new("word.application"
)pap
=WIN32OLE::new("powerpoint.application"
)/<code>
新建Excel空白工作簿、Word文檔、Ppt演示文稿\幻燈片
<code>ebk
=eap.workbooks.addwdc
=wap.documents.addpst
=pap.presentations.add/<code>
執行寫入Excel單元格、Word段落、Ppt文本框操作
<code>ebk.worksheets(1
).cells(1
,1
).value
=txt wdc.paragraphs(1
).range.text=txt psd.shapes(1
).textframe.textrange.text=txt/<code>
歡迎關注四點前白後綠氣泡某茉平臺,關注“職場編碼”查看源碼。