mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 20:54:07 -08:00
fix(Google Sheets Node): Append or update runs forever when without column headers (#7463)
Github issue / Community forum post (link here to close automatically): --------- Co-authored-by: Marcus <marcus@n8n.io>
This commit is contained in:
parent
689360ee06
commit
ab6a9bbac2
|
@ -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;
|
||||
|
@ -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 {
|
||||
|
@ -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;
|
||||
|
@ -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(
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue