Spreadsheet Workbench: Difference between revisions

From FreeCAD Documentation
m (grammar tweaks)
(add div for units-tab for local ref)
Line 145: Line 145:
* Text horizontal and vertical alignment
* Text horizontal and vertical alignment
* Text style: bold, italic, underline
* Text style: bold, italic, underline
* Display unit for this cell. Please read the [[Spreadsheet_Workbench#Units|Units]] section below.
* <div id="units-tab"></div>Display unit for this cell. Please read the [[Spreadsheet_Workbench#Units|Units]] section below.
* <div id="alias-name"></div>Define an alias-name for this cell. This alias-name can be used in cell formulas and also in FreeCAD Expressions.
* <div id="alias-name"></div>Define an alias-name for this cell. This alias-name can be used in cell formulas and also in FreeCAD Expressions.
While one may use the row and column number in an expression to reference a cell,
While one may use the row and column number in an expression to reference a cell,

Revision as of 03:09, 6 March 2019

Template:InProgress

Introduction

The Spreadsheet Workbench allows you to create and edit spreadsheets, use data from the spreadsheet as parameters in a model, fill the spreadsheet with data retrieved from a model, perform calculations, and export the data to other spreadsheet applications such as LibreOffice or Microsoft Excel.

The Spreadsheet Workbench has been available since FreeCAD 0.15.

Cell Expressions

A spreadsheet cell may contain arbitrary text or an expression. Technically, expressions must start with an equals '=' sign. However, the spreadsheet attempts to be intelligent; if you enter what looks like an expression without the leading '=', one will be added automatically.

Cell expressions may contain numbers, functions, and references to other cells. Cells are referenced by their row (CAPITAL letter) and column (number). Example: B4 + A6

Numbers may use either a comma ',' or a decimal point '.' separating whole digits from decimals.

The constants pi and e are predefined, and must be written in lowercase.

Supported Functions

Mathematical Functions

The mathematical functions listed below are available.

Multiple arguments to a function may be separated by either a semicolon (';') or a comma followed by a space (", "). In the latter case, the comma is converted to a semicolon after entry.

Trigonometric functions use degrees as their default unit; for radian measure, add rad following the first value in an expression. Example: cos(45) is the same as cos(pi rad / 4).

abs(x)

Description: Absolute value

Returns: value >= 0

acos(x)

Description: Arc cosine; -1 <= x <= 1

Returns: acos(x)

asin(x)

Description: Arc sine; -1 <= x <= 1

Returns: asin(x)

atan(x)

Description: Arc tangent

Returns: atan(x)

atan2(y, x)

Description: Arc tangent of y / x;

Returns: atan2(y,x)

ceil(x)

Description: Ceiling

Returns: The smallest integer value greater than or equal to x introduced in version 0.16

cos(angle)

Description: Cosine; angle is in degrees (any value)

Returns: -1 <= cos(x) <= 1

cosh(x)

Description: Hyperbolic cosine of x

Returns: cosh(x)

exp(y)

Description: Natural exponent

Returns: e y

floor(x)

Description: Floor

Returns: The largest integer value less than or equal to x introduced in version 0.16

log(x)

Description: Natural logarithm

Returns: loge x

log10(x)

Description: Logarithm base 10

Returns: log10 x

mod(x, y)

Description: Remainder x/y

Returns: Remainder after dividing x by y

pow(x, y)

Description: Power function

Returns: x y

round(x)

Description:

Returns: x rounded to the nearest integer introduced in version 0.16

sin(angle)

Description: Cosine; angle in degrees (any value)

Returns: -1 <= sin(x) <= 1

sinh(x)

Description: Hyperbolic sine of x

Returns: sinh(x)

sqrt(x)

Description: Square root; x >= 0

Returns: Square root of x

tan(angle)

Description: Tangent; angle in degrees (any value)

Returns: tan(angle)

tanh(x)

Description: Hyperbolic tangent of x

Returns: tanh(x)

trunc(x)

Description: Truncate

Returns: x truncated to the nearest integer introduced in version 0.16

Statistical / Aggregating Functions

The aggregating functions listed below are supported. Aggregating functions take one or more arguments, separated by a semicolon ';' or a comma and a space ", ". Arguments may include references to cells; cell references consist of the row letter (CAPITAL) followed by the column number. Arguments may include ranges of cells (two cell references separated by a colon). Example: average(B1:B8)

average(x y)

Description: Average of values in cells x through y

Returns: sum(x, y) / count(x, y)

count(x y)

Description: Number of cells from x through y

Returns: The number of cells from x through y, inclusive

max(x y)

Description: Maximum value in cells x through y

Returns: Maximum value

min(x y)

Description: Minimum value in cells x through y

Returns: Minimum value

stddev(x y)

Description: Standard deviation of values in cells x through y

Returns: Standard deviation

sum(x y)

Description: Sum of values in cells x through y

Returns: Sum

Conditional Expressions

Conditional expressions are of the form condition ? resultTrue : resultFalse

The condition is defined as an expression that evaluates to either 0 (false) or non-zero (true). The following comparison operators are defined: ==, !=, >, <, >=, and <=.

The conditional statement has a bug regarding nested conditional statements. Only the true-result may contain another conditional statement. This is because parentheses are removed after an expression is entered. Trying to put a nested conditional statement in the false-result may result in incorrect parentheses causing a different result after saving and reopening the document. Note: This may no longer be true; at least some false result conditionals work properly.

Interaction between Spreadsheets and the CAD Model

Data in the cells of a spreadsheet may be used in CAD model parameter expressions. Thus, a spreadsheet may be used as the source for parameter values used throughout a model, effectively gathering the values in one place. When values are changed in the spreadsheet, they are propagated throughout the model.

Similarly, properties from CAD model objects may be used in expressions in spreadsheet cells. This allows use of object properties like volume or area in the spreadsheet. If the name of an object in the CAD model is changed, the change will automatically be propagated to any references in spreadsheet expressions using the name which was changed.

More than one spreadsheet may be used in a document; spreadsheets may be given a user-assigned name like any other object.

FreeCAD checks for cyclic dependencies. Currently that check stops at the level of the spreadsheet object. As a consequence, you should not have a spreadsheet which contains both cells whose values are used to specify parameters to the model, and cells whose values are taken from other parameters in the model. For example, you cannot have a single spreadsheet which has cells specifying the length, width, and height of an object, and which also references the total area of that object. This restriction can usually be surmounted by having two spreadsheets, one specifying input parameters to the model and the other used for calculations based on resultant model parameters.

Cell Properties

The properties of a spreadsheet cell can be edited with a right-click on a cell. The following dialog pops up:

As indicated by the tabs, the following properties can be changed:

  • Text color and background color
  • Text horizontal and vertical alignment
  • Text style: bold, italic, underline
  • Display unit for this cell. Please read the Units section below.
  • Define an alias-name for this cell. This alias-name can be used in cell formulas and also in FreeCAD Expressions.

While one may use the row and column number in an expression to reference a cell, best practice is to give the cell an alias-name and use that. For example, if the data in cell B1 contained the length parameter for an object, an alias name of "MyObject_Length" would allow the value to be referred to as "Spreadsheet.MyObject_Length" instead of "Spreadsheet.B1". introduced in version 0.16


Reference To CAD-Data

As indicated above, one can reference data from the CAD model in spreadsheet expressions.

Computed expressions in spreadsheet cells start with an equals ('=') sign. However, the spreadsheet entry mechanism attempts to be smart. An expression may be entered without the leading '='; if the string entered is a valid expression, an '=' is automatically added when the final Enter is typed. If the string entered is not a valid expression (often the result of entering something with the wrong case, e.g. "MyCube.length" instead of "MyCube.Length"), no leading '=' is added and it is treated as simply a text string.

Note: As of pre v0.18 release, the above behavior (auto insert of '=') has some unpleasant ramifications:

  • If you want to keep a column of names corresponding to the alias-names in an adjacent column of values, you must enter the name in the label column before giving the cell in the value column its alias-name. Otherwise, when you enter the alias-name in the label column the spreadsheet will assume it is an expression and change it to "=<alias-name>"; and the displayed text will be the value from the <alias-name> cell.
  • If you make an error when entering the name in the label column and wish to correct it, you cannot simply change it to the alias-name. Instead, you must first change the alias-name to something else, then fix the text name in the label column, then change the alias-name in the value column back to its original.

One way to side-step these issues is to prefix text labels corresponding to alias-names with a fixed string, thereby making them different. Note that "_" will not work, as it is converted to "=".

The following table shows some examples assuming the model has a feature named "MyCube":

CAD-Data Cell in Spreadsheet Result
Parametric Length of a Part-Workbench Cube =MyCube.Length Length with units mm
Volume of the Cube =MyCube.Shape.Volume Volume in mm³ without units
Type of the Cube-shape =MyCube.Shape.ShapeType String: Solid
Label of the Cube =MyCube.Label String: MyCube
x-coordinate of center of mass of the Cube =MyCube.Shape.CenterOfMass.x x-coordinate in mm without units

Spreadsheet Data in Expressions

The usage of spreadsheet data in other parts of FreeCAD requires a fully defined name. Because it is possible to have more than one spreadsheet in a document, the spreadsheet name together with the cell name or alias is required. The following pictures show the usage of an alias "number" from a spreadsheet "MySheet" in an expression in the PartDesign Workbench.

Typing an "M" shows a list of available names (above). The arrow-buttons allow one to move to "MySheet" and select it; Enter will cause it to be completed in the expression. You can also continue typing or click the entry with the mouse.

Typing an "n" now shows the list of available alias names in MySheet starting with "n" (above). "number" can now be selected as described above. Once a valid name with usable content is entered, the result field will show the calculated value; in this case, the length (below).

Further typing, such as adding a "+" sign, will result in an expression which is no longer valid which will be indicated. Continuing on can again result in a valid expression.

Units

The Spreadsheet uses units. If a number has an associated unit, that unit will be used in all calculations. The multiplication of two lengths with the unit mm gives an area with the unit mm².

You can switch a length unit from mm to inch using the properties dialog units tab (above). The cell will then display the length in inches. However, the value used for calculations does not change, and the results of formulas using the value do not change — the result is still calculated from the length in mm.

A number without a unit cannot be changed to a number with a unit by the cell properties dialog. One can put in a unit string, and that string will be displayed; but the cell still contains only a number without an associated unit.

Occasionally it may be desirable to get rid of a unit. This can only to be done by multiplying by 1 with a reciprocal unit.

Importing and exporting

Sheets can be imported and exported to the csv format which can also be read and written by most other spreadsheet applications such as Microsoft Excel or LibreOffice Calc. When importing files into FreeCAD, the delimiter (the character that is used to separate columns) must be the TAB character (this can be set when exporting from other applications). The import of a CSV-file is available over the menu Spreadsheet/ Import Spreadsheet or by clicking on the icon . This import function does not open Excel files or any other spreadsheet format.

Spreadsheets in Excel-format "xlsx" can be imported via the menu File/Import... into a FreeCAD document. Excel-spreadsheets can also be opened by FreeCAD by clicking in the menu File/Open... or by clicking on the icon . In this case a new document with a spreadsheet inside is created. Supported are the following features:

  • all functions that are also available in the FreeCAD spreadsheet. Other functions do give an error in the corresponding cell after the import.
  • Alias names for cells
  • More than one table in the Excel-sheet. In this case more FreeCAD spreadsheets are created.

Other functionality is not imported into the FreeCAD spreadsheet. The Excel-import is introduced in version 0.17of FreeCAD.

Current Limitations

It is not possible providing data for a geometry, for example a length, in a spreadsheet and retrieving in the same spreadsheet the volume of the resulting shape. This will create a circular reference. This is a design decision. However, it is possible to use two different spreadsheets: one as data-source for geometry and another for reporting geometry-data.

It is not possible to select and copy multiple cells. Only the content of a cell from the input field can be copied and paste into the input field of another cell.

For FreeCAD earlier versions see Spreadsheet legacy


Scripting Basics

import Spreadsheet
sheet = App.ActiveDocument.addObject("Spreadsheet::Sheet")
sheet.Label = "Dimensions"
Sketcher Workbench
Start Workbench