Writing Spreadsheets from the Linux Command Line

From VisiCalc and Lotus 1-2-3 to Microsoft Excel, the spreadsheet has been an essential application available on just about every desktop system since the Apple II. On Linux, there are various spreadsheets available including the ones built into OpenOffice and LibreOffice, plus there is a Linux version of Gnumeric. These are all graphical spreadsheets which are run from the desktop. But what about the command line? If you can have fully featured text editors like Vi, Nano and Emacs running from the command line, what about a spreadsheet?

The good news is that Linux has a command line spreadsheet called “spreadsheet calculator” or “sc.” To install it on Ubuntu, use:

sudo apt-get install sc

To start the program, just type “sc.” The initial screen is a blank sheet which is lined across the top and down the side with the familiar spreadsheet number scheme, i.e. letters across the top and numbers down the side.

sc-blank-sheet

Like a “normal” spreadsheet, you enter values and equations into the cells and the spreadsheet calculates the needed results. However, unlike a normal spreadsheet, you can’t just type values into the cells directly. To enter numeric constants and strings, you need to proceed the value with an editing command. To enter a numeric constant, you first need to type “=” and then the number. Here is a list of some of the common editing commands:

  • = Enter a numeric constant or expression into the current cell. sc prompts for the expression on the top line.
  • < Enter a label string into the current cell to be flushed left against the left edge of the cell.
  • \ Enter a label string into the current cell to be centered in the column.
  • > Enter a label string into the current cell to be flushed right against the right edge of the cell.

There is a way to change the way values are entered. Called the “quick numeric entry mode,” you need to start sc with the “-n” option, or type  CTRL + T and then “n” in the program. In this mode, as soon as you start typing a number, the prompt will appear. What this means is that you can skip typing “=”.

sc-values

Equations are entered in the same way as in a regular spreadsheet. Type “=” followed by the equation, e.g. “=B0+B1+B2+B3″

sc-sum

The built-in functions operate a little different than those in other spreadsheets in that they start with @, so to get the sum of a range you need to use @sum (range), where “range” is the start, and end cell names separated by a colon, e.g. C5:C17.

sc-equ

Other essential functions include:

  • @avg() – Average all valid (nonblank) entries in the specified region.
  • @max() – Return the maximum value in the specified region.
  • @min() – Return the minimum value in the specified region.
  • @sqrt() – Return the square root of e.
  • @max() – Return the maximum of the values of the expressions. Two or more expressions may be specified.
  • @min() – Return the minimum of the values of the expressions. Two or more expressions may be specified.

There are also a range of other functions including string functions like @substr, @upper, and @lower; and date and time functions like @now, @date, @day, @month, @year, etc.

The best place to get a full description of the available functions is from the man page, i.e. “man sc”.

To save the current spreadsheet type “P” followed by the desired filename. Use the “.sc” extension. You can load a spreadsheet at start by supplying the name as a parameter, for example:

sc test1.sc

A quick way to save and exit sc is to use ZZ, as you would with the vi editor.

One downside of sc is that its on-disk format isn’t in any way standard. If you commit yourself to using it, you will come across compatibility problems. I doubt your boss will accept your latest expense claims in .sc format! However there is one desktop spreadsheet that understands .sc files. GNU’s Gnumeric is able to open .sc files; however, it isn’t able to save them. But the practical upshot is that you can open .sc files in Gnumeric and then save them in a better known format like .xls or .ods. These files can then be opened in Microsoft Excel, LibreOffice and so on.

If you are used to running Linux without a desktop or if you are using a Raspberry Pi, then sc is a quick way to generate a spreadsheet without needing to boot into a full desktop. Why not give it a try, and if you have any questions please feel free to ask in the comments section below.