Add support for files without headers to SpreadsheetFile node (#1738)

When false the first row of the spreadsheet file is considered a data row and each row is parsed as an array
This commit is contained in:
fntb 2021-05-08 05:50:25 +02:00 committed by GitHub
parent ffc0c7b4d5
commit 43fae950e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 5 deletions

View file

@ -1,7 +1,7 @@
{ {
"node": "n8n-nodes-base.spreadsheetFile", "node": "n8n-nodes-base.spreadsheetFile",
"nodeVersion": "1.0", "nodeVersion": "1.1",
"codexVersion": "1.0", "codexVersion": "1.1",
"categories": [ "categories": [
"Data & Storage", "Data & Storage",
"Core Nodes" "Core Nodes"

View file

@ -296,6 +296,20 @@ export class SpreadsheetFile implements INodeType {
default: 'Sheet', default: 'Sheet',
description: 'Name of the sheet to create in the spreadsheet.', description: 'Name of the sheet to create in the spreadsheet.',
}, },
{
displayName: 'Header Row',
name: 'headerRow',
type: 'boolean',
displayOptions: {
show: {
'/operation': [
'fromFile',
],
},
},
default: true,
description: 'Consider the first row as the header row or a data row.',
},
], ],
}, },
], ],
@ -359,6 +373,9 @@ export class SpreadsheetFile implements INodeType {
if (options.includeEmptyCells) { if (options.includeEmptyCells) {
sheetToJsonOptions.defval = ''; sheetToJsonOptions.defval = '';
} }
if (!options.headerRow) {
sheetToJsonOptions.header = 1; // Consider the first row as a data row
}
const sheetJson = xlsxUtils.sheet_to_json(workbook.Sheets[sheetName], sheetToJsonOptions); const sheetJson = xlsxUtils.sheet_to_json(workbook.Sheets[sheetName], sheetToJsonOptions);
@ -368,9 +385,16 @@ export class SpreadsheetFile implements INodeType {
} }
// Add all the found data columns to the workflow data // Add all the found data columns to the workflow data
if (options.headerRow) {
for (const rowData of sheetJson) { for (const rowData of sheetJson) {
newItems.push({ json: rowData } as INodeExecutionData); newItems.push({ json: rowData } as INodeExecutionData);
} }
} else {
// Data was returned as an array - https://github.com/SheetJS/sheetjs#json
for (const rowData of sheetJson) {
newItems.push({ json: { row: rowData } } as INodeExecutionData);
}
}
} }
return this.prepareOutputData(newItems); return this.prepareOutputData(newItems);