Skip to content

TS: Add types for addTable function #940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -923,9 +923,9 @@ ws.addTable({
{name: 'Amount', totalsRowFunction: 'sum', filterButton: false},
],
rows: [
[new Date('2019-07-20', 70.10],
[new Date('2019-07-21', 70.60],
[new Date('2019-07-22', 70.10],
[new Date('2019-07-20'), 70.10],
[new Date('2019-07-21'), 70.60],
[new Date('2019-07-22'), 70.10],
],
});
```
Expand Down Expand Up @@ -993,6 +993,7 @@ by the table.
| min | The minimum value in this column |
| stdDev | The standard deviation for this column |
| var | The variance for this column |
| sum | The sum of entries for this column |
| custom | A custom formula. Requires an associated totalsRowFormula value. |

### Table Style Themes
Expand Down
137 changes: 137 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,11 @@ export interface Column {
* The cell values in the column
*/
values: ReadonlyArray<CellValue>;

/**
* Column letter key
*/
readonly letter: string;
}

export interface ColumnExtension extends Partial<Style> {
Expand Down Expand Up @@ -1153,6 +1158,15 @@ export interface Worksheet {
*/
protect(password: string, options: Partial<WorksheetProtection>): Promise<void>;
unprotect(): void;

/**
* Add a new table and return a reference to it
*/
addTable(tableProperties: TableProperties): Table;
/**
* fetch table by name or id
*/
getTable(name: string): Table;
}

export interface WorksheetProperties {
Expand Down Expand Up @@ -1454,6 +1468,129 @@ export class Workbook {
getImage(id: number): Image;
}

export interface TableStyleProperties {
/**
* The colour theme of the table
* @default 'TableStyleMedium2'
*/
theme?: 'TableStyleDark1' | 'TableStyleDark10' | 'TableStyleDark11' | 'TableStyleDark2' | 'TableStyleDark3' | 'TableStyleDark4' | 'TableStyleDark5' | 'TableStyleDark6' | 'TableStyleDark7' | 'TableStyleDark8' | 'TableStyleDark9' | 'TableStyleLight1' | 'TableStyleLight10' | 'TableStyleLight11' | 'TableStyleLight12' | 'TableStyleLight13' | 'TableStyleLight14' | 'TableStyleLight15' | 'TableStyleLight16' | 'TableStyleLight17' | 'TableStyleLight18' | 'TableStyleLight19' | 'TableStyleLight2' | 'TableStyleLight20' | 'TableStyleLight21' | 'TableStyleLight3' | 'TableStyleLight4' | 'TableStyleLight5' | 'TableStyleLight6' | 'TableStyleLight7' | 'TableStyleLight8' | 'TableStyleLight9' | 'TableStyleMedium1' | 'TableStyleMedium10' | 'TableStyleMedium11' | 'TableStyleMedium12' | 'TableStyleMedium13' | 'TableStyleMedium14' | 'TableStyleMedium15' | 'TableStyleMedium16' | 'TableStyleMedium17' | 'TableStyleMedium18' | 'TableStyleMedium19' | 'TableStyleMedium2' | 'TableStyleMedium20' | 'TableStyleMedium21' | 'TableStyleMedium22' | 'TableStyleMedium23' | 'TableStyleMedium24' | 'TableStyleMedium25' | 'TableStyleMedium26' | 'TableStyleMedium27' | 'TableStyleMedium28' | 'TableStyleMedium3' | 'TableStyleMedium4' | 'TableStyleMedium5' | 'TableStyleMedium6' | 'TableStyleMedium7' | 'TableStyleMedium8' | 'TableStyleMedium9';
/**
* Highlight the first column (bold)
* @default false
*/
showFirstColumn?: boolean;
/**
* Highlight the last column (bold)
* @default false
*/
showLastColumn?: boolean;
/**
* Alternate rows shown with background colour
* @default false
*/
showRowStripes?: boolean;
/**
* Alternate rows shown with background colour
* @default false
*/
showColumnStripes?: boolean;
}

export interface TableColumnProperties {
/**
* The name of the column, also used in the header
*/
name: string;
/**
* Switches the filter control in the header
* @default false
*/
filterButton?: boolean;
/**
* Label to describe the totals row (first column)
* @default 'Total'
*/
totalsRowLabel?: string;
/**
* Name of the totals function
* @default 'none'
*/
totalsRowFunction?: 'none' | 'average' | 'countNums' | 'count' | 'max' | 'min' | 'stdDev' | 'var' | 'sum' | 'custom';
/**
* Optional formula for custom functions
*/
totalsRowFormula?: string;
}


export interface TableProperties {
/**
* The name of the table
*/
name: string;
/**
* The display name of the table
*/
displayName?: string;
/**
* Top left cell of the table
*/
ref: string;
/**
* Show headers at top of table
* @default true
*/
headerRow?: boolean;
/**
* Show totals at bottom of table
* @default false
*/
totalsRow?: boolean;
/**
* Extra style properties
* @default {}
*/
style?: TableStyleProperties;
/**
* Column definitions
*/
columns: TableColumnProperties[]
/**
* Rows of data
*/
rows: any[][]
}

export type TableColumn = Required<TableColumnProperties>

export interface Table extends Required<TableProperties> {
/**
* Commit changes
*/
commit: () => void
/**
* Remove a rows of data
*/
removeRows: (rowIndex: number, count: number) => void
/**
* Add a row of data, either insert at rowNumber or append
*/
addRow: (values: any[], rowNumber: number) => void
/**
* Get column
*/
getColumn: (colIndex: number) => TableColumn
/**
* Add a new column, including column defn and values
* inserts at colNumber or adds to the right
*/
addColumn: (column: TableColumnProperties, values: any[], colIndex: number) => void
/**
* Remove a column with data
*/
removeColumns: (colIndex: number, count: number) => void
}

export namespace config {
function setValue(key: 'promise', promise: any): void;
}
Expand Down
1 change: 1 addition & 0 deletions lib/doc/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Table {
case 'min': return `SUBTOTAL(105,${this.table.name}[${column.name}])`;
case 'stdDev': return `SUBTOTAL(106,${this.table.name}[${column.name}])`;
case 'var': return `SUBTOTAL(107,${this.table.name}[${column.name}])`;
case 'sum': return `SUBTOTAL(109,${this.table.name}[${column.name}])`;
case 'custom': return column.totalsRowFormula;
default:
throw new Error(`Invalid Totals Row Function: ${column.totalsRowFunction}`);
Expand Down