From ec30f10091b5172535fc7be69e8fe5136d91e063 Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Fri, 27 Dec 2019 10:54:51 -0600 Subject: [PATCH] :zap: Make it possible to read from specific sheet --- .../nodes-base/nodes/SpreadsheetFile.node.ts | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/nodes-base/nodes/SpreadsheetFile.node.ts b/packages/nodes-base/nodes/SpreadsheetFile.node.ts index e43b4b2d5e..37fb564e09 100644 --- a/packages/nodes-base/nodes/SpreadsheetFile.node.ts +++ b/packages/nodes-base/nodes/SpreadsheetFile.node.ts @@ -196,6 +196,20 @@ export class SpreadsheetFile implements INodeType { default: false, description: 'If the data should be returned RAW instead of parsed.', }, + { + displayName: 'Sheet Name', + name: 'sheetName', + type: 'string', + displayOptions: { + show: { + '/operation': [ + 'fromFile', + ], + }, + }, + default: 'Sheet', + description: 'Name of the sheet to read from in the spreadsheet (if supported). If not set, the first one gets chosen.', + }, { displayName: 'Sheet Name', name: 'sheetName', @@ -212,7 +226,7 @@ export class SpreadsheetFile implements INodeType { }, }, default: 'Sheet', - description: 'Name of the Sheet in the Spreadsheet.', + description: 'Name of the sheet to create in the spreadsheet.', }, ], }, @@ -248,11 +262,19 @@ export class SpreadsheetFile implements INodeType { const workbook = xlsxRead(binaryData, { raw: options.rawData as boolean }); if (workbook.SheetNames.length === 0) { - throw new Error('File does not have any sheets!'); + 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[workbook.SheetNames[0]]); + const sheetJson = xlsxUtils.sheet_to_json(workbook.Sheets[sheetName]); // Check if data could be found in file if (sheetJson.length === 0) {