Skip to content
This repository was archived by the owner on May 11, 2026. It is now read-only.

tomnitschke/gristruc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This repo is now deprecated.

See the Grist Creators forums or this Grist document for the up-to-date version.

Grist RUC - Reusable User Code

TLDR: These are ready-made formulas for getting advanced stuff done inGrist. Feel free to copy-paste them into your Grist document.

When working with Grist on an advanced level, there inevitably comes a moment when you find you're repeating yourself. Formulas doing the same kind of thing, or roughly the same thing, start appearing everywhere and you're wondering, couldn't I re-use that one solution I already have working in one spot somewhere else, too?

Here's a typical example to illustrate. Let's say your users like to enter people's names in one go like "Smith, John". Being the kind of data modelling person the average Grister probably is, you'd like to split that up into a proper "last name" and "first name" column. It's not hard to do, but you'll have to copy-paste a formula for doing it every time an input situation like that occurs in your tables. So, why not write one function "parse_usernames_properly_once_and_for_all()" and put that in a central code table, which we can then just reference from the actual formulas as needed?

So, at some point larger Grist projects would really benefit from a central code library. Unfortunately, Grist doesn't yet provide an elegant way of creating one, but with a bit of hackery, it can be done. Essentially, we'll be setting up a separate table that's just full of useful, universally reusable Python code. Once in place, your formulas can utilise it as a central library and stop re-inventing the wheel every single time.

Great! So how do I use this?

In your Grist document, create a new table. Give it a name like "Lib" or "Code" or something that both makes sense and is fast to type! It might be a good idea to name it in ALL-CAPS to make it stick out against regular tables in your project. Unsurprisingly, I named mine "RUC".

In this table, create just one record and make sure all columns are formula columns. This is where you paste the code from this repo. Go to the 'code' folder and pick whatever you like, pasting the contents into an appropriately named formula column.

For example, you might create a formula column called 'Relation', then paste the contents of code/relation.py into it. You may then use the methods contained in 'relation.py' by referring to your column. For example, to invoke the 'get_referring_columns' method - and supposing your table is named 'RUC', like mine - you can do this: RUC.lookupOne().Relation.get_referring_columns(...arguments go here...)

Why does this work?

The trick is to have a formula column spit out something that can be called rather than an actual value. This allows the column to be referenced by Grist's usual means, and called as if it were a normal Python function. To illustrate, here's an example formula column, let's call it "QuestionBot", as part of a table named "CODE":

def solve_meaning_of_life(question):
   return f"You asked '{question}' - to which the answer is, as always it must be, 42."

return solve_meaning_of_life

A "QuestionBot" column with the above formula in it will get a Python function as its value. This can be called from other places within your Project, like this:

# Nota bene: Using lookupOne() without any arguments will just fetch the first record.
return CODE.lookupOne().QuestionBot.solve_meaning_of_life("Pray, what does it all mean?")

This will put "You asked..." and so forth into the column with the above formula.

You can also use classes to organize things further:

class Solver:
   def solve_meaning_of_life(question):
      ... (see above) ...

return Solver

This may be called exactly the same way as before, but allows for having multiple functions in one column.

Technically, that's all the RUC does, really. It's basically a collection of the most useful results from years of in-depth Grist hackery. Enjoy!

Please share!

I cordially invite every seasoned or aspiring Grister to share! Open a pull request and let's add all your little snippets, advanced calculations and all-time greats!

About

RUC is reusable user code for Grist - a collection of functions and other reusable stuff for use in your Grist formulas.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages