@/utils#copyData TypeScript Examples

The following examples show how to use @/utils#copyData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: issue-446-spec.ts    From S2 with MIT License 6 votes vote down vote up
describe('export', () => {
  test('should export correct data with showSeriesNumber', () => {
    const s2 = new TableSheet(getContainer(), mockDataConfig, s2Options);
    s2.render();
    const data = copyData(s2, '\t');

    expect(data.split('\n').length).toEqual(3);
    expect(data.split('\n')[0].split('\t').length).toEqual(4);
    expect(data.split('\n')[0].split('\t')[0]).toEqual('"序号"');
    expect(data.length).toEqual(89);
  });

  test('should export correct data without showSeriesNumber', () => {
    const s2 = new TableSheet(getContainer(), mockDataConfig, {
      ...s2Options,
      showSeriesNumber: false,
    });
    s2.render();
    const data = copyData(s2, '\t');

    expect(data.split('\n').length).toEqual(3);
    expect(data.split('\n')[0].split('\t').length).toEqual(3);
    expect(data.split('\n')[0].split('\t')[0]).toEqual('"col0"');
    expect(data.length).toEqual(76);
  });
});
Example #2
Source File: issue-565-spec.ts    From S2 with MIT License 6 votes vote down vote up
describe('Export data in pivot tree mode', () => {
  test('should export correct col header in pivot tree mode', () => {
    const s2 = new PivotSheet(getContainer(), mockDataConfig, s2Options);
    s2.render();
    const data = copyData(s2, '\t');
    const rows = data.split('\n');

    expect(rows.length).toEqual(9);
    expect(rows[0].split('\t').length).toEqual(5);
    expect(rows[0].split('\t')[0]).toEqual('');
    expect(rows[1].split('\t')[0]).toEqual('');
    expect(rows[7].split('\t')[0]).toEqual('"row0"');
    expect(rows[8].split('\t')[0]).toEqual('"row0"');
    expect(data.length).toEqual(263);
  });
});
Example #3
Source File: export-spec.ts    From S2 with MIT License 5 votes vote down vote up
describe('TableSheet Export Test', () => {
  it('should export correct data with series number', () => {
    const s2 = new TableSheet(
      getContainer(),
      assembleDataCfg({
        meta: [],
        fields: {
          columns: ['province', 'city', 'type', 'sub_type', 'number'],
        },
      }),
      assembleOptions({
        showSeriesNumber: true,
      }),
    );
    s2.render();
    const data = copyData(s2, '\t');
    const rows = data.split('\n');
    const headers = rows[0].split('\t');
    // 33行数据 包括一行列头
    expect(rows).toHaveLength(33);
    // 6列数据 包括序列号
    rows.forEach((e) => {
      expect(e.split('\t')).toHaveLength(6);
    });
    expect(headers.map((e) => JSON.parse(e))).toEqual([
      '序号',
      'province',
      'city',
      'type',
      'sub_type',
      'number',
    ]);
  });
  it('should export correct data with no series number', () => {
    const s2 = new TableSheet(
      getContainer(),
      assembleDataCfg({
        meta: [],
        fields: {
          columns: ['province', 'city', 'type', 'sub_type', 'number'],
        },
      }),
      assembleOptions({
        showSeriesNumber: false,
      }),
    );
    s2.render();
    const data = copyData(s2, '\t');
    const rows = data.split('\n');
    const headers = rows[0].split('\t');
    // 33行数据 包括一行列头
    expect(rows).toHaveLength(33);
    // 5列数据 包括序列号
    rows.forEach((e) => {
      expect(e.split('\t')).toHaveLength(5);
    });
    expect(headers.map((e) => JSON.parse(e))).toEqual([
      'province',
      'city',
      'type',
      'sub_type',
      'number',
    ]);
  });
});
Example #4
Source File: export-spec.ts    From S2 with MIT License 4 votes vote down vote up
describe('PivotSheet Export Test', () => {
  it('should export correct data in grid mode', () => {
    const s2 = new PivotSheet(
      getContainer(),
      assembleDataCfg({
        meta: [],
        fields: {
          valueInCols: true,
          columns: ['province', 'city', 'type', 'sub_type', 'number'],
        },
      }),
      assembleOptions({ hierarchyType: 'grid' }),
    );
    s2.render();
    const data = copyData(s2, '\t');
    const rows = data.split('\n');
    expect(rows).toHaveLength(14);
    expect(rows[0].split('\t')[1]).toEqual('"province"');
    expect(rows[1].split('\t')[1]).toEqual('"city"');

    rows.forEach((e) => {
      expect(e.split('\t')).toHaveLength(34);
    });
  });
  it('should export correct data in tree mode', () => {
    const s2 = new PivotSheet(
      getContainer(),
      assembleDataCfg({
        meta: [],
        fields: {
          valueInCols: true,
          columns: ['province', 'city', 'type', 'sub_type', 'number'],
        },
      }),
      assembleOptions({
        hierarchyType: 'tree',
      }),
    );
    s2.render();
    const data = copyData(s2, '\t');
    const rows = data.split('\n');
    expect(rows).toHaveLength(16);
    expect(rows[0].split('\t')[1]).toEqual('"province"');
    expect(rows[1].split('\t')[1]).toEqual('"city"');
    rows.forEach((e) => {
      expect(e.split('\t')).toHaveLength(34);
    });
  });
  it('should export correct data in grid mode with valueInCols is false', () => {
    const s2 = new PivotSheet(
      getContainer(),
      assembleDataCfg({
        meta: [],
        fields: {
          valueInCols: false,
          columns: ['province', 'city', 'type', 'sub_type', 'number'],
        },
      }),
      assembleOptions({
        hierarchyType: 'grid',
      }),
    );
    s2.render();
    const data = copyData(s2, '\t');
    const rows = data.split('\n');
    expect(rows).toHaveLength(13);
    rows.forEach((e) => {
      expect(e.split('\t')).toHaveLength(35);
    });
  });

  it('should export correct data in grid mode with totals in col', () => {
    const s2 = new PivotSheet(
      getContainer(),
      assembleDataCfg({
        fields: {
          valueInCols: true,
          columns: ['province', 'city', 'type', 'sub_type', 'number'],
        },
      }),
      assembleOptions({
        hierarchyType: 'grid',
        totals: {
          row: {
            showGrandTotals: true,
            showSubTotals: true,
            subTotalsDimensions: ['province'],
          },
          col: {
            showGrandTotals: true,
            showSubTotals: true,
            subTotalsDimensions: ['type'],
          },
        },
      }),
    );
    s2.render();
    const data = copyData(s2, '\t');
    const rows = data.split('\n');
    expect(rows).toHaveLength(17);
    rows.forEach((e) => {
      expect(e.split('\t')).toHaveLength(53);
    });
  });

  it('should export correct data in grid mode with totals in row', () => {
    const s2 = new PivotSheet(
      getContainer(),
      assembleDataCfg({
        fields: {
          valueInCols: false,
          columns: ['province', 'city', 'type', 'sub_type', 'number'],
        },
      }),
      assembleOptions({
        hierarchyType: 'grid',
        totals: {
          row: {
            showGrandTotals: true,
            showSubTotals: true,
            subTotalsDimensions: ['province'],
          },
          col: {
            showGrandTotals: true,
            showSubTotals: true,
            subTotalsDimensions: ['type'],
          },
        },
      }),
    );
    s2.render();
    const data = copyData(s2, '\t');
    const rows = data.split('\n');
    expect(rows).toHaveLength(16);
    rows.forEach((e) => {
      expect(e.split('\t')).toHaveLength(54);
    });
  });

  it('should export correct data when isFormat: {isFormatHeader: true}', () => {
    const s2 = new PivotSheet(
      getContainer(),
      assembleDataCfg({
        meta: [
          {
            field: 'province',
            formatter: (value) => {
              return `${value}-province`;
            },
          },
          {
            field: 'type',
            formatter: (value) => {
              return `${value}-type`;
            },
          },
        ],
        fields: {
          valueInCols: true,
          columns: ['province', 'city'],
          rows: ['type', 'sub_type'],
          values: ['number'],
        },
      }),
      assembleOptions({}),
    );
    s2.render();
    const data = copyData(s2, '\t', { isFormatHeader: true });
    const rows = data.split('\n');
    expect(rows).toHaveLength(7);
    expect(rows[0].split('\t')[1]).toEqual('"province"');
    expect(rows[0].split('\t')[2]).toEqual('"浙江省-province"');
    expect(rows[1].split('\t')[1]).toEqual('"city"');
    expect(rows[3].split('\t')[0]).toEqual('"家具-type"');
  });
});