Program Functions

How The Slice Function Works In Notion

Screen Shot 2020-04-28 at 9.25.15 PM.png

๐Ÿ—ก Slice, Slice, Slice

This tutorial will provide both a basic and complex understanding of the slice function and unique ways to use it inside Notion. In short, what slice does is take a string of text and remove any particular element from it. This can be especially useful for sorting pages by a particular prefix or absence of it.

How Slice Works

Extracts a substring from a string from the start index (inclusively) to the end index (optional and exclusively).

Syntax:

  • slice(text, number)

  • slice(text, number, number)

Scenario 1: Movie Sorting Example and Breakdown

Screen_Shot_2020-04-28_at_9.03.05_PM.png

I recently received the following question from a reader and found it intriguing: All pages with "The" at the start are still ordered by "T." How do I sort a group of movies in alphabetical order without "The" coming into the equation?

Solution:

Step 1: Test page for "The" and proceed to slice.

test(prop("Name"), "The") ? format(slice(prop("Name"), 4)) : format(prop("Name"))

Step 2: Sort formula property in alphabetical order

Step 3: Hide formula property but keep sort settings triggered to call that property.

Scenario 2: Example and Breakdown

Screen_Shot_2020-04-28_at_8.15.52_PM.jpg

In the example above, every page contains an entry number and a variable (ie. Light, Mild, Severe). In the next properties, I've sliced both elements into different columns.

Entry Number:

One way we can approach extracting the entry and number is to simply count how many characters make up this value. In the case above, this would be 7. The formula would look like this:

slice(prop("Name"), 0, 7)

  • This formula removes all characters excluding the first 7.

Variable:

For the variable, we've run into an issue. Unlike the entry number, all of the variables do not contain the same number of characters. Assuming one value (ie. Entry #) has a consistent character count, we can work backward. The formula would look like this:

slice(prop("Name"), 9)

  • This formula includes the removal of all characters up to (this includes the โ€œ:โ€ and โ€œspaceโ€ which are counted as characters as well).

SCENARIO 2.5: A CASE OF ONLY 1 INCONSISTENT SYMBOL

Screen_Shot_2020-04-28_at_8.20.43_PM.jpg

In this next example, I'm going to show you how to extract two values from the text that have no definite pattern, however, there is one constant symbol to work with. In the above image, I plug in "Entry 34" which is 8 characters instead of 7 and "Kind of Severe" which returns an odd space at the start. Let's fix this.

Entry Number:

For this, I would locate the constant colon (:). The formula would look like this:

My scenario โ†’ The entry numbers will not exceed 200.

Entry Number:

if(contains(slice(prop("Name"), 0, 8), ":"), slice(prop("Name"), 0, 7), if(contains(slice(prop("Name"), 0, 9), ":"), slice(prop("Name"), 0, 8), if(contains(slice(prop("Name"), 0, 10), ":"), slice(prop("Name"), 0, 9), "")))

Breakdown:

  • if(contains(slice(prop("Name"), 0, 8), ":") โ†’ if the page contains a colon after this particular slice function is executed ...

  • slice(prop("Name"), 0, 7) โ†’ execute this slice function. (from original example)

  • if(contains(slice(prop("Name"), 0, 9), ":"), slice(prop("Name"), 0, 8) โ†’ if the page contains a colon after this particular slice function is executed ...

  • slice(prop("Name"), 0, 8) โ†’ execute this slice function instead.

and so on ...

Variable:

And now, we work backwards again:

if(contains(slice(prop("Name"), 0, 8), ":"), slice(prop("Name"), 9), if(contains(slice(prop("Name"), 0, 9), ":"), slice(prop("Name"), 10), if(contains(slice(prop("Name"), 0, 10), ":"), slice(prop("Name"), 11), "")))

Breakdown

  • if(contains(slice(prop("Name"), 0, 8), ":") โ†’ if the page contains a colon after this particular slice function is executed ...

  • slice(prop("Name"), 9) โ†’ execute this slice function. (from original example)

  • if(contains(slice(prop("Name"), 0, 9), ":") โ†’ if the page contains a colon after this particular slice function is executed ...

  • slice(prop("Name"), 10) โ†’ execute this slice function instead.

and so on ...

Result โ†’

Screen_Shot_2020-04-28_at_8.57.50_PM.jpg