Skip to content

Commit

Permalink
Added exportCSV and exportTable
Browse files Browse the repository at this point in the history
  • Loading branch information
jadbox authored Feb 18, 2021
1 parent 3458ef7 commit 7aceb21
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/table-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,31 @@ export class TableEditor {
});
}

/**
* Exports the table as a two dimensional string array
*/
public exportTable(withtHeaders:boolean, options: Options): string[][] | undefined {
return this.withCompletedTable(
options,
({ range, lines, formulaLines, table, focus }: TableInfo) => {
const bodyRows = table.getRows();
if(bodyRows.length > 0 && !withtHeaders) {
bodyRows.splice(0, 2);
}
// else if(bodyRows.length > 1) bodyRows.splice(1, 1);
return bodyRows.map(row=>row.getCells().map(cell=>cell.content));
},
);
}

/**
* Exports the table as a two dimensional string array
*/
public exportCSV(withtHeaders:boolean, options: Options): string | undefined {
const r = this.exportTable(withtHeaders, options);
return !r ? undefined : r.map(row=>row.join('\t')).join('\n');
}

/**
* Finds a table, completes it, then does an operation with it.
*
Expand Down
62 changes: 62 additions & 0 deletions test/table-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5535,6 +5535,68 @@ describe('TableEditor', () => {
});
});

/**
* @test {TableEditor#exportTable}
*/
describe('#exportTable(withHeader, defaultOptions)', () => {
it('should export out the grid as two dimensional array with headers', () => {
{
const textEditor = new TextEditor([
'| A | B |',
'| C | D |',
'| E | F |'
]);
const tableEditor = new TableEditor(textEditor);
const result = tableEditor.exportTable(true, defaultOptions);
expect(result).to.be.eql([['A', 'B'], ['---', '---'], ['C', 'D'], ['E', 'F']]);
}
})

it('should export out the grid as two dimensional array without headers', () => {
{
const textEditor = new TextEditor([
'| A | B |',
'| C | D |',
'| E | F |'
]);
const tableEditor = new TableEditor(textEditor);
const result = tableEditor.exportTable(false, defaultOptions);
expect(result).to.be.eql([['C', 'D'], ['E', 'F']]);
}
})
});

/**
* @test {TableEditor#exportCSV}
*/
describe('#exportCSV(withHeader, defaultOptions)', () => {
it('should export out the grid as two dimensional array with headers', () => {
{
const textEditor = new TextEditor([
'| A | B |',
'| C | D |',
'| E | F |'
]);
const tableEditor = new TableEditor(textEditor);
const result = tableEditor.exportCSV(true, defaultOptions);
expect(result).to.be.eql('A\tB\n---\t---\nC\tD\nE\tF');
}
})

it('should export out the grid as two dimensional array without headers', () => {
{
const textEditor = new TextEditor([
'| A | B |',
'| C | D |',
'| E | F |'
]);
const tableEditor = new TableEditor(textEditor);
const result = tableEditor.exportCSV(false, defaultOptions);
expect(result).to.be.eql('C\tD\nE\tF');
}
})
});

/**
* @test {TableEditor#formatAll}
*/
Expand Down

0 comments on commit 7aceb21

Please sign in to comment.