
When working with Google Sheets or Excel, you may find situations where built-in functions won’t meet your needs. However, you can use both platforms to create custom functions solely based on your specific needs. You can create similar custom functions on both platforms, but their approaches and languages are different. This post will show you how to create custom functions in Google Sheets with Apps Script while comparing Excel’s User-Defined Functions (UDFs).
Language and Environment
Google Sheets with Apps Script
Google Sheets uses Apps Script to create custom functions. App Script creates custom functions using standard JavaScript. Apps Script is integrated within Google Sheets. It deploys scripts directly in the browser without needing external tools. However, you must need to have a Google Account (Google Workspace accounts might require administrator approval) and a web browser with access to the internet.
Excel’s User-Defined Functions
Excel uses VBA (Visual Basic for Applications) to create user-defined functions. Excel has a built-in VBA editor with various built-in procedures developed locally. You will need the Excel desktop application to create a user-defined function
Using Google Sheets Apps Script and Excel’s VBA for Creating Custom (User-Defined) Functions
Let’s consider a scenario where you want to calculate a discount based on the purchase amount. If a customer spends more than $150, they will get a 7% discount, otherwise no discount. To perform this task you can create this custom function using app scripts JavaScript and Excel’s VBA.
Creating a Custom Function in Google Sheets
To create a custom function in Google Sheets for calculating discounts based on your scenario, follow the steps given below:
- Open your Google Sheet >> go to Extensions >> select Apps Script.
- In the Apps Script editor, write the function using JavaScript syntax.
function calculateDiscount(purchaseAmount) {
if (purchaseAmount >= 150) {
return purchaseAmount * 0.07; // 7% discount on the purchase.
} else {
return 0; // No discount
}
}
- Save the script, go back to your Google Sheet, and write the custom function name just like the usual function =calculateDiscount(A2)
Output:

Note: Apps Script will prompt you to review and approve permissions when first running the script, as it requires access to your spreadsheet. Grant the necessary permissions to use the script in Google Sheets with your data.
User-Defined Functions in Excel
To create a similar user-defined function in Excel, you can use the VBA, by following the given steps:
To open the VBA editor in Excel,
- Go to Developer tab >> select Visual Basic or press Alt + F11)
- From Insert >> select Module.
- In the Module write your user-defined function.
Function CalculateDiscount(purchaseAmount As Double) As Double If purchaseAmount> 150 Then CalculateDiscount = purchaseAmount * 0.07 ' 7% discount on purchase. Else CalculateDiscount = 0 ' No discount End If End Function
- Save the VBA code go back to your Excel sheet and write the user-defined function name =CalculateDiscount(A2)
Output:

Both Google Sheets and Excel have different capabilities, accessibility, and limitations. Apps Script works online, and the function requires permission to access your Google Sheets data. If granted it integrates well with other Google services like Google Drive, Gmail, and other APIs. Custom functions are instantly available to any collaborator with access to the sheet.
Excel’s UDFs can work offline and don’t require additional permissions, making them ideal for businesses with desktop-based workflows. It allows integration with the workbook to manipulate the worksheet, interacts with other MS Office applications like Word or Access, and automates a wide range of tasks. User-defined functions are only available to users who have access to the workbook.
Conclusion
Google Sheets with Apps Script offers a simple and cloud-friendly way to create custom functions using JavaScript. It is easy to share or collaborate with team-oriented work. Excel with VBA UDFs is useful and ideal for businesses without internet access or that rely on desktop software. Its localized automation is ideal for users who require advanced Excel operations and integrations with desktop applications.
