【发布时间】:2017-05-15 23:43:16
【问题描述】:
我计划学习 Lua 以满足我的桌面脚本需求。我想知道是否有任何可用的文档,以及标准库中是否有所有需要的东西。
【问题讨论】:
我计划学习 Lua 以满足我的桌面脚本需求。我想知道是否有任何可用的文档,以及标准库中是否有所有需要的东西。
【问题讨论】:
您应该查看 Lua for Windows——Windows 上 Lua 脚本语言的“包含电池的环境”
http://luaforwindows.luaforge.net/
它包括 LuaCOM 库,您可以从中访问 Excel COM 对象。
尝试查看 LuaCOM 文档,其中有一些 Excel 示例:
http://www.tecgraf.puc-rio.br/~rcerq/luacom/pub/1.3/luacom-htmldoc/
我只将它用于非常简单的事情。这是一个让您入门的示例:
-- test.lua
require('luacom')
excel = luacom.CreateObject("Excel.Application")
excel.Visible = true
wb = excel.Workbooks:Add()
ws = wb.Worksheets(1)
for i=1, 20 do
ws.Cells(i,1).Value2 = i
end
【讨论】:
lua 使用 excel 的更复杂的代码示例:
require "luacom"
excel = luacom.CreateObject("Excel.Application")
local book = excel.Workbooks:Add()
local sheet = book.Worksheets(1)
excel.Visible = true
for row=1, 30 do
for col=1, 30 do
sheet.Cells(row, col).Value2 = math.floor(math.random() * 100)
end
end
local range = sheet:Range("A1")
for row=1, 30 do
for col=1, 30 do
local v = sheet.Cells(row, col).Value2
if v > 50 then
local cell = range:Offset(row-1, col-1)
cell:Select()
excel.Selection.Interior.Color = 65535
end
end
end
excel.DisplayAlerts = false
excel:Quit()
excel = nil
另一个例子,可以添加图表。
require "luacom"
excel = luacom.CreateObject("Excel.Application")
local book = excel.Workbooks:Add()
local sheet = book.Worksheets(1)
excel.Visible = true
for row=1, 30 do
sheet.Cells(row, 1).Value2 = math.floor(math.random() * 100)
end
local chart = excel.Charts:Add()
chart.ChartType = 4 — xlLine
local range = sheet:Range("A1:A30")
chart:SetSourceData(range)
【讨论】: