Search code examples
excelcsvlua

converting lua file to csv or tsv or any excel database file


I have a lua file that is a database of all my raids in World of Warcraft game. It is generated by an addon in the game and has the information of which player received which loot for which price. Like this:

GDKPd_PotData = {
["playerBalance"] = {
},
["curPotHistory"] = {
},
["history"] = {
    {
        ["date"] = "Thu Apr 29 14:54:21 2021",
        ["note"] = false,
        ["items"] = {
            {
                ["bid"] = 225,
                ["item"] = "|cffa335ee|Hitem:19866::::::::60:::::::|h[Warblade of the Hakkari]|h|r",
                ["name"] = "Brighht",
            }, -- [1]
            {
                ["bid"] = 50,
                ["item"] = "|cff0070dd|Hitem:19907::::::::60:::::::|h[Zulian Tigerhide Cloak]|h|r",
                ["name"] = "Treasuredon",
            }, -- [2]
            {
                ["bid"] = 50,
                ["item"] = "|cffa335ee|Hitem:19897::::::::60:::::::|h[Betrayer's Boots]|h|r",
                ["name"] = "Sassysis",
            }, -- [3]
            {
                ["bid"] = 50,
                ["item"] = "|cff0070dd|Hitem:19895::::::::60:::::::|h[Bloodtinged Kilt]|h|r",
                ["name"] = "Yabmage",
            }, -- [4]
            {
                ["bid"] = 180,
                ["item"] = "|cffa335ee|Hitem:19856::::::::60:::::::|h[The Eye of Hakkar]|h|r",
                ["name"] = "Consti",
            }, -- [5]
            {
                ["bid"] = 100,
                ["item"] = "|cffa335ee|Hitem:19802::::::::60:::::::|h[Heart of Hakkar]|h|r",
                ["name"] = "Consti",
            }, -- [6]
            {
                ["bid"] = 300,
                ["item"] = "|cff0070dd|Hitem:22637::::::::60:::::::|h[Primal Hakkari Idol]|h|r",
                ["name"] = "Gnomepowah",
            }, -- [7]
            {
                ["bid"] = 220,
                ["item"] = "|cff0070dd|Hitem:22637::::::::60:::::::|h[Primal Hakkari Idol]|h|r",
                ["name"] = "Lockbik",
            }, -- [8]
            {
                ["bid"] = 110,
                ["item"] = "|cffa335ee|Hitem:20725::::::::60:::::::|h[Nexus Crystal]|h|r",
                ["name"] = "Aeto",
            }, -- [9]
        },
        ["size"] = 1285,
    }, -- [1]

My idea was to convert it into an excel where I could create statistics and graphics and manage the info.

I looked all through the web and couldn't find any program that would convert lua into any excel file formats (xls,csv,tsv)

Can anyone give me some ideas on how to best do this?


Solution

  • CSV and TSV is basically the same thing you just have a different delimiter.

    Writing .xls or xlsx is a bit more complicated.

    If you want to use xlsx you open a web brower and enter "lua xlsx" into any websearch. You'll find https://github.com/jmcnamara/xlsxwriter.lua for example.

    --
    -- A simple example of some of the features of the xlsxwriter module.
    --
    
    local Workbook = require "xlsxwriter.workbook"
    
    local workbook  = Workbook:new("demo.xlsx")
    local worksheet = workbook:add_worksheet()
    
    -- Widen the first column to make the text clearer.
    worksheet:set_column("A:A", 20)
    
    -- Add a bold format to use to highlight cells.
    local bold = workbook:add_format({bold = true})
    
    -- Write some simple text.
    worksheet:write("A1", "Hello")
    
    -- Text with formatting.
    worksheet:write("A2", "World", bold)
    
    -- Write some numbers, with row/column notation.
    worksheet:write(2, 0, 123)
    worksheet:write(3, 0, 123.456)
    
    workbook:close()
    

    Writing CSV or TSV is trivial. You just need to write the values to a file using Lua's io library and separate them by a comma or a tab.

    Both solutions require you to traverse over your table recursively using a few for loops.