Skip to content

ExportCSV

Anthony Headley edited this page May 20, 2020 · 4 revisions

ExportCSV(string:filename, table: data) : bool

Brief:

Exports a text file in CSV format of the specified data to the selected file name.

❓ Seems to not write to Internal disk.

Paramiters:

Name Description Optional
string : filename The full path and file name to store the file. Should grab the path using one of the methouds in the example since paths differ depening on drive and operation system.
table : data The data you want to save to disk

Returns:

⚠ Should return bool but seems to always return nil

bool : true = Success writing file bool : true = Failed to write file

Examples:

local filename = "ExportFile.JSON"
local drives = Root().Temp.DriveCollect
local sep = GetPathSeparator()
local selectedDrive -- users selected drive
local options = {}  -- popup options

-- data to store, three levels of tables
data = {
    "Level 1.1",
    "Level 1.2",
        {
            "Level 2.1",
            "Level 2.2",
                {
                    "Level 3.1",
                    "Level 3.2"
                }
        }
}


-- grab a list of connected drives
for i = 1, drives.count , 1 do
    table.insert(options, string.format("%s (%s)", drives[i].name, drives[i].DriveType))      
end

-- present a popup for the user choose (Internal may not work)
selectedDrive = PopupInput("Select a disk", display_handle, options)

-- if the user cancled then exit the plugin
if selectedDrive == nil then
    return
end

-- grab the export path for the se;ected drive and append the file name
local exportPath = GetPathOverrideFor("export", drives[selectedDrive + 1].path) .. sep .. filename

-- export the JSON the the selected path
local result = ExportCSV(exportPath, data) -- as of 1.1.3.2 always returns nil

-- sync the files to disk when done and before your user unplugs the device
SyncFS()

--written in the negitive since it seems to always return nil in 1.1.3.2
if result == false then
    Echo("Failed to write to " .. exportPath)
else
    Echo("Wrote file to " .. exportPath)
end
Clone this wiki locally