🎓 [Getting Started with Functions p. 2] Math Functions

Hey Makers :wave:

Today, I am jumping in with the second part of our Getting Started with Functions series.
Let’s dive right in and learn something about Math functions on Make

:student: In the previous part of the series, we introduced the general concept of using functions in your scenario as a flexible way to manipulate and format data. In particular, we used a couple of general functions to summarise form responses of prospects who were requesting to work with a web development agency.

Check out p. 1 to get context for today’s article:
🎓 [Getting Started with Functions p. 1] General Functions

:dart: The Goal

The goal of today’s article is to

  • use the summarised form responses to dynamically estimate project prices
  • attach the estimated prices to a quote document
  • send the quote to the prospect by email

Wonder how we’ll go about calculating the prices? You got it! With the help of Math functions.


:test_tube: Example Scenario

For today’s article, we created a scenario with a Google Sheets > Watch Rows module which will retrieve all the new rows that have been created by the scenario in part 1 of this series.





For each row in the sheet, we can generate a quote using the Google Docs > Create a Document from a Template module.


:one: A prerequisite for using this module is having a Google Doc template with {{placeholder tags}}.

You can have as many of these tags as you like and each one of them can be dynamically populated with data from the scenario.

The template used in this example has tags for

  • quote_number
  • customer_name
  • service_name
  • service_description
  • total

These tags are automatically detected by the module and fields are created for them.


:two: Now that we have those fields ready, let’s map

  • the Row number of the Google Sheet to the quote_number field (since this number will automatically be incremented for each proposal).
  • the value of the Name column to the customer_name field.
  • the value of the Service column to the service_name field.



:three: For the service_description field, you can use the switch function to craft a description based on the value of the Service column.

For example, if the Service is equal to E-Commerce, the populated text will say

“We will develop a fully functional e-commerce store, integrate your payment gateway and upload all your products.”


:four: When it comes to the total field, this is where math functions will play an important role. The goal is to add together the numbers and calculate the total price of the project, based on the prospect’s requirements.

In the example of this web development agency, the price of the project will be different based on the service the prospect is interested in.

To populate the base price of the project you can once again use the switch function.


Since the agency also offers creation of various assets (e.g. logo, images, text etc.), an additional cost will have to be added to the total price.

The addition can be accomplished using a math operator.

:1234: Math Functions

:five: To add a math function to a field, simply click in any module field and select the ‘Math functions’ tab. In this particular example, we will simply choose the :heavy_plus_sign: operator to add the two numbers.


However, the cost of the asset creation service should only be added if the prospect has stated in the form that they do not already have all the required assets.

:six: Therefore, an if function needs to check if the value of the Assets column on the spreadsheet is equal to ‘No’, before adding the number to the total price of the project.


With all these fields set, the module is now ready to generate quotes.

Here’s an example of what a quote will look like once it is dynamically generated with the data from the Google Sheet and the conditions defined by the functions in the module fields.

Since Nick was interested in the development of an E-Commerce store ($400) and he did not already have a set of assets for the website ($200) the total price of the quote is $600.


:seven: A math function that is particularly useful in this use case is formatNumber.

As you can see in the screenshot above, the price on the quote is not formatted correctly, since a price is supposed to have two decimal points.

The formatNumber function takes a number as input and converts it to a number with a specified number of decimal points, a decimal separator as well as a thousands separator of your choice.

Here’s an example

The number 3224.14899 has been entered as the input of the formatNumber function.
The decimal points have been set to 2, the separator for the decimal point is set to be a dot, and the separator for the thousands is set to a comma.

When this module is executed the result number is 3,224.15 .
The function automatically rounded the decimal part of the number (.14899) to .15.


Similarly, the formatNumber function can be applied to the quote generator use case to generate a total price that has two decimal points.

This is what the function looks like once applied to the total field of the Google Docs > Create a Document from a Template module.

The input number of the formatNumber function is the number generated as a result of the switch function. The other parameters of the formatNumber function are the same as in the example above.


:tada: Yaay, we did it!

Here’s what the formatted number looks like on the sales quote


With these basics, you can start applying math functions to your scenarios and calculating numbers with dynamic inputs. You may have noticed that our quote is missing some dates, though.

Well, that’s why the next part of the series focuses on generating and manipulating date/time using the Date functions.

:tv: Stay tuned! :tv:

6 Likes

This is a fantastic breakdown Michaela!

2 Likes

Thanks so much, Paul :purple_heart: The power of Make functions is begging to be showcased :nerd_face:

2 Likes