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:
Michael Kret 2023-10-19 14:54:13 +03:00 committed by GitHub
parent 689360ee06
commit ab6a9bbac2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 28 deletions

View file

@ -280,6 +280,21 @@ export async function execute(
const updateData: ISheetUpdateData[] = []; const updateData: ISheetUpdateData[] = [];
const appendData: IDataObject[] = []; 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[] = []; const mappedValues: IDataObject[] = [];
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
if (dataMode === 'nothing') continue; if (dataMode === 'nothing') continue;
@ -292,22 +307,11 @@ export async function execute(
data.push(items[i].json); data.push(items[i].json);
} }
if (handlingExtraDataOption === 'error') { if (handlingExtraDataOption === 'error') {
Object.keys(items[i].json).forEach((key) => { Object.keys(items[i].json).forEach((key) => errorOnUnexpectedColumn(key, i));
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'.`,
});
}
});
data.push(items[i].json); data.push(items[i].json);
} }
if (handlingExtraDataOption === 'insertInNewColumn') { if (handlingExtraDataOption === 'insertInNewColumn') {
Object.keys(items[i].json).forEach((key) => { Object.keys(items[i].json).forEach(addNewColumn);
if (!columnNames.includes(key)) {
newColumns.add(key);
}
});
data.push(items[i].json); data.push(items[i].json);
} }
} else { } else {
@ -324,6 +328,7 @@ export async function execute(
"At least one value has to be added under 'Values to Send'", "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) => { const fields = valuesToSend.reduce((acc, entry) => {
if (entry.column === 'newColumn') { if (entry.column === 'newColumn') {
const columnName = entry.columnName as string; const columnName = entry.columnName as string;
@ -358,12 +363,15 @@ export async function execute(
} }
if (newColumns.size) { if (newColumns.size) {
const newColumnNames = columnNames.concat([...newColumns]);
await sheet.updateRows( await sheet.updateRows(
sheetName, sheetName,
[columnNames.concat([...newColumns])], [newColumnNames],
(options.cellFormat as ValueInputOption) || cellFormatDefault(nodeVersion), (options.cellFormat as ValueInputOption) || cellFormatDefault(nodeVersion),
headerRow + 1, headerRow + 1,
); );
columnNames = newColumnNames;
newColumns.clear();
} }
const preparedData = await sheet.prepareDataForUpdateOrUpsert( const preparedData = await sheet.prepareDataForUpdateOrUpsert(

View file

@ -280,6 +280,21 @@ export async function execute(
const mappedValues: IDataObject[] = []; 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++) { for (let i = 0; i < items.length; i++) {
if (dataMode === 'nothing') continue; if (dataMode === 'nothing') continue;
@ -291,22 +306,11 @@ export async function execute(
data.push(items[i].json); data.push(items[i].json);
} }
if (handlingExtraDataOption === 'error' && columnsToMatchOn[0] !== 'row_number') { if (handlingExtraDataOption === 'error' && columnsToMatchOn[0] !== 'row_number') {
Object.keys(items[i].json).forEach((key) => { Object.keys(items[i].json).forEach((key) => errorOnUnexpectedColumn(key, i));
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'.`,
});
}
});
data.push(items[i].json); data.push(items[i].json);
} }
if (handlingExtraDataOption === 'insertInNewColumn' && columnsToMatchOn[0] !== 'row_number') { if (handlingExtraDataOption === 'insertInNewColumn' && columnsToMatchOn[0] !== 'row_number') {
Object.keys(items[i].json).forEach((key) => { Object.keys(items[i].json).forEach(addNewColumn);
if (!columnNames.includes(key)) {
newColumns.add(key);
}
});
data.push(items[i].json); data.push(items[i].json);
} }
} else { } else {
@ -323,6 +327,7 @@ export async function execute(
"At least one value has to be added under 'Values to Send'", "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) => { const fields = valuesToSend.reduce((acc, entry) => {
if (entry.column === 'newColumn') { if (entry.column === 'newColumn') {
const columnName = entry.columnName as string; const columnName = entry.columnName as string;
@ -361,12 +366,15 @@ export async function execute(
} }
if (newColumns.size) { if (newColumns.size) {
const newColumnNames = columnNames.concat([...newColumns]);
await sheet.updateRows( await sheet.updateRows(
sheetName, sheetName,
[columnNames.concat([...newColumns])], [newColumnNames],
(options.cellFormat as ValueInputOption) || cellFormatDefault(nodeVersion), (options.cellFormat as ValueInputOption) || cellFormatDefault(nodeVersion),
headerRow + 1, headerRow + 1,
); );
columnNames = newColumnNames;
newColumns.clear();
} }
let preparedData; let preparedData;