CsvWriter

Description

The CsvWriter object is a special DataObjectWriter designed to write datasets as rows containing comma-separated values to an output, usually a File. The format of the output data can be configured through the delimiter and writeHeader properties.

› Inherits:DataObjectWriter

Properties

delimiter

This property holds the delimiter for separating columns in a data row.

› Type:String
› Default:;
› Signal:delimiterChanged()
› Attributes:Writable

error

This property holds the most recently occurred error or CsvWriter.NoError if no error occurred. If the same error occurs multiple times this property does not change. Use the errorOccurred() signal to detect multiple occurrences of the same error.

› Type:Error
› Signal:errorChanged()
› Attributes:Readonly

errorString

This property holds the current human readable error string corresponding to the current value in the error property. It may include additional information such as failure reasons or locations.

› Type:String
› Signal:errorStringChanged()
› Attributes:Readonly

output

This property holds the output device which the CSV data is written to. Usually a File object should be used here.

› Type:IoDevice
› Signal:outputChanged()
› Attributes:Writable

outputMode

This property holds the output mode which defines how new rows are written to the output. See the OutputMode enumeration for details.

› Type:OutputMode
› Default:CsvWriter.OutputAppend
› Signal:outputModeChanged()
› Attributes:Writable

rotationMode

This property holds the rotation mode which allows rotating files periodically in an automated manner. On every data row submission the CsvWriter checks whether a rotation period is elapsed and if necessary closes the current file and opens a file for the new period. The name of the file depends on the configured rotation mode. See the RotationMode enumeration for details.

› Type:RotationMode
› Default:CsvWriter.NoRotation
› Signal:rotationModeChanged()
› Attributes:Writable

writeByteOrderMark

This property holds whether to write the Byte Order Mark (BOM) EF BB BF as the first characters to the output. These BOM characters indicate that UTF-8 encoding should be used when reading the file.

This property was introduced in InCore 2.1.

› Type:Boolean
› Default:false
› Signal:writeByteOrderMarkChanged()
› Attributes:Writable

writeHeader

This property holds whether to write a header with the column names (DataObject.name) to the output.

› Type:Boolean
› Default:true
› Signal:writeHeaderChanged()
› Attributes:Writable

Signals

errorOccurred()

This signal is emitted whenever an error has occurred, regardless of whether the error property has changed or not. In contrast to the change notification signal of the error property this signal is also emitted several times if a certain error occurs several times in succession.

Enumerations

Error

This enumeration describes all errors which can occur in CsvWriter objects. The most recently occurred error is stored in the error property.

Name Value Description
CsvWriter.NoError 0 No error occurred or was detected.
CsvWriter.OutputNotSetError 1 Output not set.
CsvWriter.OutputOpenError 2 Could not open output.

OutputMode

This enumeration describes the output mode which defines how new rows are written to the output.

Name Value Description
CsvWriter.OutputAppend 0 Always append rows to the output. This mode sets the IoDevice.append property to true and clears the IoDevice.truncate and IoDevice.unbuffered properties.
CsvWriter.OutputTruncate 1 Truncate the output on every submission to make it always contain only one row with the latest data. This mode clears the IoDevice.append property and sets the IoDevice.truncate and IoDevice.unbuffered properties to true.
CsvWriter.OutputCustom 2 Open the output without changing the IoDevice.append, IoDevice.truncate and IoDevice.unbuffered properties. This allows implementing a custom output mode by setting these properties manually.

RotationMode

This enumeration describes all supported modes for rotating files periodically.

Name Value Description
CsvWriter.NoRotation 0 Disable periodical file rotation.
CsvWriter.RotateMinutely 1 Rotate every minute with file suffix <yyyyMMddTHHmm>.
CsvWriter.RotateHourly 2 Rotate every hour with file suffix <yyyyMMddTHH00>.
CsvWriter.RotateDaily 3 Rotate every day with file suffix <yyyyMMdd>.
CsvWriter.RotateWeekly 4 Rotate every week with file suffix <yyyyWW>.
CsvWriter.RotateMonthly 5 Rotate every month with file suffix <yyyyMM>.
CsvWriter.RotateYearly 6 Rotate every year with file suffix <yyyy>.

Example

import InCore.Foundation 2.5

Application {

    // record measurements and append new lines after all data objects have been updated
    CsvWriter {
        id: writer1
        Repeater on objects {
            model: 3
            Measurement {
                id: measurement
                name: "meas" + index
                property var updateTimer : Timer {
                    interval: 1000
                    running: true
                    onTriggered: measurement.data = Math.random() * 100;
                }
            }
        }

        output: File {
            fileName: "all-values.csv"
            storage: LocalStorage { }
        }

        outputMode: CsvWriter.OutputAppend
        submitMode: CsvWriter.SubmitOnCompleteDataset
    }

    // continuously update a file in memory which always contains only one line with the most recent values
    CsvWriter {
        objects: writer1.objects

        output: File {
            unbuffered: true
            fileName: "current-values.txt"
            storage: InMemoryStorage { }
        }

        writeHeader: false
        delimiter: " "
        outputMode: CsvWriter.OutputTruncate
        submitMode: CsvWriter.SubmitOnAnyChange
    }
}