@trebco/treb 29.5.4 → 29.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/treb.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- /*! API v29.5. Copyright 2018-2024 trebco, llc. All rights reserved. LGPL: https://treb.app/license */
1
+ /*! API v29.7. Copyright 2018-2024 trebco, llc. All rights reserved. LGPL: https://treb.app/license */
2
2
 
3
3
  /**
4
4
  * add our tag to the map
@@ -388,10 +388,8 @@ export declare class EmbeddedSpreadsheet<USER_DATA_TYPE = unknown> {
388
388
  * Use this function to batch multiple document changes. Essentially the
389
389
  * grid stops broadcasting events for the duration of the function call,
390
390
  * and collects them instead. After the function call we update as necessary.
391
- *
392
- * @public
393
391
  */
394
- Batch(func: () => void, paint?: boolean): Promise<void>;
392
+ Batch(func: () => void, paint?: boolean): void;
395
393
 
396
394
  /** set freeze area */
397
395
  Freeze(rows?: number, columns?: number): void;
@@ -783,7 +781,7 @@ export declare class EmbeddedSpreadsheet<USER_DATA_TYPE = unknown> {
783
781
  *
784
782
  * @public
785
783
  */
786
- Recalculate(): Promise<void>;
784
+ Recalculate(): void;
787
785
 
788
786
  /**
789
787
  * Save document to local storage.
@@ -964,6 +962,37 @@ export declare class EmbeddedSpreadsheet<USER_DATA_TYPE = unknown> {
964
962
  */
965
963
  Select(range?: RangeReference): void;
966
964
 
965
+ /**
966
+ * override for paste method omits the data parameter.
967
+ */
968
+ Paste(target?: RangeReference, options?: PasteOptions): void;
969
+
970
+ /**
971
+ * standard paste method accepts data argument
972
+ *
973
+ * @param target
974
+ * @param data
975
+ * @param options
976
+ */
977
+ Paste(target?: RangeReference, data?: ClipboardData, options?: PasteOptions): void;
978
+
979
+ /**
980
+ * copy data. this method returns the copied data. it does not put it on
981
+ * the system clipboard. this is for API access when the system clipboard
982
+ * might not be available.
983
+ */
984
+ Copy(source?: RangeReference): ClipboardData;
985
+
986
+ /**
987
+ * cut data. this method returns the cut data. it does not put it on the
988
+ * system clipboard. this method is similar to the Copy method, with
989
+ * two differences: (1) we remove the source data, effectively clearing
990
+ * the source range; and (2) the clipboard data retains references, meaning
991
+ * if you paste the data in a different location it will refer to the same
992
+ * cells.
993
+ */
994
+ Cut(source?: RangeReference): ClipboardData;
995
+
967
996
  /**
968
997
  *
969
998
  * @param range target range. leave undefined to use current selection.
@@ -1268,6 +1297,10 @@ export interface Complex {
1268
1297
  real: number;
1269
1298
  imaginary: number;
1270
1299
  }
1300
+
1301
+ /**
1302
+ * dimensioned quantity: 3.2 m/s, 2kg, 5m, &c.
1303
+ */
1271
1304
  export interface DimensionedQuantity {
1272
1305
  value: number;
1273
1306
  unit: string;
@@ -1350,6 +1383,53 @@ export interface EvaluateOptions {
1350
1383
  */
1351
1384
  r1c1?: boolean;
1352
1385
  }
1386
+
1387
+ /** clipboard data is a 2d array */
1388
+ export type ClipboardData = ClipboardDataElement[][];
1389
+
1390
+ /**
1391
+ * optional paste options. we can paste formulas or values, and we
1392
+ * can use the source style, target style, or just use the source
1393
+ * number formats.
1394
+ */
1395
+ export interface PasteOptions {
1396
+
1397
+ /**
1398
+ * when clipboard data includes formulas, optionally paste calculated
1399
+ * values instead of the original formulas. defaults to false.
1400
+ */
1401
+ values?: boolean;
1402
+
1403
+ /**
1404
+ * when pasting data from the clipboard, we can copy formatting/style
1405
+ * from the original data, or we can retain the target range formatting
1406
+ * and just paste data. a third option allows pasting source number
1407
+ * formats but dropping other style information.
1408
+ *
1409
+ * defaults to "source", meaning paste source styles.
1410
+ */
1411
+ formatting?: 'source' | 'target' | 'number-formats';
1412
+ }
1413
+
1414
+ /**
1415
+ * this is a structure for copy/paste data. clipboard data may include
1416
+ * relative formauls and resolved styles, so it's suitable for pasting into
1417
+ * other areas of the spreadsheet.
1418
+ */
1419
+ export interface ClipboardDataElement {
1420
+
1421
+ /** calculated cell value */
1422
+ calculated: CellValue;
1423
+
1424
+ /** the actual cell value or formula */
1425
+ value: CellValue;
1426
+
1427
+ /** cell style. this may include row/column styles from the copy source */
1428
+ style?: CellStyle;
1429
+
1430
+ /** area. if this cell is part of an array, this is the array range */
1431
+ area?: IArea;
1432
+ }
1353
1433
  export declare type BorderConstants = "none" | "all" | "outside" | "top" | "bottom" | "left" | "right";
1354
1434
 
1355
1435
  /**
@@ -1589,23 +1669,6 @@ export interface SelectionEvent {
1589
1669
  export interface FocusViewEvent {
1590
1670
  type: 'focus-view';
1591
1671
  }
1592
- export interface SerializedMacroFunction {
1593
- name: string;
1594
- function_def: string;
1595
- argument_names?: string[];
1596
- description?: string;
1597
- }
1598
-
1599
- /**
1600
- * this type is no longer in use, but we retain it to parse old documents
1601
- * that use it.
1602
- *
1603
- * @deprecated
1604
- */
1605
- export interface SerializedNamedExpression {
1606
- name: string;
1607
- expression: string;
1608
- }
1609
1672
 
1610
1673
  /**
1611
1674
  * serialized type is a composite of expression/range. we determine
@@ -1970,6 +2033,23 @@ export interface Corner {
1970
2033
  address: ICellAddress;
1971
2034
  offset: AddressOffset;
1972
2035
  }
2036
+ export interface SerializedMacroFunction {
2037
+ name: string;
2038
+ function_def: string;
2039
+ argument_names?: string[];
2040
+ description?: string;
2041
+ }
2042
+
2043
+ /**
2044
+ * this type is no longer in use, but we retain it to parse old documents
2045
+ * that use it.
2046
+ *
2047
+ * @deprecated
2048
+ */
2049
+ export interface SerializedNamedExpression {
2050
+ name: string;
2051
+ expression: string;
2052
+ }
1973
2053
 
1974
2054
  /**
1975
2055
  * options for exporting CSV/TSV
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trebco/treb",
3
- "version": "29.5.4",
3
+ "version": "29.7.5",
4
4
  "license": "LGPL-3.0-or-later",
5
5
  "homepage": "https://treb.app",
6
6
  "repository": {
@@ -1210,6 +1210,40 @@ export class Cells {
1210
1210
 
1211
1211
  }
1212
1212
 
1213
+ /**
1214
+ * yet another iterator, this one returns cell and address.
1215
+ * iterates all cells; does not create missing cells.
1216
+ *
1217
+ * UPDATE: adding area parameter; not shrinking it (don't call w/ infinities)
1218
+ */
1219
+ public *IterateRC(area?: IArea) {
1220
+
1221
+ if (area) {
1222
+ for (let row = area.start.row; row <= area.end.row; row++) {
1223
+ const block = this.data[row];
1224
+ if (block) {
1225
+ for (let column = area.start.column; column <= area.end.column; column++) {
1226
+ const cell = block[column];
1227
+ if (cell) {
1228
+ yield { cell, row, column };
1229
+ }
1230
+ }
1231
+ }
1232
+ }
1233
+ }
1234
+ else {
1235
+ for (const [row, r] of this.data.entries()) {
1236
+ if (r) {
1237
+ for (const [column, cell] of r.entries()) {
1238
+ if (cell) {
1239
+ yield { cell, row, column };
1240
+ }
1241
+ }
1242
+ }
1243
+ }
1244
+ }
1245
+ }
1246
+
1213
1247
  /**
1214
1248
  * replacement for old callback iterator. this is a composite
1215
1249
  * of iterating all and iterating an area. I want to remove the
@@ -1,4 +1,7 @@
1
1
 
2
+ import type { ExpressionUnit } from 'treb-parser';
3
+ import type { ICellAddress } from './area';
4
+
2
5
  /**
3
6
  * options for the evaluate function
4
7
  */
@@ -17,5 +20,23 @@ export interface EvaluateOptions {
17
20
  * so we optionally support that behind this flag.
18
21
  */
19
22
  r1c1?: boolean;
20
-
23
+
24
+ /**
25
+ * @internal
26
+ *
27
+ * @privateRemarks
28
+ * in some cases we may call this method after parsing an expression.
29
+ * we don't need the evaluate method to parse it again.
30
+ */
31
+ preparsed?: ExpressionUnit;
32
+
33
+ /**
34
+ * @internal
35
+ *
36
+ * @privateRemarks
37
+ * we also might want to pass the address of the expression, if (or as if)
38
+ * it was originally in the spreadsheet.
39
+ */
40
+ address?: ICellAddress;
41
+
21
42
  }
@@ -105,15 +105,6 @@ export type UnionValue
105
105
  | ErrorUnion
106
106
  ;
107
107
 
108
- // common types
109
-
110
- /**
111
- * this is a factory instead of a constant value to prevent any accidental pollution
112
- */
113
- // export const CreateUndefinedUnion = (): UnionValue => {
114
- // return { type: ValueType.undefined, value: undefined };
115
- //};
116
-
117
108
  /**
118
109
  * shortcut, although this is wasteful
119
110
  *
@@ -136,8 +127,6 @@ export const Box = (value: unknown, type?: ValueType): UnionValue => {
136
127
 
137
128
  };
138
129
 
139
- // export type UnionOrArray = UnionValue|UnionValue[][];
140
-
141
130
  /**
142
131
  * box a complex value in a union, potentially switching to a real if
143
132
  * there's no imaginary component.
@@ -19,9 +19,6 @@
19
19
  *
20
20
  */
21
21
 
22
- // split from cell for separate import,
23
- // @see format-index.ts
24
-
25
22
  /**
26
23
  * Complex number type
27
24
  */
@@ -67,6 +64,9 @@ export const ComplexToString = (value: Complex): string => {
67
64
  }
68
65
  };
69
66
 
67
+ /**
68
+ * dimensioned quantity: 3.2 m/s, 2kg, 5m, &c.
69
+ */
70
70
  export interface DimensionedQuantity {
71
71
  value: number;
72
72
  unit: string;
@@ -1589,45 +1589,54 @@ export class Calculator extends Graph {
1589
1589
  /** moved from embedded sheet */
1590
1590
  public Evaluate(expression: string, active_sheet?: Sheet, options: EvaluateOptions = {}, raw_result = false) {
1591
1591
 
1592
- // const current = this.parser.argument_separator;
1593
- // const r1c1_state = this.parser.flags.r1c1;
1592
+ let parse_expression = options?.preparsed;
1594
1593
 
1595
- this.parser.Save();
1594
+ if (!parse_expression) {
1596
1595
 
1597
- if (options.argument_separator) {
1598
- if (options.argument_separator === ',') {
1599
- this.parser.SetLocaleSettings(DecimalMarkType.Period);
1596
+ this.parser.Save();
1600
1597
 
1601
- // this.parser.argument_separator = ArgumentSeparatorType.Comma;
1602
- // this.parser.decimal_mark = DecimalMarkType.Period;
1598
+ if (options.argument_separator) {
1599
+ if (options.argument_separator === ',') {
1600
+ this.parser.SetLocaleSettings(DecimalMarkType.Period);
1601
+
1602
+ // this.parser.argument_separator = ArgumentSeparatorType.Comma;
1603
+ // this.parser.decimal_mark = DecimalMarkType.Period;
1604
+ }
1605
+ else {
1606
+ this.parser.SetLocaleSettings(DecimalMarkType.Comma);
1607
+
1608
+ // this.parser.argument_separator = ArgumentSeparatorType.Semicolon;
1609
+ // this.parser.decimal_mark = DecimalMarkType.Comma;
1610
+ }
1603
1611
  }
1604
- else {
1605
- this.parser.SetLocaleSettings(DecimalMarkType.Comma);
1606
1612
 
1607
- // this.parser.argument_separator = ArgumentSeparatorType.Semicolon;
1608
- // this.parser.decimal_mark = DecimalMarkType.Comma;
1613
+ if (options.r1c1) {
1614
+ this.parser.flags.r1c1 = options.r1c1;
1609
1615
  }
1610
- }
1611
1616
 
1612
- if (options.r1c1) {
1613
- this.parser.flags.r1c1 = options.r1c1;
1614
- }
1617
+ const parse_result = this.parser.Parse(expression);
1618
+
1619
+ // reset
1615
1620
 
1616
- const parse_result = this.parser.Parse(expression);
1621
+ // this.parser.argument_separator = current;
1622
+ // this.parser.decimal_mark = (current === ArgumentSeparatorType.Comma) ? DecimalMarkType.Period : DecimalMarkType.Comma;
1623
+ // this.parser.flags.r1c1 = r1c1_state;
1617
1624
 
1618
- // reset
1625
+ this.parser.Restore();
1619
1626
 
1620
- // this.parser.argument_separator = current;
1621
- // this.parser.decimal_mark = (current === ArgumentSeparatorType.Comma) ? DecimalMarkType.Period : DecimalMarkType.Comma;
1622
- // this.parser.flags.r1c1 = r1c1_state;
1627
+ parse_expression = parse_result.expression;
1623
1628
 
1624
- this.parser.Restore();
1629
+ if (parse_result.error) {
1630
+ throw new Error(parse_result.error);
1631
+ }
1632
+
1633
+ }
1625
1634
 
1626
1635
  // OK
1627
1636
 
1628
- if (parse_result && parse_result.expression ){
1637
+ if (parse_expression ){
1629
1638
 
1630
- this.parser.Walk(parse_result.expression, (unit) => {
1639
+ this.parser.Walk(parse_expression, (unit) => {
1631
1640
  if (unit.type === 'address' || unit.type === 'range') {
1632
1641
 
1633
1642
  // don't allow offset references, even in R1C1
@@ -1648,7 +1657,7 @@ export class Calculator extends Graph {
1648
1657
  });
1649
1658
 
1650
1659
  // console.info({expression: parse_result.expression})
1651
- const result = this.CalculateExpression(parse_result.expression);
1660
+ const result = this.CalculateExpression(parse_expression, options.address);
1652
1661
  if (raw_result) {
1653
1662
  return result;
1654
1663
  }
@@ -1664,10 +1673,6 @@ export class Calculator extends Graph {
1664
1673
 
1665
1674
  // or? (...)
1666
1675
 
1667
- if (parse_result.error) {
1668
- throw new Error(parse_result.error);
1669
- }
1670
-
1671
1676
  throw new Error('invalid expression');
1672
1677
 
1673
1678
  }
@@ -76,6 +76,8 @@ export interface ArgumentDescriptor {
76
76
  *
77
77
  * if this flag is set in any argument descriptor in a function, we'll
78
78
  * apply arrays. that's done when the function is installed.
79
+ *
80
+ * FIXME: this should maybe be the default, and we have an !unroll flag
79
81
  */
80
82
  unroll?: boolean;
81
83