Imagine this scenario: you are given an Excel/LibreOffice spreadsheet (but increasingly now collaborating on a Google Sheet) where some columns are already filled, particularly a column containing row ID or primary key of some sort (e.g. client_id). Then, you have some other columns that you fill manually (e.g. audit_note). Finally, you also have yet some other columns that you need to fill from database (e.g. client_email, client_phone) so this sheet can be given to the sales person to follow up.

Currently my solution to fill the columns is the mysql-fill-csv-columns-from-query script. The flow is:

1. I select all cells on the Google Sheet, paste to terminal or a text file and save it to tmp1.tsv, then run something like:

% mysql-fill-csv-columns-from-query DBNAME --tsv tmp1.tsv 'SELECT c.email AS client_email, c.phone AS client_phone FROM clients c ... WHERE c.id=$client_id' > tmp2.csv

The script basically takes the SQL, replaces $xxx parts with value of corresponding CSV field, runs the query, fetches a single row, then adds/sets CSV fields from the SQL SELECT fields. It does this for every CSV row.

2. I then open tmp2.csv using LibreOffice then copy-paste the columns to the Google Sheet.

It is very much still bearable, but I am wondering whether there is an easier flow, preferably where Perl is still involved somehow.


In reply to Filling spreadsheet columns with data from database by perlancar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.