Updates to Grist Functions (#125)

Clarify documentation for QUOTIENT, SUMPRODUCT, UUID, REPLACE, lookupOne, lookupRecords, and $group.
This commit is contained in:
Natalie Misasi
2022-02-12 23:38:24 -06:00
committed by GitHub
parent ec7bc9bef3
commit 2611d05c39
4 changed files with 39 additions and 19 deletions

View File

@@ -550,7 +550,7 @@ def PRODUCT(factor1, *more_factors):
def QUOTIENT(dividend, divisor):
"""
Returns one number divided by another.
Returns one number divided by another, without the remainder.
>>> QUOTIENT(5, 2)
2
@@ -797,7 +797,8 @@ def SUMIFS(sum_range, criteria_range1, criterion1, *args):
def SUMPRODUCT(array1, *more_arrays):
"""
Multiplies corresponding components in the given arrays, and returns the sum of those products.
Multiplies corresponding components in two equally-sized arrays,
and returns the sum of those products.
>>> SUMPRODUCT([3,8,1,4,6,9], [2,6,5,7,7,3])
156
@@ -859,7 +860,13 @@ def TRUNC(value, places=0):
return ROUNDDOWN(value, places)
def UUID():
"""Generate a random UUID-formatted string identifier."""
"""
Generate a random UUID-formatted string identifier.
Since UUID() produces a different value each time it's called, it is best to use it in
[trigger formula](https://support.getgrist.com/formulas/#trigger-formulas) for new records.
This would only calculate UUID() once and freeze the calculated value. By contrast, a regular [formula]
may get recalculated any time the document is reloaded, producing a different value for UUID() each time.
"""
if six.PY2:
return str(uuid.UUID(bytes=[chr(random.randrange(0, 256)) for _ in xrange(0, 16)], version=4))
else:

View File

@@ -220,7 +220,9 @@ def LEFT(string, num_chars=1):
def LEN(text):
"""
Returns the number of characters in a text string. Same as `len(text)`.
Returns the number of characters in a text string, or the number of items in a list. Same as
[`len`](https://docs.python.org/3/library/functions.html#len) in python.
See [Record Set](https://support.getgrist.com/functions/#recordset) for an example of using `len` on a list of records.
>>> LEN("Phoenix, AZ")
11
@@ -339,9 +341,9 @@ def REGEXREPLACE(text, regular_expression, replacement):
return re.sub(regular_expression, replacement, text)
def REPLACE(old_text, start_num, num_chars, new_text):
def REPLACE(text, position, length, new_text):
"""
Replaces part of a text string with a different text string. Start_num is counted from 1.
Replaces part of a text string with a different text string. Position is counted from 1.
>>> REPLACE("abcdefghijk", 6, 5, "*")
'abcde*k'
@@ -354,11 +356,11 @@ def REPLACE(old_text, start_num, num_chars, new_text):
>>> REPLACE('foo', 0, 1, 'bar')
Traceback (most recent call last):
...
ValueError: start_num invalid
ValueError: position invalid
"""
if start_num < 1:
raise ValueError("start_num invalid")
return old_text[:start_num - 1] + new_text + old_text[start_num - 1 + num_chars:]
if position < 1:
raise ValueError("position invalid")
return text[:position - 1] + new_text + text[position - 1 + length:]
def REPT(text, number_times):