* Creates column at the right of the data array. * * @param {number} [index] Visual index of the column before which the new column will be inserted. * @param {number} [amount=1] An amount of columns to add. * @param {object} [options] Additional options for created columns. * @param
(index: number | undefined, amount = 1,
{ source, mode = 'start' }: { source?: string; mode?: 'start' | 'end' } = {})
| 461 | * @returns {number} Returns number of created columns. |
| 462 | */ |
| 463 | createCol(index: number | undefined, amount = 1, |
| 464 | { source, mode = 'start' }: { source?: string; mode?: 'start' | 'end' } = {}) { |
| 465 | if (!this.hot!.isColumnModificationAllowed()) { |
| 466 | throwWithCause('Cannot create new column. When data source in an object, ' + |
| 467 | // eslint-disable-next-line max-len |
| 468 | 'you can only have as much columns as defined in first data row, data schema or in the \'columns\' setting.' + |
| 469 | 'If you want to be able to add new columns, you have to use array datasource.'); |
| 470 | } |
| 471 | |
| 472 | const dataSource = this.dataSource ?? []; |
| 473 | const maxCols = this.tableMeta.maxCols; |
| 474 | const numberOfSourceCols = this.hot!.countSourceCols(); |
| 475 | const numberOfVisualCols = this.hot!.countCols(); |
| 476 | const numberOfSourceRows = this.hot!.countSourceRows(); |
| 477 | const visualColumnIndex = (typeof index === 'number' && index <= numberOfSourceCols) ? index : numberOfVisualCols; |
| 478 | |
| 479 | if (this.hot!.runHooks('beforeCreateCol', visualColumnIndex, amount, source) === false) { |
| 480 | return { delta: 0 }; |
| 481 | } |
| 482 | |
| 483 | const physicalColumnIndex = (visualColumnIndex < numberOfVisualCols) ? |
| 484 | this.hot!.toPhysicalColumn(visualColumnIndex) : numberOfSourceCols; |
| 485 | const firstNewPhysicalColumnIndex = (mode === 'end') ? |
| 486 | Math.min(physicalColumnIndex + 1, numberOfSourceCols) : physicalColumnIndex; |
| 487 | |
| 488 | let numberOfCreatedCols = 0; |
| 489 | |
| 490 | for ( |
| 491 | let col = firstNewPhysicalColumnIndex; |
| 492 | numberOfCreatedCols < amount && numberOfVisualCols + numberOfCreatedCols < (maxCols ?? Infinity); |
| 493 | col++, numberOfCreatedCols++ |
| 494 | ) { |
| 495 | this.#insertColumnIntoDataSource( |
| 496 | dataSource, col, visualColumnIndex, numberOfVisualCols + numberOfCreatedCols, numberOfSourceRows |
| 497 | ); |
| 498 | } |
| 499 | |
| 500 | if (numberOfCreatedCols > 0) { |
| 501 | if ((index === undefined || index === null)) { |
| 502 | // Creates the meta columns at the end of the columns collection without shifting the cells |
| 503 | // that were defined out of the range of the dataset. |
| 504 | this.metaManager!.createColumn(null, numberOfCreatedCols); |
| 505 | |
| 506 | } else if (source !== 'auto') { |
| 507 | this.metaManager!.createColumn(firstNewPhysicalColumnIndex, amount); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | this.hot!.columnIndexMapper.insertIndexes(visualColumnIndex, numberOfCreatedCols, mode); |
| 512 | |
| 513 | this.hot!.runHooks( |
| 514 | 'afterCreateCol', |
| 515 | this.hot!.toVisualColumn(firstNewPhysicalColumnIndex), |
| 516 | numberOfCreatedCols, |
| 517 | source |
| 518 | ); |
| 519 | this.refreshDuckSchema(); |
| 520 |
no test coverage detected