Hey,
How are you doing?我是职场编码,
很高兴见到你!
从开号至今,
已累计发布了7条,
C#办公自动化推文,
最近1个多月,
我收到了很多粉丝私信,
都在说,
C#是专门用来,
开发ERP软件专属工具,
C#用来实现办公自动化,
岂不是有点高射炮打蚊子?
C#作为一门,
语法严谨的静态编程语言,
拿它用于实现办公自动化,
确实存在一些不便,
因为我实在受不了Python强制缩进,
于是利用周末时间,
专门研究了一下,
Python竞争对手Ruby,
惊奇地发现,
它和Python一样,
也是一门应用于服务器端,
解释型动态脚本语言,
不同的是,
Ruby语法更精炼,
写起来更舒适。
除了C#,
我们还应该掌握一门脚本语言,
Ruby执行速度并不算最快,
但它的开发速度,
可以与C#抗衡,
接下来的时间,
我将与你分享,
如何使用Ruby实现办公自动化,
下面跟着我,
一起来看看吧!
001 安装Ruby
第一步:登陆Ruby官网
https://rubyinstaller.org/downloads/
第二步:根据你的操作系统,下载对应安装文件
第三步:下载WITHOUT DEVKIT(Ruby 2.7.1-1 (x86))【win7】
第四步:安装Ruby 2.7.1-1 (x86)
第五步:Win+R,运行命令提示符,输入 ruby -v
第六步:如果显示Ruby版本信息,则Ruby系统环境配置成功
【Ruby官网下载速度极慢,回复 软件 获取Ruby安装包。】
002 下载、配置Sublime Text3
你肯定不想在黑框框的CMD命令提示符里写代码,
我给你找了一款代码编辑器IDE->Sublime Text3。
第一步:完成文件解压,将下载好的 Sublime text3.rar 解压到硬盘上。
第二步;双击软件打开,在解压后的文件中双击 sublime_text.exe 打开。
第三步:配置字体信息,依次选择 Preferences - Settings。
第四步:配置主题信息,选择 Preferences - color Scheme - Sixteen。
第五步:设置编程环境,在右下角点选 Ruby。
第六步:输出测试代码。
puts "hello"
回复 软件 获取 Sublime Text3 解压文件。
003 自定义主题风格
打开Sublime Text3根目录,
你会发现主题风格Sixteen是我个人后期修改的,
那么如何实现自定义主题风格呢?
利用解压缩软件,
选中Sublime text3\Packages\Color Scheme - Default.sublime-package文件
右击查看文件,选择 Sixteen,用记事本打开。
{ "name": "Sixteen", "author": "Sublime HQ Pty Ltd, Chris Kempson", "variables": { "darkorchid":"hsl(243, 82.61% , 59.41% )", "firebrick": "hsl(354, 99.10% , 43.53% )",//数字、布尔值 "aliceblue":"hsl(229,63%,95%)",//背景色 "dark":"hsl(0, 0.00% , 0.00% )",//普通文字颜色 "green":"hsl(127, 63.01% , 33.92% )",//注释、字符串 "royalblue":"hsl(237, 65.18% , 43.92% )"//关键字、运算符 }, "globals": { "foreground": "var(dark)",//前景色 "background": "var(aliceblue)",//背景色 "selection": "var(darkorchid)",//选中区域 "selection_border": "var(darkorchid)",//选中区域边框线 }, "rules": [ { // * 注释 * "name": "Comments", "scope": "comment, punctuation.definition.comment", "foreground": "var(green)", "font_style": "bold" }, { // * 标点符号 * "name": "Punctuation", "scope": "punctuation.definition", "foreground": "var(grey6)", }, { // * 关键词 * "name": "Keywords", "scope": "keyword, keyword.operator.word", "foreground": "var(royalblue)", "font_style": "bold" }, { // * 变量 * "name": "Variables", "scope": "variable", "foreground": "var(firebrick)" }, { // * 字符串 * "name": "Strings, Inherited Class", "scope": "string, entity.other.inherited-class", "foreground": "var(green)", "font_style": "bold" }, { // * 转义字符 * "name": "Escape Characters", "scope": "constant.character.escape", "foreground": "var(royalblue)" }, { // * 常数 * "name": "Constants", "scope": "constant", "foreground": "var(firebrick)", "font_style": "bold" }, { // * 运算符 * "name": "Operators", "scope": "keyword.operator", "foreground": "var(royalblue)", "font_style": "bold" }, { // * 函数 * "name": "Functions", "scope": "entity.name.function", "foreground": "var(blue)" }, { // * 类 * "name": "Classes", "scope": "entity.name - (entity.name.section | entity.name.tag | entity.name.label)", "foreground": "var(firebrick)" }, { // * 类库 * "name": "Support Classes", "scope": "support.class", "foreground": "var(royalblue)", "font_style": "bold | italic" }, { // * 正则表达式 * "name": "Regular Expressions", "scope": "string.regexp", "foreground": "var(royalblue)" }, { // * 正则表达式运算符 * "name": "Regular Expressions Operator", "scope": "string.regexp keyword.operator", "foreground": "var(pink)" }, ] }
在"variables"代码块里声明颜色变量,
需要注意的是颜色格式是"hsl()类型",
我们可以到
http://www.yuangongju.com/color转换颜色类型。
接着,在"rules"代码块里,
通过var()调用已定义好的颜色属性,
代码都是通俗易懂的,
如:
"foreground"代表前景色,
"name"代表规则名称,
"font_style"代表字体格式。
当然还有一些常用的,我就不一一列举了。
注:使用解压缩文件直接打开Color Scheme - Default.sublime-package,一定要把Sublime Text3关闭,不然会报错。
【报错后文件会损毁!!!】
004 运行Ruby代码
require "win32ole" # 生成Excel应用 Eap = WIN32OLE::new('Excel.Application') Eap.Visible = true Ebk = Eap.WorkBooks.Add Ebk.Sheets[1].Cells(1,1).value="很高兴与你见面"Ebk.Sheets[1].Cells(1,1).Font.Size = 32Ebk.Sheets[1].Cells(2,1).value="我是职场编码"Ebk.Sheets[1].Cells(2,1).Font.Size = 32# 生成Word应用Wap = WIN32OLE::new('Word.Application') Wap.Visible = true Wdc = Wap.Documents.Add Wdc.Paragraphs.Last.Range.Text="请关注我的GZH"Wdc.Paragraphs.Last.Range.Font.Size = 32Wdc.Paragraphs.Add Wdc.Paragraphs.Last.Range.InsertAfter("K31389")Wdc.Paragraphs.Last.Range.Font.Size = 32# 生成Powerpoint应用Pap = WIN32OLE::new('Powerpoint.Application') Pap.Visible = true Pst = Pap.Presentations.AddPsd = Pst.Slides.Add(1,2)Psd.Shapes(1).Textframe.Textrange.Text="你我相遇,实属不易"Psd.Shapes(2).Textframe.Textrange.Text="且行且珍惜!"
回复 R,获取 Ruby 源代码。
注:双击以.rb结尾的文件运行,用Sublime Text3等文本文件打开,即可获得源代码。