gsSQL Custom Function

Custom functions used in Google Sheets for ease of use and improved functionality.

Purpose

About

Github Project

GitHub gsSQL

Usage

=gsSQL( SelectSqlStatement, [TableDefinitions], [ColumnOutputFlag], [BindVariableData])

  1. SelectSqlStatement. (Required)
    • Only the SELECT statement is supported.
    • Most all common SELECT syntax is supported.
    • The first row of the table MUST contain unique column titles (for field names).
      • To reference a field where the title contains spaces, just use the underscore in place of the space.
        • Column Title = “Transaction Date”
        • SELECT = "SELECT transaction_date from master_transactions"
    • Simple select example. There is a sheet called ‘authors’ and data starts in row 1, column 1.
      =gsSQL("select * from authors")
      
=gsSQL("SELECT 
            books.id, books.title, books.type, authors.last_name, translators.last_name 
        FROM books 
        LEFT JOIN authors ON books.author_id = authors.id 
        LEFT JOIN translators ON books.translator_id = translators.id 
        ORDER BY books.id", "books", Books!A1:F, "authors", Authors!A1:C, "translators", Translators!A1:C)
  1. TableDefinitions (Optional)
    • Defines each table referenced in SELECT statement.
    • If a table does not encompass an entire sheet or you need to specify a range for the data, a table definition is required.
    • If you require your select statement to refesh results when any source table changes, table definitions are required (new format ONLY).
    • There are TWO formats to define tables: the original format (for backwards compatibility) and the new (preferred) format.
    • NEW FORMAT. This format will update all SELECT results when any source table data changes.
      • This format REQUIRES each column in the table to contain a unique title in the first row.
      • The TABLE NAME, RANGE parameters are repeated for every table referenced in the SELECT.
        • a) Table name. Logical name of table as referenced in SELECT statement.
        • b) Google Sheets Range. Any valid named range or A1 notation.
=gsSQL("SELECT  
            books.id, books.title, authors.first_name, authors.last_name 
        FROM 
            books 
        INNER JOIN 
            authors ON books.author_id = authors.id 
        ORDER BY 
            books.id", 
        "books", Books!A1:F, "authors", Authors!A1:C, true)
  1. ColumnOutputFlag (Optional)
    • Include column title in output or not. (true adds column titles, false omits the title row).
  2. BindVariableData. (Optional)
    • There should be one data item listed PER question mark in the SELECT statement. Data for the variables can be literal data, cell references (A1 notation), and named fields.
    • Bind variables are for convenience only.
    • You can concatenate the various string components with the variable data to make a valid SELECT. It is just less messy using bind data.
    • In the following example, startDate and endDate are named ranges on the sheet.
      =gsSQL
      (
        "select  
            booksales.invoice as 'Invoice', booksales.quantity as 'Quantity', booksales.price as 'Price', booksales.quantity * booksales.price as 'Sales', booksales.date, books.title, customers.name, authors.first_name + ' ' + authors.last_name as 'Author', translators.first_name + ' ' + translators.last_name as 'Translator', editors.first_name + ' ' + editors.last_name as 'Editor' 
        from booksales left join books on booksales.book_id = books.id 
        left join customers on booksales.customer_id = customers.id 
        left join authors on books.author_id = authors.id 
        left join translators on books.translator_id = translators.id 
        left join editors on books.editor_id = editors.id 
        where booksales.date >= ?1 and booksales.date <= ?2 
        union all select 'Total', SUM(booksales.quantity), avg(booksales.price), SUM(booksales.quantity * booksales.price), '' ,'', '', '', '', '' from booksales 
        where booksales.date >= ?1 and booksales.date <= ?2",
        , true, startDate, endDate
      )