1. 程式人生 > >Processing spreadsheet data in Go · Applied Go

Processing spreadsheet data in Go · Applied Go

This article is also available as a video on the Applied Go YouTube channel:

It is the shortened version of a lecture in my upcoming minicourse “Workplace Automation With Go”.

Spreadsheet data is everywhere. You can find it in Excel sheets as well as when downloading business data from a website.

Package encoding/csv from the Go standard library can help you processing that data and produce statistics, reports or other kinds of output from it. Here is how.

Let’s assume we work at a stationery distributor. Every evening, we receive a spreadsheet containing the orders of the day for review. The data looks like this:

Date Order ID Order Item Unit Price Quantity
2017-11-17 1 Ball Pen 1.99 50
2017-11-17 2 Notebook 12.99 10
2017-11-17 3 Binder 4.99 25
2017-11-20 4 Pencil 0.99 100
2017-11-20 5 Sketch Block 2.99 40
2017-11-22 6 Ball Pen 1.99 30
2017-11-23 7 Sketch Block 2.99 20
2017-11-24 8 Ball Pen 1.99 60

We’re interested in some information that is not directly contained in the data; especially, we want to know

  • the total price for each order,
  • the total sales volume,
  • and the number of ball pens sold.

As we get a new copy every day, creating formulas within the spreadsheet is not an option. Instead, we decide to write a small tool to do the calculations for us. Also, the tool shall add the result to the table and write a new spreadsheet file.

But before starting to code, our first step is to export the spreadsheet data to CSV. To make things a bit more complicated, we export the data with a semicolon as the column separator.

(The exact steps vary, depending on the spreadsheet software used.)

The raw CSV data looks like this:

Date;Order ID;Order Item;Unit Price;Quantity
2017-11-17;1;Ball Pen;1.99;50
2017-11-17;2;Notebook;12.99;10
2017-11-17;3;Binder;4.99;25
2017-11-18;4;Pencil;0.99;100
2017-11-18;5;Sketch Block;2.99;40
2017-11-19;6;Ball Pen;1.99;30
2017-11-19;7;Sketch Block;2.99;20
2017-11-19;8;Ball Pen;1.99;60

We can see a header row and data rows, with data separated by semicolons.

Now let’s dive into the Go code to process the data from this spreadsheet and from all spreadsheets that are still to come.

Reading and processing CSV data with Go