Skip to content

Custom Function

You'll write the first script behind a sheet: a function the grid can call that you wrote yourself, and a button that does a job no formula can.

About

Every spreadsheet has a programming language behind it. Google Sheets has Apps Script, Excel has Office Scripts and VBA, and LibreOffice has Basic and Python. You don't need to be a programmer, and this isn't the Coding track; the script is twenty to fifty lines, and the point is that the sheet now does something it couldn't. A custom function is the gentle way in, because it's just a formula you wrote. A menu action that moves rows or stamps a date is the useful way in. A script runs with your account's permissions, so never paste in code you can't read line by line, and don't grant a script access to your email or files unless you wrote the line that needs it. What counts is code you wrote, in a sheet you use, with the code visible. Scripts that run on a timer and reach outside the sheet belong to Automation & Scripts.

At a glance

Per requirement
35 XP
Completion bonus
300 XP
Requirements
6
Takes
Three evenings, then two weeks
Skill level
Intermediate (months of practice)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own
Locked for now
Earn two more Uncommon badges in Spreadsheets to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Write a custom function that takes a cell and returns something no built-in does. Open the script editor (Extensions in Sheets, Automate in Excel, Tools then Macros in LibreOffice). Good first functions: tidy a phone number into one format, turn '2 lb 3 oz' into grams, return a letter grade from your own bands, or compute the week number your club uses. Call it from a cell. Custom functions can only return a value, not change other cells, which is the limit you'll hit next. Take a screenshot of the code and the cell using it. Photo
  2. 2
    Fill it down a hundred rows and compare it against a built-in formula that does the same job. Custom functions are slow, because the sheet calls each one separately, and a hundred of them can sit on Loading for seconds where SUBSTITUTE takes none. If a built-in can do the job, use the built-in, and keep the custom function for what it can't. Count the seconds from paste to the last cell filling in, for both columns. Take a screenshot of the two columns and the two timings. Photo
  3. 3
    Write a script that runs from a menu item or a button and changes the sheet. Archive every row ticked done to another tab, stamp today's date in the next empty cell, or clear and reset an input block. In Sheets, an onOpen function adds a custom menu; in Excel, assign an Office Script or macro to a button; in LibreOffice, assign the macro to a form button. Film pressing it and the sheet changing. Video
  4. 4
    Make it safe to press twice. The first bug every script has is that a second press archives nothing, or everything again. Add a check (is this row already stamped, is there anything to move) and either a confirmation dialog or a tab the script copies rows to before it deletes them. Film pressing it twice in a row with nothing bad happening. Video
  5. 5
    Put a plain-words comment above every block of the script. The reader is you in a year, or whoever inherits the sheet, and they don't code. Each comment says what the block does and why, not how. Take a screenshot of the commented script. Photo
  6. 6
    Use it for two weeks and keep a log of every run and every misbehavior. A small log tab: date, what you pressed, what happened. 'Worked' is an entry. Expect at least one surprise, like a row with a blank date or someone pressing the button on the wrong tab. Take a screenshot of the log with at least ten entries. Photo