Google Sheets Custom Formulas - Comprehensive Guide
Google Sheets Custom Formulas - Comprehensive Guide
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
1
Example 1: Generate a Full Name 3
Example 2: Calculate Percentage 3
Example 3: Fetch Current Exchange Rate 4
Example 4: Calculate Workdays Between Two Dates 4
Example 5: Highlight Duplicate Values in a Column 5
Exercises 5
Exercise 1: Custom Formula to Calculate BMI 5
Exercise 2: Calculate Compound Interest 6
Exercise 3: Fetch Weather Data 6
Troubleshooting Custom Formulas 7
Multiple-Choice Questions 7
Question 1: 7
Question 2: 7
Question 3: 7
Best Practices for Custom Formulas 8
Custom formulas in Google Sheets are created using Google Apps Script, which allows you to
build powerful, reusable functions for advanced spreadsheet operations. This guide explains
how to create, use, and troubleshoot custom formulas in Google Sheets, with detailed
examples, exercises, and quiz questions.
Custom formulas are user-defined functions that extend Google Sheets' built-in functions. They
are written in Google Apps Script and can be used directly in your spreadsheet like built-in
formulas such as SUM or AVERAGE.
Code:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
2
function ADD_TWO_NUMBERS(a, b) {
return a + b;
}
Usage:
● In your sheet, enter =ADD_TWO_NUMBERS(10, 20) and the result will be 30.
Explanation:
Code:
Usage:
Explanation:
Code:
Usage:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
3
● Enter =PERCENTAGE(50, 200) in a cell.
● Output: 25
Explanation:
● Divides the part by the total and multiplies by 100 to return the percentage.
Code:
Usage:
Explanation:
Code:
4
return count;
}
Usage:
Code:
function HIGHLIGHT_DUPLICATES(range) {
const values = range.flat();
const duplicates = values.filter((value, index, self) => value &&
self.indexOf(value) !== index);
return values.map(value => (duplicates.includes(value) ? "Duplicate"
: "Unique"));
}
Usage:
Exercises
● Write a custom function BMI that calculates BMI using the formula: BMI = weight
(kg) / height^2 (m^2).
Solution:
Test:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
5
● Output: 22.86
Solution:
Test:
● Write a custom formula WEATHER to fetch the current weather for a city using an API like
OpenWeatherMap.
Solution:
function WEATHER(city) {
const apiKey = "YOUR_API_KEY";
const url =
`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${api
Key}&units=metric`;
const response = UrlFetchApp.fetch(url);
const data = JSON.parse(response.getContentText());
return `Temperature: ${data.main.temp}°C, Weather:
${data.weather[0].description}`;
}
Test:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
6
Troubleshooting Custom Formulas
1. Authorization Issues:
○ Ensure the script is authorized to access external resources.
2. Error Messages:
○ Check for invalid inputs or unhandled edge cases.
3. Performance:
○ Optimize code for large datasets by minimizing loops.
Multiple-Choice Questions
Question 1:
Question 2:
1. Sends an email.
2. Fetches data from an external API or URL.
3. Writes data to a Google Sheet.
4. Reads a file from Google Drive.
Question 3:
1. Math.sqrt()
2. Math.pow(base, exponent)
3. Math.square()
4. Math.abs()
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
7
Best Practices for Custom Formulas
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis