SpreadsheetFile
Package: Libraries.Interface.Controls.Support · Inherits from: Libraries.Language.Object
The SpreadsheetFile class reads the text of a comma separated value (CSV) file into rows, and writes rows back as text, so that a file opened and saved without changes is the same as it was. It reads the whole text, not line by line, so a quoted value can hold a line break. It remembers how each value was written (quoted or not), how each line ended (a line feed, a carriage return and line feed, or a carriage return), whether the last line ended with a line break, and whether the file started with a byte order mark. Every value is text: nothing is turned into numbers or dates, and a value starting with = is text like any other. When writing, a value read from the file and not changed is written exactly as it was. A changed value is written plain, or in quotes when it holds a comma, a quote, or a line break, with each quote inside doubled. A file that isn't valid CSV, such as one with a quoted value that never closes, is still read: the problem is listed in GetProblems, the value takes the rest of the file, and writing gives valid CSV.
Example Code
use Libraries.Interface.Controls.Support.SpreadsheetFile
use Libraries.Interface.Controls.Support.SpreadsheetRow
use Libraries.Containers.Array
SpreadsheetFile csv
Array<SpreadsheetRow> rows = csv:Read("Name,Year
Ada,1815")
output rows:Get(1):GetValue(0)
output csv:Write(rows)
Actions
Encode(text value)
This action returns one value as CSV writes it: plain, or in quotes with each quote doubled when it holds a comma, a quote, a carriage return or a line feed.
Parameters
valuetext: The value.
Returns
text: The value as written in a CSV file.
Example
use Libraries.Interface.Controls.Support.SpreadsheetFile
SpreadsheetFile csv
output csv:Encode("Las Vegas, NV")
GetProblems()
This action returns the problems found in the text last read, or an empty array when it was valid CSV.
Returns
Libraries.Containers.Array<Libraries.Interface.Controls.Support.SpreadsheetFileProblem>: The problems, in the order they were found.
HasByteOrderMark()
This action returns true if the text last read started with a byte order mark. Writing puts it back.
Returns
boolean: True if there was a byte order mark.
Read(Libraries.System.File file)
This action reads a CSV file into rows, as Read(text) does with the file's text.
Parameters
fileLibraries.System.File: The file to read.
Returns
Libraries.Containers.Array<Libraries.Interface.Controls.Support.SpreadsheetRow>: The rows, in order.
Example
use Libraries.Interface.Controls.Support.SpreadsheetFile
use Libraries.Interface.Controls.Support.SpreadsheetRow
use Libraries.Containers.Array
use Libraries.System.File
File file
file:SetPath("Data.csv")
SpreadsheetFile csv
Array<SpreadsheetRow> rows = csv:Read(file)
Read(text contents)
This action reads CSV text into rows, one for each line of the file. An empty text gives no rows. A file ending with a line break gives no empty row after it.
Parameters
contentstext: The text of a CSV file.
Returns
Libraries.Containers.Array<Libraries.Interface.Controls.Support.SpreadsheetRow>: The rows, in order.
Example
use Libraries.Interface.Controls.Support.SpreadsheetFile
use Libraries.Interface.Controls.Support.SpreadsheetRow
use Libraries.Containers.Array
SpreadsheetFile csv
Array<SpreadsheetRow> rows = csv:Read("a,b,c")
output rows:Get(0):GetSize()
SetByteOrderMark(boolean mark)
This action sets whether writing starts with a byte order mark.
Parameters
markboolean: True to write a byte order mark.
Write(Libraries.Containers.Array<Libraries.Interface.Controls.Support.SpreadsheetRow> rows, Libraries.System.File file)
This action writes rows to a CSV file, replacing what was in it.
Parameters
rowsLibraries.Containers.Array<Libraries.Interface.Controls.Support.SpreadsheetRow>: The rows to write.fileLibraries.System.File: The file to write.
Example
use Libraries.Interface.Controls.Support.SpreadsheetFile
use Libraries.Interface.Controls.Support.SpreadsheetRow
use Libraries.Containers.Array
use Libraries.System.File
File file
file:SetPath("Data.csv")
SpreadsheetFile csv
Array<SpreadsheetRow> rows = csv:Read(file)
csv:Write(rows, file)
Write(Libraries.Containers.Array<Libraries.Interface.Controls.Support.SpreadsheetRow> rows)
This action writes rows as CSV text. Values read from a file and not changed are written exactly as they were; changed values are quoted only when they need to be. Each row ends as it did in the file.
Parameters
rowsLibraries.Containers.Array<Libraries.Interface.Controls.Support.SpreadsheetRow>: The rows to write.
Returns
text: The CSV text.
Example
use Libraries.Interface.Controls.Support.SpreadsheetFile
use Libraries.Interface.Controls.Support.SpreadsheetRow
use Libraries.Containers.Array
SpreadsheetFile csv
Array<SpreadsheetRow> rows = csv:Read("a,b")
rows:Get(0):SetValue(1, "c, d")
output csv:Write(rows)