Hi,How are you doing?
我是职场编码(CodeVoc)。
在E000中,我们介绍了Node.js、Ruby、Electron等工具下载安装。
这期,给你演示一下由Electron联合Ruby制作的小工具。
知乎视频www.zhihu.com借助Electron官方Demo,我们很容易制作一个工具展示平台。
点击“View Demo”会弹出我们的工具界面。
一、项目需求
这个工具的主要目的是为了把Excel数据批量写入Word生成工资字条。
它除了可以选择源文件,还可以选择目标文件。
点击“写入”按钮,完美执行写入操作。
二、界面设计
【html】
生成表单容器:<form></form>
生成布局标签:<div></div>
生成行内标签:<span></span>
生成单行输入框:<input id="source_line"><input id="goal_line">
生成文件选择按钮:
<input type="file" id="goal_file">
<input type="file" id="source_file">
生成普通按钮:<input type="button" id="execute">
【css】
关注四点前白后绿气泡某杺平台,搜索“职场编码”查看源码。
【javascript】
根据ID,选中source_file按钮
var source_line=document.getElementById('source_line')
给source_file按钮,添加"change"事件
source_line.value=document.getElementById('source_file').files[0].path
根据ID,选中goal_file按钮,
var goal_line=document.getElementById('goal_line')
给goal_file按钮,添加"change"事件
goal_file.value=document.getElementById('goal_file').files[0].path
execute按钮添加单击事件
execute.addEventListener("click",function(){获取参数1,获取参数2,调用})
获取参数1
var source_line=document.getElementById('source_line').value
获取参数2
var goal_line=document.getElementById('goal_line').value
调用Ruby脚本
const { spawn } = require('child_process')
const ls = spawn('ruby', ['Ruby脚本完整路径',参数1,参数2])
三、逻辑梳理
=> 基础语法
引用Ruby标准库
require "win32ole"
创建双参数入口方法
def Excel_to_table(pth_source,pth_goal)
接收控制台传双参
Excel_to_table(ARGV[0],ARGV[1])
设置条件判断
if 条件表达式
elsif 条件表达式
else
end
设置双循环
s=2;s.step(20,2) do |s|
(1..11).each do |j|
end
end
=> 对象模型
创建可视化Excel、Word应用
@eap=WIN32OLE::new("excel.application");@eap.visible=true
@wap=WIN32OLE::new("word.application");@wap.visible=true
打开Excel工作簿、Word文档
ebk=@eap.workbooks.open(pth_source)
wdc=@wap.documents.open(pth_goal)
确定表格行数、创建表格
max=@eap.Worksheets(1).usedrange.currentregion.rows.count-1
wdc.tables.add(wdc.paragraphs(1).range,max*2,11);
表格格式化
@wtb=wdc.tables(1)
@wtb.borders.insidelinestyle=1 #内实线
@wtb.borders.outsidelinestyle=1 #外实线
@wtb.range.paragraphformat.alignment=1 #居中
@wtb.cell(s,j).range.font.name="黑体"
执行Excel数据写入Word操作、日期格式化为“年-月”
@wtb.cell(s,j).range.text=@eap.worksheets(1).cells(i,j).value.strftime("%Y-%m")
关注四点前白后绿气泡某杺平台,搜索“职场编码”查看源码。