Skip to content

Commit

Permalink
fix(Google Sheets Node): Append or update runs forever when without c…
Browse files Browse the repository at this point in the history
…olumn headers (n8n-io#7463)

Github issue / Community forum post (link here to close automatically):

---------

Co-authored-by: Marcus <marcus@n8n.io>
  • Loading branch information
michael-radency and maspio authored Oct 19, 2023
1 parent 689360e commit ab6a9bb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,21 @@ export async function execute(
const updateData: ISheetUpdateData[] = [];
const appendData: IDataObject[] = [];

const errorOnUnexpectedColumn = (key: string, i: number) => {
if (!columnNames.includes(key)) {
throw new NodeOperationError(this.getNode(), 'Unexpected fields in node input', {
itemIndex: i,
description: `The input field '${key}' doesn't match any column in the Sheet. You can ignore this by changing the 'Handling extra data' field, which you can find under 'Options'.`,
});
}
};

const addNewColumn = (key: string) => {
if (!columnNames.includes(key)) {
newColumns.add(key);
}
};

const mappedValues: IDataObject[] = [];
for (let i = 0; i < items.length; i++) {
if (dataMode === 'nothing') continue;
Expand All @@ -292,22 +307,11 @@ export async function execute(
data.push(items[i].json);
}
if (handlingExtraDataOption === 'error') {
Object.keys(items[i].json).forEach((key) => {
if (!columnNames.includes(key)) {
throw new NodeOperationError(this.getNode(), 'Unexpected fields in node input', {
itemIndex: i,
description: `The input field '${key}' doesn't match any column in the Sheet. You can ignore this by changing the 'Handling extra data' field, which you can find under 'Options'.`,
});
}
});
Object.keys(items[i].json).forEach((key) => errorOnUnexpectedColumn(key, i));
data.push(items[i].json);
}
if (handlingExtraDataOption === 'insertInNewColumn') {
Object.keys(items[i].json).forEach((key) => {
if (!columnNames.includes(key)) {
newColumns.add(key);
}
});
Object.keys(items[i].json).forEach(addNewColumn);
data.push(items[i].json);
}
} else {
Expand All @@ -324,6 +328,7 @@ export async function execute(
"At least one value has to be added under 'Values to Send'",
);
}
// eslint-disable-next-line @typescript-eslint/no-loop-func
const fields = valuesToSend.reduce((acc, entry) => {
if (entry.column === 'newColumn') {
const columnName = entry.columnName as string;
Expand Down Expand Up @@ -358,12 +363,15 @@ export async function execute(
}

if (newColumns.size) {
const newColumnNames = columnNames.concat([...newColumns]);
await sheet.updateRows(
sheetName,
[columnNames.concat([...newColumns])],
[newColumnNames],
(options.cellFormat as ValueInputOption) || cellFormatDefault(nodeVersion),
headerRow + 1,
);
columnNames = newColumnNames;
newColumns.clear();
}

const preparedData = await sheet.prepareDataForUpdateOrUpsert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,21 @@ export async function execute(

const mappedValues: IDataObject[] = [];

const errorOnUnexpectedColumn = (key: string, i: number) => {
if (!columnNames.includes(key)) {
throw new NodeOperationError(this.getNode(), 'Unexpected fields in node input', {
itemIndex: i,
description: `The input field '${key}' doesn't match any column in the Sheet. You can ignore this by changing the 'Handling extra data' field, which you can find under 'Options'.`,
});
}
};

const addNewColumn = (key: string) => {
if (!columnNames.includes(key)) {
newColumns.add(key);
}
};

for (let i = 0; i < items.length; i++) {
if (dataMode === 'nothing') continue;

Expand All @@ -291,22 +306,11 @@ export async function execute(
data.push(items[i].json);
}
if (handlingExtraDataOption === 'error' && columnsToMatchOn[0] !== 'row_number') {
Object.keys(items[i].json).forEach((key) => {
if (!columnNames.includes(key)) {
throw new NodeOperationError(this.getNode(), 'Unexpected fields in node input', {
itemIndex: i,
description: `The input field '${key}' doesn't match any column in the Sheet. You can ignore this by changing the 'Handling extra data' field, which you can find under 'Options'.`,
});
}
});
Object.keys(items[i].json).forEach((key) => errorOnUnexpectedColumn(key, i));
data.push(items[i].json);
}
if (handlingExtraDataOption === 'insertInNewColumn' && columnsToMatchOn[0] !== 'row_number') {
Object.keys(items[i].json).forEach((key) => {
if (!columnNames.includes(key)) {
newColumns.add(key);
}
});
Object.keys(items[i].json).forEach(addNewColumn);
data.push(items[i].json);
}
} else {
Expand All @@ -323,6 +327,7 @@ export async function execute(
"At least one value has to be added under 'Values to Send'",
);
}
// eslint-disable-next-line @typescript-eslint/no-loop-func
const fields = valuesToSend.reduce((acc, entry) => {
if (entry.column === 'newColumn') {
const columnName = entry.columnName as string;
Expand Down Expand Up @@ -361,12 +366,15 @@ export async function execute(
}

if (newColumns.size) {
const newColumnNames = columnNames.concat([...newColumns]);
await sheet.updateRows(
sheetName,
[columnNames.concat([...newColumns])],
[newColumnNames],
(options.cellFormat as ValueInputOption) || cellFormatDefault(nodeVersion),
headerRow + 1,
);
columnNames = newColumnNames;
newColumns.clear();
}

let preparedData;
Expand Down

0 comments on commit ab6a9bb

Please sign in to comment.