AI-generated Key Takeaways
-
This solution uses a custom function to calculate tiered pricing discounts in Google Sheets, which is simpler and more flexible than the built-in
SUMPRODUCT
function for this purpose. -
The script works by iterating through defined price tiers and applying the associated discount percentage to the portion of the total price that falls within each tier.
-
This beginner-level project uses the Google Apps Script Spreadsheet service and requires a Google Account to set up and run.
-
The provided sample demonstrates calculating discounts for a SaaS product and includes instructions on how to use the
tierPrice()
custom function in a Google Sheet. -
You can optionally add a checkbox to manually refresh the results of the custom function due to caching optimizations in Google Sheets.
Coding level: Beginner
Duration: 10 minutes
Project type: Custom function
Objectives
- Understand what the solution does.
- Understand what the Apps Script services do within the solution.
- Set up the script.
- Run the script.
About this solution
If you offer a tiered pricing system for your customers, this custom function makes it easier to calculate the discount amounts for your prices.
Though you could use the built-in function SUMPRODUCT
to make a tiered pricing
calculation, using SUMPRODUCT
is more complex and less flexible than this
solution's custom function.
How it works
A tiered pricing model means that the cost of goods or services goes down based on the quantity purchased.
For example, imagine you have two tiers, one that ranges from $0-$500 and is discounted by 10% and one that ranges from $501-$1,000 and is discounted by 20%. If the total price you need to calculate a discount for is $700, the script multiplies the first $500 by 10% and the remaining $200 by 20%, for a total discount of $90.
For a given total price, the script loops through the specified tiers in the tier pricing table. For each portion of the total price that falls within a tier, that portion is multiplied by the tier's associated percent value. The result is the sum of each tier's calculation.
Apps Script services
This solution uses the following service:
- Spreadsheet service–Takes the given value and calculates what portion of the value to multiply by each tier's percent discount.
Prerequisites
To use this sample, you need the following prerequisites:
- A Google Account (Google Workspace accounts might require administrator approval).
- A web browser with access to the internet.
Set up the script
Click the button below to make a copy of the Tier pricing custom function
spreadsheet. The Apps Script project for
this solution is attached to the spreadsheet.
Make a copy
Run the script
- In your copied spreadsheet, the table at row 16 shows a sample price calculation for a Software as a Service (SaaS) product.
- To calculate the discount amount, in cell
C20
, enter=tierPrice(C19,$B$3:$D$6)
. The final price updates in cellC21
. If you're in a location that uses decimal commas, you might need to enter=tierPrice(C19;$B$3:$D$6)
instead.
Review the code
To review the Apps Script code for this solution, click View source code below:
View source code
Code.gs
Modifications
You can edit the custom function as much as you'd like to fit your needs. Below is an optional addition to manually refresh custom function results.
Refresh cached results
Unlike built-in functions, Google caches custom functions to optimize performance. Therefore, if you change something within your custom function, such as a value that's being calculated, it might not immediately force an update. To refresh the function result manually, take the following steps:
- Add a checkbox to an empty cell by clicking Insert > Checkbox.
- Add the cell that has the checkbox as an extra parameter of the custom
function. For example, if you add a checkbox to cell
D20
, update thetierPrice()
function in cellC20
to=tierPrice(C19,$B$3:$D$6,D20)
. - Check or uncheck the checkbox to refresh the custom function results.
Contributors
This sample is maintained by Google with the help of Google Developer Experts.