Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async importExcel(fileBuffer: any, modelName: string) {
const workbook = read(fileBuffer, {});
const worksheet = workbook.Sheets[workbook.SheetNames[0]];
const jsonArray = utils.sheet_to_json(worksheet, { header: 1 });
const repository = await this.getRepository(modelName);
const schemas = await this.getSchemas(repository);
const status = [];
for (let row = 1; row < jsonArray.length; row += 1) {
const entity = repository.create();
for (const [column, element] of schemas.entries()) {
if (jsonArray[0][column] === element.config.info.name) {
const keyName = element.name;
let value = jsonArray[row][column];
// 如果是外键关系表,则需要处理外键表数据
if (element.config.selectable !== undefined) {
const tempRepo = await this.getRepository(element.config.selectable);
if (!element.config.many) {
const res = await tempRepo.findOne({ name: jsonArray[row][column] } as any);
value = res;
} else {
const workbook = xlsxRead(binaryData, { raw: options.rawData as boolean });
if (workbook.SheetNames.length === 0) {
throw new Error('Spreadsheet does not have any sheets!');
}
let sheetName = workbook.SheetNames[0];
if (options.sheetName) {
if (!workbook.SheetNames.includes(options.sheetName as string)) {
throw new Error(`Spreadsheet does not contain sheet called "${options.sheetName}"!`);
}
sheetName = options.sheetName as string;
}
// Convert it to json
const sheetJson = xlsxUtils.sheet_to_json(workbook.Sheets[sheetName]);
// Check if data could be found in file
if (sheetJson.length === 0) {
continue;
}
// Add all the found data columns to the workflow data
for (const rowData of sheetJson) {
newItems.push({ json: rowData } as INodeExecutionData);
}
}
return this.prepareOutputData(newItems);
} else if (operation === 'toFile') {
// Write the workflow data to spreadsheet file
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', 0) as string;
export const readSpreadsheetData = (data, callback) => {
const workbook = xlsxRead(data, { type: 'binary' });
const sheetName = workbook.SheetNames[0];
const jsonObject = xlsxUtils.sheet_to_json(workbook.Sheets[sheetName], { raw: false });
callback(jsonObject);
};