|
Hi
I am getting nowhere fast trying to figure out how to make a chart in Excel. I took Thomas's code snippet and started to try and adapt it to be a standalone small chart demo in Lua. I cant see what I am doing wrong so if anyone could assist I would be most grateful. The Internet is completely devoid of any example Lua scripts for making charts. I am getting lots of errors when trying to set properties in the graph function. One example error lua: COM exception:(.\src\library\tLuaCOM.cpp,394):Unable to set the HasTitle property of the Chart class stack traceback: Thanks for any help Geoff For reference here is my Lua example code so far require "luacom" local rgbMediumSeaGreen = 7451452 local xlToRight = -4161 local xlXYScatter = -4169 local xlValue = 2 local xlCategory = 1 function AddGraph(graphsheet, datasheet, color) color = color or rgbMediumSeaGreen local numMachines = datasheet.Rows(1):End(xlToRight).Column / 5 -- 10 pixel per machine creates a dense graph that's good for overview, -- though the individual labels aren't visible anymore local width, height = 500, numMachines * 10 local xOffset, yOffset = 32, 32 local chart = graphsheet:ChartObjects():Add(xOffset, yOffset, width, height).Chart --#ERROR chart.HasTitle = true --#ERROR --~ chart.ChartTitle.Text = graphsheet.Name .. " - " .. "the graph for the data" chart.ChartType = xlXYScatter local axis = chart:Axes(xlValue) --#ERROR --~ axis.ReversePlotOrder = true --#ERROR --~ axis.MajorUnit = 1 axis = chart:Axes(xlCategory) axis.TickLabels.NumberFormatLinked = false -- to prevent "scientific" tick labels axis.TickLabels.NumberFormat = "General" -- auto-size -- this is now highly dependent on the data sheet layout, nevermind the details -- it's about adding new series to the newly created chart, specifying the ranges -- of data in 'data'sheet' to use local startingColumn = 1 local xValuesOffset = 1 -- column offset 1: measured data local valuesOffset = 4 -- column offset 4: ordinal number local seriesNumber = 1 local series = "MyDataSeries" while datasheet.Cells(2, startingColumn).Formula ~= "" do print((" adding series %d (%s)"):format(seriesNumber, series)) -- next machine data set, find out how far down it goes local lastRow = datasheet.Columns(startingColumn + xValuesOffset):End(xlenums.XlDirection.xlDown).Row local series = chart:SeriesCollection():NewSeries() series.Name = datasheet.Cells(2, startingColumn) series.XValues = datasheet:Range(datasheet.Cells(2, startingColumn + xValuesOffset), datasheet.Cells(lastRow, startingColumn + xValuesOffset)) series.Values = datasheet:Range(datasheet.Cells(2, startingColumn + valuesOffset), datasheet.Cells(lastRow, startingColumn + valuesOffset)) series.MarkerSize = 2 series.MarkerStyle = xlenums.XlMarkerStyle.xlMarkerStyleX series.MarkerForegroundColor = color seriesNumber = seriesNumber + 1 startingColumn = startingColumn + 5 end return chart end excel = luacom.CreateObject("Excel.Application") local book = excel.Workbooks:Add() local sheet1 = book.Worksheets(1) local sheet2 = book.Worksheets(2) excel.Visible = true excel.DisplayAlerts = false math.randomseed(1234) for row=1, 10 do for col=1, 10 do sheet1.Cells(row, col).Value2 = math.floor(math.random() * 100) end end AddGraph(sheet1, sheet2) Date: Thu, 30 Apr 2015 10:38:14 -0300 From: iburgueno@gmail.com To: lua-l@lists.lua.org Subject: Re: Frustration with Luacom and Excel On Thu, Apr 30, 2015 at 8:51 AM, Geoff Smith <spammealot1@live.co.uk> wrote:
...
That is indeed the case. The cost of an out-of-process call in COM is way bigger that an in-process call. You can try a different thing. LuaCOM can act as a COM component, registering a COM interface so that other processes can consume it. So you can split your code in two. One part, opens Excel, and adds a macro to it. That macro (VBA code) will create an object of this new interface and call it. So you will have your Lua code running within the Excel process. All your calls will now be in-process and you get a huge speedup. However, as you can see, the setup process is quite convoluted.
You can post this samples in the wiki: http://lua-users.org/wiki/ |