Bird
Raised Fist0
Google Sheetsspreadsheet~8 mins

Custom functions in Google Sheets - Dashboard Guide

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Dashboard Mode - Custom functions
Goal

Calculate the area of different shapes using a custom function in Google Sheets to simplify repeated calculations.

Sample Data
ShapeDimension 1Dimension 2Area
Rectangle510
Triangle68
Circle7
Rectangle34
Triangle105
Dashboard Components
  • KPI Card: Total Area of all shapes combined.
    Formula: =SUM(D2:D6)
  • Custom Function: =CALCULATE_AREA(A2, B2, C2) used in cells D2 to D6.
    Function logic:
    - Rectangle: area = dimension1 * dimension2
    - Triangle: area = 0.5 * dimension1 * dimension2
    - Circle: area = PI() * (dimension1 ^ 2) (dimension1 is radius)
    Example formula in D2: =CALCULATE_AREA(A2, B2, C2)
  • Table: Shows shapes, dimensions, and calculated area using the custom function.
Dashboard Layout
+---------------------------+
|       Total Area KPI      |
|         (Cell F1)         |
+---------------------------+
| Shape | Dim1 | Dim2 | Area |
|---------------------------|
| Data Table with custom fn |
+---------------------------+
Interactivity

Users can change the dimensions or shape names in columns A, B, and C. The custom function recalculates the area automatically in column D. The Total Area KPI updates to reflect the sum of all areas.

Self Check

If you change the shape in A3 from "Circle" to "Rectangle" and set Dimension 2 to 9, what happens to the area in D3 and the total area KPI?

Key Result
Dashboard calculates areas of shapes using a custom function and sums total area.

Practice

(1/5)
1. What is a custom function in Google Sheets?
easy
A. A chart type you add to your sheet
B. A built-in formula like SUM or AVERAGE
C. A filter to sort data automatically
D. A formula you create using JavaScript in Google Apps Script

Solution

  1. Step 1: Understand what custom functions are

    Custom functions are formulas you write yourself using JavaScript in Google Apps Script.
  2. Step 2: Differentiate from built-in features

    Built-in formulas like SUM are pre-made, while custom functions are user-created.
  3. Final Answer:

    A formula you create using JavaScript in Google Apps Script -> Option D
  4. Quick Check:

    Custom function = user-made JavaScript formula [OK]
Hint: Custom functions are your own formulas written in JavaScript [OK]
Common Mistakes:
  • Confusing custom functions with built-in formulas
  • Thinking custom functions are chart types
  • Believing custom functions are filters
2. Which of the following is the correct way to start defining a custom function in Google Apps Script?
easy
A. function MYFUNCTION() { return 'Hello'; }
B. def MYFUNCTION() { return 'Hello'; }
C. func MYFUNCTION() { return 'Hello'; }
D. function: MYFUNCTION() { return 'Hello'; }

Solution

  1. Step 1: Recall JavaScript function syntax

    In Google Apps Script, which uses JavaScript, functions start with the keyword 'function' followed by the name and parentheses.
  2. Step 2: Check each option's syntax

    function MYFUNCTION() { return 'Hello'; } uses correct JavaScript syntax. Options B and C use Python or other language syntax. function: MYFUNCTION() { return 'Hello'; } has incorrect punctuation.
  3. Final Answer:

    function MYFUNCTION() { return 'Hello'; } -> Option A
  4. Quick Check:

    JavaScript function syntax = function MYFUNCTION() { return 'Hello'; } [OK]
Hint: JavaScript functions start with 'function' keyword [OK]
Common Mistakes:
  • Using Python or other language syntax
  • Adding colons after function name
  • Missing parentheses after function name
3. Given this custom function code in Google Apps Script:
function DOUBLE(input) {
  return input * 2;
}

What will be the result of =DOUBLE(5) in a sheet cell?
medium
A. 5
B. DOUBLE(5)
C. 10
D. Error

Solution

  1. Step 1: Understand the function logic

    The function takes an input and returns input multiplied by 2.
  2. Step 2: Calculate the output for input 5

    5 multiplied by 2 equals 10.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    5 * 2 = 10 [OK]
Hint: Multiply input by 2 to get output [OK]
Common Mistakes:
  • Expecting the function name as output
  • Thinking it returns input unchanged
  • Assuming syntax error without checking code
4. You wrote this custom function:
function ADDTEN(value) {
  return value + 10
}

But when you use =ADDTEN(5) in the sheet, it shows an error. What is the likely problem?
medium
A. Function name must be lowercase
B. Missing closing brace '}'
C. Function lacks parentheses
D. Missing semicolon after return statement

Solution

  1. Step 1: Check the function syntax carefully

    The function code is missing the closing brace '}' at the end.
  2. Step 2: Understand impact of missing brace

    Without the closing brace, the script is incomplete and causes an error when called.
  3. Final Answer:

    Missing closing brace '}' -> Option B
  4. Quick Check:

    All functions need matching braces [OK]
Hint: Always close function with a brace '}' [OK]
Common Mistakes:
  • Thinking semicolon is mandatory in Apps Script
  • Assuming function name case matters
  • Overlooking missing braces
5. You want a custom function that takes a range of numbers and returns the sum of only the positive numbers. Which of these custom functions will work correctly?
hard
A. function SUMPOS(range) { let total = 0; for (let i = 0; i < range.length; i++) { if (range[i][0] > 0) total += range[i][0]; } return total; }
B. function SUMPOS(range) { return range.reduce((a,b) => a + b); }
C. function SUMPOS(range) { let total = 0; for (let i in range) { if (range[i] < 0) total += range[i]; } return total; }
D. function SUMPOS(range) { return Math.sum(range); }

Solution

  1. Step 1: Analyze each function's logic

    function SUMPOS(range) { let total = 0; for (let i = 0; i < range.length; i++) { if (range[i][0] > 0) total += range[i][0]; } return total; } loops through the range and adds only positive numbers. function SUMPOS(range) { return range.reduce((a,b) => a + b); } sums all numbers without filtering. function SUMPOS(range) { let total = 0; for (let i in range) { if (range[i] < 0) total += range[i]; } return total; } adds negative numbers only. function SUMPOS(range) { return Math.sum(range); } uses a non-existent Math.sum function.
  2. Step 2: Identify correct filtering and summing

    Only function SUMPOS(range) { let total = 0; for (let i = 0; i < range.length; i++) { if (range[i][0] > 0) total += range[i][0]; } return total; } correctly filters positive numbers and sums them.
  3. Final Answer:

    The function that sums only positive numbers -> Option A
  4. Quick Check:

    Filter positive numbers, then sum [OK]
Hint: Loop and add only if number > 0 [OK]
Common Mistakes:
  • Summing all numbers without filtering
  • Adding negative numbers by mistake
  • Using non-existent functions like Math.sum