Skip to content
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

feat: support excel import #431

Merged
merged 17 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Copyright (c) 2012-2021 Olivier Louvignes

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.

Except where noted, this license applies to any and all software programs and associated documentation files created by
the Original Author and distributed with the Software:

Inspired by SheetJS gist examples, Copyright (c) SheetJS.
1 change: 1 addition & 0 deletions apps/nestjs-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
"transliteration": "2.3.5",
"ts-pattern": "5.0.8",
"ws": "8.16.0",
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz",
"zod": "3.22.4",
"zod-validation-error": "3.0.3"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import { FieldKeyType } from '@teable/core';
import type { IAnalyzeRo, IImportOptionRo } from '@teable/core';
import { RecordOpenApiService } from '../../record/open-api/record-open-api.service';
Expand All @@ -8,80 +8,88 @@ import { importerFactory } from './import.class';

@Injectable()
export class ImportOpenApiService {
// private logger = new Logger(ImportOpenApiService.name);
private logger = new Logger(ImportOpenApiService.name);
constructor(
private readonly tableOpenApiService: TableOpenApiService,
private readonly recordOpenApiService: RecordOpenApiService
) {}

async analyze(analyzeRo: IAnalyzeRo) {
const { attachmentUrl, fileType } = analyzeRo;

const importer = importerFactory(fileType, {
url: attachmentUrl,
fileType,
type: fileType,
});

return await importer.genColumns();
}

async createTableFromImport(baseId: string, importRo: IImportOptionRo) {
// TODO support groups
const { attachmentUrl, fileType, worksheets } = importRo;

const {
options: { importData, useFirstRowAsHeader },
columns: columnInfo,
name,
} = worksheets[0];

const importer = importerFactory(fileType, {
url: attachmentUrl,
fileType,
});
const fieldsRo = columnInfo.map((col, index) => {
return {
...col,
isPrimary: index === 0 ? true : null,
};
type: fileType,
});

// create table with column
const table = await this.tableOpenApiService.createTable(baseId, {
name: name || 'import table',
fields: fieldsRo,
views: DEFAULT_VIEWS,
records: [],
});
const { fields } = table;
const tableResult = [];

for (const [sheetKey, value] of Object.entries(worksheets)) {
const { importData, useFirstRowAsHeader, columns: columnInfo, name } = value;
const fieldsRo = columnInfo.map((col, index) => {
return {
...col,
isPrimary: index === 0 ? true : null,
};
});

if (importData) {
await importer.streamParse(
{
skipFirstNLines: useFirstRowAsHeader ? 1 : 0,
},
async (result) => {
// fill data
const records = result.map((row) => {
const res: { fields: Record<string, unknown> } = {
fields: {},
};
columnInfo.forEach((col, index) => {
res.fields[fields[index].id] = row[col.sourceColumnIndex];
// create table with column
const table = await this.tableOpenApiService.createTable(baseId, {
name: name,
fields: fieldsRo,
views: DEFAULT_VIEWS,
records: [],
});

tableResult.push(table);

const { fields } = table;

if (importData) {
importer.parse(
{
skipFirstNLines: useFirstRowAsHeader ? 1 : 0,
key: sheetKey,
},
async (result) => {
const currentResult = result[sheetKey];
// fill data
const records = currentResult.map((row) => {
const res: { fields: Record<string, unknown> } = {
fields: {},
};
columnInfo.forEach((col, index) => {
res.fields[fields[index].id] = row[col.sourceColumnIndex];
});
return res;
});
return res;
});
if (records.length === 0) {
return;
if (records.length === 0) {
return;
}
try {
await this.recordOpenApiService.multipleCreateRecords(table.id, {
fieldKeyType: FieldKeyType.Id,
typecast: true,
records,
});
} catch (e) {
this.logger.error((e as Error)?.message, 'Import: Records');
}
}
await this.recordOpenApiService.multipleCreateRecords(table.id, {
fieldKeyType: FieldKeyType.Id,
typecast: true,
records,
});
}
);
);
}
}

return [table];
return tableResult;
}
}