From 00da2cfce96cbdef9ea230764a025a3b675eeb57 Mon Sep 17 00:00:00 2001 From: Kuba Sekowski Date: Mon, 11 Apr 2022 14:27:26 +0200 Subject: [PATCH 1/5] Add unit tests for function LEFT with apostrophe operator --- test/interpreter/function-left.spec.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/interpreter/function-left.spec.ts b/test/interpreter/function-left.spec.ts index 7a52bc8ce1..4968fb5002 100644 --- a/test/interpreter/function-left.spec.ts +++ b/test/interpreter/function-left.spec.ts @@ -1,4 +1,4 @@ -import {ErrorType, HyperFormula} from '../../src' +import {CellType, CellValueType, ErrorType, HyperFormula} from '../../src' import {ErrorMessage} from '../../src/error-message' import {adr, detailedError} from '../testUtils' @@ -65,13 +65,17 @@ describe('Function LEFT', () => { it('should coerce other types to string', () => { const engine = HyperFormula.buildFromArray([ - ['=LEFT(1, 1)'], + ['=LEFT(10, 1)'], ['=LEFT(5+5, 1)'], ['=LEFT(TRUE(), 1)'], + ['=LEFT("010", 1)'], + ['=LEFT(B5, 1)', "'010"], ]) expect(engine.getCellValue(adr('A1'))).toEqual('1') expect(engine.getCellValue(adr('A2'))).toEqual('1') expect(engine.getCellValue(adr('A3'))).toEqual('T') + expect(engine.getCellValue(adr('A4'))).toEqual('0') + expect(engine.getCellValue(adr('A5'))).toEqual('0') }) }) From 510302d3f7339db30bb2c6a56dffc3b21e920d10 Mon Sep 17 00:00:00 2001 From: Kuba Sekowski Date: Thu, 9 Nov 2023 14:35:12 +0100 Subject: [PATCH 2/5] Add a commet to the randomized unit test about the chance of failure --- test/interpreter/function-randbetween.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/interpreter/function-randbetween.spec.ts b/test/interpreter/function-randbetween.spec.ts index 3595a38c1e..581b7f2078 100644 --- a/test/interpreter/function-randbetween.spec.ts +++ b/test/interpreter/function-randbetween.spec.ts @@ -17,7 +17,7 @@ describe('Interpreter - function RANDBETWEEN', () => { arr[val] += 1 } for (const val of arr) { - expect(val).toBeGreaterThan(0) + expect(val).toBeGreaterThan(0) // 10*9^100/10^100 ~= 0.0003 chance of failure if truly random } }) From d9b754e89f8854245fcd768c431afff77f7c3167 Mon Sep 17 00:00:00 2001 From: Kuba Sekowski Date: Thu, 9 Nov 2023 17:46:44 +0100 Subject: [PATCH 3/5] Add section 'Forcing the string value type' to the 'Types of values' guide --- docs/guide/advanced-usage.md | 12 ++++++------ docs/guide/types-of-values.md | 24 +++++++++++++++++++++++- test/interpreter/function-left.spec.ts | 2 +- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/docs/guide/advanced-usage.md b/docs/guide/advanced-usage.md index 249dd2f7bf..0786437e8e 100644 --- a/docs/guide/advanced-usage.md +++ b/docs/guide/advanced-usage.md @@ -122,9 +122,9 @@ console.log(winningTeam) ## Demo + src="https://codesandbox.io/embed/github/handsontable/hyperformula-demos/tree/2.6.x/advanced-usage?autoresize=1&fontsize=11&hidenavigation=1&theme=light&view=preview" + style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" + title="handsontable/hyperformula-demos: advanced-usage" + allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking" + sandbox="allow-autoplay allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"> + diff --git a/docs/guide/types-of-values.md b/docs/guide/types-of-values.md index 6565459f6c..9109e36b73 100644 --- a/docs/guide/types-of-values.md +++ b/docs/guide/types-of-values.md @@ -6,7 +6,7 @@ which it's referring. Functions may work differently based on the types of values. | Type of value | Description | -| :------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +|:---------------------------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | Number | A numeric value such as 0, 2, -40, 0.1, and also scientific notation e.g. 5.6E+01; with a period as a default decimal separator. | | Text (string) | A text value, like "ABC", "apollo". | | Logical (Distinct Boolean) | A logical value might be one of two values: TRUE or FALSE. Please note that even if there is type coercion this will be recognized as TRUE/FALSE when comparing to numbers. It will not be recognized as 1 or 0. | @@ -30,6 +30,28 @@ operations such as calculating the number of days between two dates. - A DateTime value is represented as the number of (possibly fractional) days since [`nullDate`](../api/interfaces/configparams.md#nulldate). +## Forcing the string value type + +Similarly to other popular spreadsheet software, HyperFormula automatically detects the type of an input value. +On some occasions, the value should be treated as a string even though it's parsable as a number/date/formula/etc. +The typical examples are numeric values with no number semantics, such as zip codes, bank sort codes, social security numbers, etc. +To prevent the automatic type conversion, you can prepend the string value with an apostrophe character (`'`). + +```js +const hf = HyperFormula.buildFromArray([ + ["11201"], // a number: 11201 + ["'11201"], // a string: "11201" + ["22/06/2022"], // a date: June 22nd 2022 + ["'22/06/2022"], // a string: "22/06/2022" +]); + +// a formula: SUM(B1,B2) +hf.setCellContents({ col: 0, row: 4, sheet: 0 }, [["=SUM(B1,B2)"]]); + +// a string: "=SUM(B1,B2)" +hf.setCellContents({ col: 0, row: 5, sheet: 0 }, [["'=SUM(B1,B2)"]]); +``` + ## Getting cell type Cells have types that can be retrieved by using the `getCellType` method. Cell diff --git a/test/interpreter/function-left.spec.ts b/test/interpreter/function-left.spec.ts index 4968fb5002..83165164ff 100644 --- a/test/interpreter/function-left.spec.ts +++ b/test/interpreter/function-left.spec.ts @@ -1,4 +1,4 @@ -import {CellType, CellValueType, ErrorType, HyperFormula} from '../../src' +import {ErrorType, HyperFormula} from '../../src' import {ErrorMessage} from '../../src/error-message' import {adr, detailedError} from '../testUtils' From c63ab1ff40fe1879947ff6d523b94a0586b0e1ef Mon Sep 17 00:00:00 2001 From: Kuba Sekowski Date: Tue, 19 Dec 2023 11:45:54 +0100 Subject: [PATCH 4/5] Be more specific about the cell content data types in the types-of-values guide --- docs/guide/types-of-values.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guide/types-of-values.md b/docs/guide/types-of-values.md index 9109e36b73..5ec7aa72f0 100644 --- a/docs/guide/types-of-values.md +++ b/docs/guide/types-of-values.md @@ -33,7 +33,7 @@ operations such as calculating the number of days between two dates. ## Forcing the string value type Similarly to other popular spreadsheet software, HyperFormula automatically detects the type of an input value. -On some occasions, the value should be treated as a string even though it's parsable as a number/date/formula/etc. +On some occasions, the value should be treated as a string even though it's parsable as a formula, number, date, time, datetime, boolean, currency or percentage. The typical examples are numeric values with no number semantics, such as zip codes, bank sort codes, social security numbers, etc. To prevent the automatic type conversion, you can prepend the string value with an apostrophe character (`'`). From a694abecfcbbefe6282df3b70d477bae593fa465 Mon Sep 17 00:00:00 2001 From: Kuba Sekowski Date: Tue, 19 Dec 2023 11:48:21 +0100 Subject: [PATCH 5/5] Fix grammatical error Co-authored-by: Evan Seaward --- docs/guide/types-of-values.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guide/types-of-values.md b/docs/guide/types-of-values.md index 5ec7aa72f0..e5e70bc760 100644 --- a/docs/guide/types-of-values.md +++ b/docs/guide/types-of-values.md @@ -34,7 +34,7 @@ operations such as calculating the number of days between two dates. Similarly to other popular spreadsheet software, HyperFormula automatically detects the type of an input value. On some occasions, the value should be treated as a string even though it's parsable as a formula, number, date, time, datetime, boolean, currency or percentage. -The typical examples are numeric values with no number semantics, such as zip codes, bank sort codes, social security numbers, etc. +Typical examples are numeric values with no number semantics, such as zip codes, bank sort codes, social security numbers, etc. To prevent the automatic type conversion, you can prepend the string value with an apostrophe character (`'`). ```js