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",
"nodeVersion": "1.0",
"codexVersion": "1.0",
"nodeVersion": "1.1",
"codexVersion": "1.1",
"categories": [
"Data & Storage",
"Core Nodes"

View file

@ -27,7 +27,7 @@ import {
* @param {IDataObject} data The object to flatten
* @returns
*/
function flattenObject (data: IDataObject) {
function flattenObject(data: IDataObject) {
const returnData: IDataObject = {};
for (const key1 of Object.keys(data)) {
if (data[key1] !== null && (typeof data[key1]) === 'object') {
@ -296,6 +296,20 @@ export class SpreadsheetFile implements INodeType {
default: 'Sheet',
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) {
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);
@ -368,8 +385,15 @@ export class SpreadsheetFile implements INodeType {
}
// Add all the found data columns to the workflow data
for (const rowData of sheetJson) {
newItems.push({ json: rowData } as INodeExecutionData);
if (options.headerRow) {
for (const rowData of sheetJson) {
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);
}
}
}