n8n-nodes-base
Version:
Base nodes of n8n
226 lines • 9.82 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SpreadsheetFileV1 = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const xlsx_1 = require("xlsx");
const descriptions_1 = require("../../../utils/descriptions");
const utilities_1 = require("../../../utils/utilities");
const description_1 = require("../description");
class SpreadsheetFileV1 {
description;
constructor(baseDescription) {
this.description = {
...baseDescription,
version: 1,
defaults: {
name: 'Spreadsheet File',
color: '#2244FF',
},
inputs: [n8n_workflow_1.NodeConnectionTypes.Main],
outputs: [n8n_workflow_1.NodeConnectionTypes.Main],
properties: [
descriptions_1.oldVersionNotice,
description_1.operationProperty,
description_1.binaryProperty,
...description_1.toFileProperties,
description_1.fromFileOptions,
description_1.toFileOptions,
],
};
}
async execute() {
const items = this.getInputData();
const pairedItem = (0, utilities_1.generatePairedItemData)(items.length);
const operation = this.getNodeParameter('operation', 0);
const newItems = [];
if (operation === 'fromFile') {
// Read data from spreadsheet file to workflow
for (let i = 0; i < items.length; i++) {
try {
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
const options = this.getNodeParameter('options', i, {});
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
// Read the binary spreadsheet data
let workbook;
const xlsxOptions = { raw: options.rawData };
if (options.readAsString)
xlsxOptions.type = 'string';
if (binaryData.id) {
const binaryPath = this.helpers.getBinaryPath(binaryData.id);
xlsxOptions.codepage = 65001; // utf8 codepage
workbook = (0, xlsx_1.readFile)(binaryPath, xlsxOptions);
}
else {
const binaryDataBuffer = Buffer.from(binaryData.data, n8n_workflow_1.BINARY_ENCODING);
workbook = (0, xlsx_1.read)(options.readAsString ? binaryDataBuffer.toString() : binaryDataBuffer, xlsxOptions);
}
if (workbook.SheetNames.length === 0) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Spreadsheet does not have any sheets!', {
itemIndex: i,
});
}
let sheetName = workbook.SheetNames[0];
if (options.sheetName) {
if (!workbook.SheetNames.includes(options.sheetName)) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Spreadsheet does not contain sheet called "${options.sheetName}"!`, { itemIndex: i });
}
sheetName = options.sheetName;
}
// Convert it to json
const sheetToJsonOptions = {};
if (options.range) {
if (isNaN(options.range)) {
sheetToJsonOptions.range = options.range;
}
else {
sheetToJsonOptions.range = parseInt(options.range, 10);
}
}
if (options.includeEmptyCells) {
sheetToJsonOptions.defval = '';
}
if (options.headerRow === false) {
sheetToJsonOptions.header = 1; // Consider the first row as a data row
}
const sheetJson = xlsx_1.utils.sheet_to_json(workbook.Sheets[sheetName], sheetToJsonOptions);
// Check if data could be found in file
if (sheetJson.length === 0) {
continue;
}
// Add all the found data columns to the workflow data
if (options.headerRow === false) {
// Data was returned as an array - https://github.com/SheetJS/sheetjs#json
for (const rowData of sheetJson) {
newItems.push({
json: {
row: rowData,
},
pairedItem: {
item: i,
},
});
}
}
else {
for (const rowData of sheetJson) {
newItems.push({
json: rowData,
pairedItem: {
item: i,
},
});
}
}
}
catch (error) {
if (this.continueOnFail()) {
newItems.push({
json: {
error: error.message,
},
pairedItem: {
item: i,
},
});
continue;
}
throw error;
}
}
return [newItems];
}
else if (operation === 'toFile') {
try {
// Write the workflow data to spreadsheet file
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', 0);
const fileFormat = this.getNodeParameter('fileFormat', 0);
const options = this.getNodeParameter('options', 0, {});
const sheetToJsonOptions = {};
if (options.headerRow === false) {
sheetToJsonOptions.skipHeader = true;
}
// Get the json data of the items and flatten it
let item;
const itemData = [];
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
item = items[itemIndex];
itemData.push((0, utilities_1.flattenObject)(item.json));
}
const ws = xlsx_1.utils.json_to_sheet(itemData, sheetToJsonOptions);
const wopts = {
bookSST: false,
type: 'buffer',
};
if (fileFormat === 'csv') {
wopts.bookType = 'csv';
}
else if (fileFormat === 'html') {
wopts.bookType = 'html';
}
else if (fileFormat === 'rtf') {
wopts.bookType = 'rtf';
}
else if (fileFormat === 'ods') {
wopts.bookType = 'ods';
if (options.compression) {
wopts.compression = true;
}
}
else if (fileFormat === 'xls') {
wopts.bookType = 'xls';
}
else if (fileFormat === 'xlsx') {
wopts.bookType = 'xlsx';
if (options.compression) {
wopts.compression = true;
}
}
// Convert the data in the correct format
const sheetName = options.sheetName || 'Sheet';
const wb = {
SheetNames: [sheetName],
Sheets: {
[sheetName]: ws,
},
};
const wbout = (0, xlsx_1.write)(wb, wopts);
// Create a new item with only the binary spreadsheet data
const newItem = {
json: {},
binary: {},
pairedItem,
};
let fileName = `spreadsheet.${fileFormat}`;
if (options.fileName !== undefined) {
fileName = options.fileName;
}
newItem.binary[binaryPropertyName] = await this.helpers.prepareBinaryData(wbout, fileName);
newItems.push(newItem);
}
catch (error) {
if (this.continueOnFail()) {
newItems.push({
json: {
error: error.message,
},
pairedItem,
});
}
else {
throw error;
}
}
}
else {
if (this.continueOnFail()) {
return [[{ json: { error: `The operation "${operation}" is not supported!` } }]];
}
else {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `The operation "${operation}" is not supported!`);
}
}
return [newItems];
}
}
exports.SpreadsheetFileV1 = SpreadsheetFileV1;
//# sourceMappingURL=SpreadsheetFileV1.node.js.map