Skip to main content

CSV

CSV is a table written as plain text: one row per line, a separator between the cells, and usually a first line of column names. Every spreadsheet exports it, so it is the shortest way from a table of measurements, coordinates or prices to geometry. bitbybit.csv reads it and writes it.

Reading a table​

  • parseToArray gives every row as a list of cell texts. Nothing is converted to numbers.
  • parseToJson gives one object per row, keyed by the column names of the header row. numberColumns names the columns to read as numbers.
  • parseToJsonWithHeaders does the same for a file with no header line, with the names you give.
  • queryColumn lists the values of one column, found by its name. With asNumber on, they come back as numbers.
  • queryRowsByValue keeps the rows whose cell in a column equals a value.
  • getHeaders, getRowCount and getColumnCount describe the table.

Every reader takes the row and column separators, a line break and a comma unless you say otherwise. A semicolon is common in countries that write decimals with a comma. Written in a Rete or Blockly field, \n and \t are read as a line break and a tab, so a whole table fits in one text field.

A cell in double quotes may hold the separator, and a quote inside it is written twice.

A table into points​

Here a table holds the x, y and z of five points. Each column is read as numbers, the three columns are turned on their side into points with bitbybit.lists.flipLists, and one smooth curve passes through them:

A curve through points read from a CSV table
Bitbybit Platform

A curve through points read from a CSV table in Rete

rete logoRete

In a project, keep the table as a file and load it with bitbybit.asset.getTextFile (see Assets). A changed file changes the model.

Writing a table​

  • arrayToCsv writes a list of rows, each a list of cells. A cell that holds a separator, a quote or a line break is quoted.
  • jsonToCsv writes a list of objects with the columns you name, in that order.
  • jsonToCsvAuto takes the columns from the first object's properties.

bitbybit.asset.download then saves the text as a .csv file, for a parts list or the measurements of a model.

Every function​

FunctionWhat it does
arrayToCsvWrites a list of rows, each a list of cells, as CSV text; a cell holding a separator, a quote or a line break is wrapped in double quotes.
getColumnCountCounts the cells of the first non-blank row, which is the number of columns; empty text gives 0.
getHeadersReads the cells of row headerRow as the header names; a row index past the end throws an error.
getRowCountCounts the data rows: all non-blank lines minus the ones before dataStartRow, or minus one header line when hasHeaders is true and dataStartRow is left out.
jsonToCsvWrites a list of objects as CSV text with the columns you name in headers, in that order; a property an object lacks becomes an empty cell.
jsonToCsvAutoWrites a list of objects as CSV text using the property names of the first object as the columns, in their order; an empty list gives empty text.
parseToArraySplits CSV text into a list of rows, each a list of cell strings; nothing is converted to numbers.
parseToJsonTurns CSV text into a list of objects, one per data row, keyed by the header names of row headerRow.
parseToJsonWithHeadersTurns CSV text into a list of objects keyed by the headers you give, for files without a header line; a header line the file does have is skipped by setting dataStartRow past it.
queryColumnLists every value of one column, found by its header name, in row order; a row without that cell gives an empty string.
queryRowsByValueKeeps only the rows whose cell in column equals value, giving them as objects keyed by the headers.