fix(Google Sheets Node): Tweaks (#7357)

Github issue / Community forum post (link here to close automatically):
This commit is contained in:
Michael Kret 2023-10-17 18:41:30 +03:00 committed by GitHub
parent a2d2e3dda7
commit d8531a53b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 414 additions and 304 deletions

View file

@ -11,7 +11,7 @@ export class GoogleSheets extends VersionedNodeType {
name: 'googleSheets', name: 'googleSheets',
icon: 'file:googleSheets.svg', icon: 'file:googleSheets.svg',
group: ['input', 'output'], group: ['input', 'output'],
defaultVersion: 4, defaultVersion: 4.1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Read, update and write data to Google Sheets', description: 'Read, update and write data to Google Sheets',
}; };
@ -21,6 +21,7 @@ export class GoogleSheets extends VersionedNodeType {
2: new GoogleSheetsV1(baseDescription), 2: new GoogleSheetsV1(baseDescription),
3: new GoogleSheetsV2(baseDescription), 3: new GoogleSheetsV2(baseDescription),
4: new GoogleSheetsV2(baseDescription), 4: new GoogleSheetsV2(baseDescription),
4.1: new GoogleSheetsV2(baseDescription),
}; };
super(nodeVersions, baseDescription); super(nodeVersions, baseDescription);

View file

@ -1,7 +1,12 @@
import type { IExecuteFunctions, IDataObject, INodeExecutionData } from 'n8n-workflow'; import type { IExecuteFunctions, IDataObject, INodeExecutionData } from 'n8n-workflow';
import type { SheetProperties, ValueInputOption } from '../../helpers/GoogleSheets.types'; import type { SheetProperties, ValueInputOption } from '../../helpers/GoogleSheets.types';
import type { GoogleSheet } from '../../helpers/GoogleSheet'; import type { GoogleSheet } from '../../helpers/GoogleSheet';
import { autoMapInputData, mapFields, untilSheetSelected } from '../../helpers/GoogleSheets.utils'; import {
autoMapInputData,
cellFormatDefault,
mapFields,
untilSheetSelected,
} from '../../helpers/GoogleSheets.utils';
import { cellFormat, handlingExtraData } from './commonDescription'; import { cellFormat, handlingExtraData } from './commonDescription';
export const description: SheetProperties = [ export const description: SheetProperties = [
@ -131,7 +136,7 @@ export const description: SheetProperties = [
show: { show: {
resource: ['sheet'], resource: ['sheet'],
operation: ['append'], operation: ['append'],
'@version': [4], '@version': [4, 4.1],
}, },
hide: { hide: {
...untilSheetSelected, ...untilSheetSelected,
@ -154,7 +159,7 @@ export const description: SheetProperties = [
}, },
}, },
options: [ options: [
...cellFormat, cellFormat,
{ {
displayName: 'Data Location on Sheet', displayName: 'Data Location on Sheet',
name: 'locationDefine', name: 'locationDefine',
@ -181,7 +186,11 @@ export const description: SheetProperties = [
}, },
], ],
}, },
handlingExtraData,
{
...handlingExtraData, ...handlingExtraData,
displayOptions: { show: { '/columns.mappingMode': ['autoMapInputData'] } },
},
], ],
}, },
]; ];
@ -227,7 +236,7 @@ export async function execute(
setData, setData,
sheetName, sheetName,
headerRow, headerRow,
(options.cellFormat as ValueInputOption) || 'RAW', (options.cellFormat as ValueInputOption) || cellFormatDefault(nodeVersion),
false, false,
); );

View file

@ -7,7 +7,7 @@ import type {
} from '../../helpers/GoogleSheets.types'; } from '../../helpers/GoogleSheets.types';
import { NodeOperationError } from 'n8n-workflow'; import { NodeOperationError } from 'n8n-workflow';
import type { GoogleSheet } from '../../helpers/GoogleSheet'; import type { GoogleSheet } from '../../helpers/GoogleSheet';
import { untilSheetSelected } from '../../helpers/GoogleSheets.utils'; import { cellFormatDefault, untilSheetSelected } from '../../helpers/GoogleSheets.utils';
import { cellFormat, handlingExtraData, locationDefine } from './commonDescription'; import { cellFormat, handlingExtraData, locationDefine } from './commonDescription';
export const description: SheetProperties = [ export const description: SheetProperties = [
@ -172,7 +172,7 @@ export const description: SheetProperties = [
show: { show: {
resource: ['sheet'], resource: ['sheet'],
operation: ['appendOrUpdate'], operation: ['appendOrUpdate'],
'@version': [4], '@version': [4, 4.1],
}, },
hide: { hide: {
...untilSheetSelected, ...untilSheetSelected,
@ -194,7 +194,15 @@ export const description: SheetProperties = [
...untilSheetSelected, ...untilSheetSelected,
}, },
}, },
options: [...cellFormat, ...locationDefine, ...handlingExtraData], options: [
cellFormat,
locationDefine,
handlingExtraData,
{
...handlingExtraData,
displayOptions: { show: { '/columns.mappingMode': ['autoMapInputData'] } },
},
],
}, },
]; ];
@ -205,9 +213,16 @@ export async function execute(
sheetId: string, sheetId: string,
): Promise<INodeExecutionData[]> { ): Promise<INodeExecutionData[]> {
const items = this.getInputData(); const items = this.getInputData();
const valueInputMode = this.getNodeParameter('options.cellFormat', 0, 'RAW') as ValueInputOption; const nodeVersion = this.getNode().typeVersion;
const range = `${sheetName}!A:Z`; const range = `${sheetName}!A:Z`;
const valueInputMode = this.getNodeParameter(
'options.cellFormat',
0,
cellFormatDefault(nodeVersion),
) as ValueInputOption;
const options = this.getNodeParameter('options', 0, {}); const options = this.getNodeParameter('options', 0, {});
const valueRenderMode = (options.valueRenderMode || 'UNFORMATTED_VALUE') as ValueRenderOption; const valueRenderMode = (options.valueRenderMode || 'UNFORMATTED_VALUE') as ValueRenderOption;
@ -238,7 +253,7 @@ export async function execute(
} }
columnNames = sheetData[headerRow]; columnNames = sheetData[headerRow];
const nodeVersion = this.getNode().typeVersion;
const newColumns = new Set<string>(); const newColumns = new Set<string>();
const columnsToMatchOn: string[] = const columnsToMatchOn: string[] =
@ -346,7 +361,7 @@ export async function execute(
await sheet.updateRows( await sheet.updateRows(
sheetName, sheetName,
[columnNames.concat([...newColumns])], [columnNames.concat([...newColumns])],
(options.cellFormat as ValueInputOption) || 'RAW', (options.cellFormat as ValueInputOption) || cellFormatDefault(nodeVersion),
headerRow + 1, headerRow + 1,
); );
} }

View file

@ -1,7 +1,6 @@
import type { INodeProperties } from 'n8n-workflow'; import type { INodeProperties } from 'n8n-workflow';
export const dataLocationOnSheet: INodeProperties[] = [ export const dataLocationOnSheet: INodeProperties = {
{
displayName: 'Data Location on Sheet', displayName: 'Data Location on Sheet',
name: 'dataLocationOnSheet', name: 'dataLocationOnSheet',
type: 'fixedCollection', type: 'fixedCollection',
@ -106,11 +105,9 @@ export const dataLocationOnSheet: INodeProperties[] = [
], ],
}, },
], ],
}, };
];
export const locationDefine: INodeProperties[] = [ export const locationDefine: INodeProperties = {
{
displayName: 'Data Location on Sheet', displayName: 'Data Location on Sheet',
name: 'locationDefine', name: 'locationDefine',
type: 'fixedCollection', type: 'fixedCollection',
@ -146,11 +143,9 @@ export const locationDefine: INodeProperties[] = [
], ],
}, },
], ],
}, };
];
export const outputFormatting: INodeProperties[] = [ export const outputFormatting: INodeProperties = {
{
displayName: 'Output Formatting', displayName: 'Output Formatting',
name: 'outputFormatting', name: 'outputFormatting',
type: 'fixedCollection', type: 'fixedCollection',
@ -209,35 +204,31 @@ export const outputFormatting: INodeProperties[] = [
], ],
}, },
], ],
}, };
];
export const cellFormat: INodeProperties[] = [ export const cellFormat: INodeProperties = {
{
displayName: 'Cell Format', displayName: 'Cell Format',
name: 'cellFormat', name: 'cellFormat',
type: 'options', type: 'options',
options: [ options: [
{
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
name: 'Let n8n format',
value: 'RAW',
description: 'Cells have the same types as the input data',
},
{ {
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased // eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
name: 'Let Google Sheets format', name: 'Let Google Sheets format',
value: 'USER_ENTERED', value: 'USER_ENTERED',
description: 'Cells are styled as if you typed the values into Google Sheets directly', description: 'Cells are styled as if you typed the values into Google Sheets directly',
}, },
],
default: 'RAW',
description: 'Determines how data should be interpreted',
},
];
export const handlingExtraData: INodeProperties[] = [
{ {
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
name: 'Let n8n format',
value: 'RAW',
description: 'Cells have the same types as the input data',
},
],
default: 'USER_ENTERED',
description: 'Determines how data should be interpreted',
};
export const handlingExtraData: INodeProperties = {
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased // eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
displayName: 'Handling extra fields in input', displayName: 'Handling extra fields in input',
name: 'handlingExtraData', name: 'handlingExtraData',
@ -266,5 +257,4 @@ export const handlingExtraData: INodeProperties[] = [
}, },
default: 'insertInNewColumn', default: 'insertInNewColumn',
description: "What do to with fields that don't match any columns in the Google Sheet", description: "What do to with fields that don't match any columns in the Google Sheet",
}, };
];

View file

@ -80,8 +80,8 @@ export const description: SheetProperties = [
}, },
}, },
options: [ options: [
...dataLocationOnSheet, dataLocationOnSheet,
...outputFormatting, outputFormatting,
{ {
displayName: 'When Filter Has Multiple Matches', displayName: 'When Filter Has Multiple Matches',
name: 'returnAllMatches', name: 'returnAllMatches',

View file

@ -7,7 +7,7 @@ import type {
} from '../../helpers/GoogleSheets.types'; } from '../../helpers/GoogleSheets.types';
import { NodeOperationError } from 'n8n-workflow'; import { NodeOperationError } from 'n8n-workflow';
import type { GoogleSheet } from '../../helpers/GoogleSheet'; import type { GoogleSheet } from '../../helpers/GoogleSheet';
import { untilSheetSelected } from '../../helpers/GoogleSheets.utils'; import { cellFormatDefault, untilSheetSelected } from '../../helpers/GoogleSheets.utils';
import { cellFormat, handlingExtraData, locationDefine } from './commonDescription'; import { cellFormat, handlingExtraData, locationDefine } from './commonDescription';
export const description: SheetProperties = [ export const description: SheetProperties = [
@ -172,7 +172,7 @@ export const description: SheetProperties = [
show: { show: {
resource: ['sheet'], resource: ['sheet'],
operation: ['update'], operation: ['update'],
'@version': [4], '@version': [4, 4.1],
}, },
hide: { hide: {
...untilSheetSelected, ...untilSheetSelected,
@ -194,7 +194,15 @@ export const description: SheetProperties = [
...untilSheetSelected, ...untilSheetSelected,
}, },
}, },
options: [...cellFormat, ...locationDefine, ...handlingExtraData], options: [
cellFormat,
locationDefine,
handlingExtraData,
{
...handlingExtraData,
displayOptions: { show: { '/columns.mappingMode': ['autoMapInputData'] } },
},
],
}, },
]; ];
@ -204,17 +212,22 @@ export async function execute(
sheetName: string, sheetName: string,
): Promise<INodeExecutionData[]> { ): Promise<INodeExecutionData[]> {
const items = this.getInputData(); const items = this.getInputData();
const valueInputMode = this.getNodeParameter('options.cellFormat', 0, 'RAW') as ValueInputOption; const nodeVersion = this.getNode().typeVersion;
const range = `${sheetName}!A:Z`; const range = `${sheetName}!A:Z`;
const valueInputMode = this.getNodeParameter(
'options.cellFormat',
0,
cellFormatDefault(nodeVersion),
) as ValueInputOption;
const options = this.getNodeParameter('options', 0, {}); const options = this.getNodeParameter('options', 0, {});
const valueRenderMode = (options.valueRenderMode || 'UNFORMATTED_VALUE') as ValueRenderOption; const valueRenderMode = (options.valueRenderMode || 'UNFORMATTED_VALUE') as ValueRenderOption;
const locationDefineOptions = (options.locationDefine as IDataObject)?.values as IDataObject; const locationDefineOptions = (options.locationDefine as IDataObject)?.values as IDataObject;
const nodeVersion = this.getNode().typeVersion;
let headerRow = 0; let headerRow = 0;
let firstDataRow = 1; let firstDataRow = 1;
@ -254,6 +267,7 @@ export async function execute(
// TODO: Add support for multiple columns to match on in the next overhaul // TODO: Add support for multiple columns to match on in the next overhaul
const keyIndex = columnNames.indexOf(columnsToMatchOn[0]); const keyIndex = columnNames.indexOf(columnsToMatchOn[0]);
//not used when updating row
const columnValues = await sheet.getColumnValues( const columnValues = await sheet.getColumnValues(
range, range,
keyIndex, keyIndex,
@ -276,7 +290,7 @@ export async function execute(
if (handlingExtraDataOption === 'ignoreIt') { if (handlingExtraDataOption === 'ignoreIt') {
data.push(items[i].json); data.push(items[i].json);
} }
if (handlingExtraDataOption === 'error') { if (handlingExtraDataOption === 'error' && columnsToMatchOn[0] !== 'row_number') {
Object.keys(items[i].json).forEach((key) => { Object.keys(items[i].json).forEach((key) => {
if (!columnNames.includes(key)) { if (!columnNames.includes(key)) {
throw new NodeOperationError(this.getNode(), 'Unexpected fields in node input', { throw new NodeOperationError(this.getNode(), 'Unexpected fields in node input', {
@ -287,7 +301,7 @@ export async function execute(
}); });
data.push(items[i].json); data.push(items[i].json);
} }
if (handlingExtraDataOption === 'insertInNewColumn') { if (handlingExtraDataOption === 'insertInNewColumn' && columnsToMatchOn[0] !== 'row_number') {
Object.keys(items[i].json).forEach((key) => { Object.keys(items[i].json).forEach((key) => {
if (!columnNames.includes(key)) { if (!columnNames.includes(key)) {
newColumns.add(key); newColumns.add(key);
@ -350,12 +364,18 @@ export async function execute(
await sheet.updateRows( await sheet.updateRows(
sheetName, sheetName,
[columnNames.concat([...newColumns])], [columnNames.concat([...newColumns])],
(options.cellFormat as ValueInputOption) || 'RAW', (options.cellFormat as ValueInputOption) || cellFormatDefault(nodeVersion),
headerRow + 1, headerRow + 1,
); );
} }
const preparedData = await sheet.prepareDataForUpdateOrUpsert( let preparedData;
if (columnsToMatchOn[0] === 'row_number') {
preparedData = sheet.prepareDataForUpdatingByRowNumber(data, range, [
columnNames.concat([...newColumns]),
]);
} else {
preparedData = await sheet.prepareDataForUpdateOrUpsert(
data, data,
columnsToMatchOn[0], columnsToMatchOn[0],
range, range,
@ -366,6 +386,7 @@ export async function execute(
[columnNames.concat([...newColumns])], [columnNames.concat([...newColumns])],
columnValues, columnValues,
); );
}
updateData.push(...preparedData.updateData); updateData.push(...preparedData.updateData);
} }

View file

@ -9,7 +9,7 @@ export const versionDescription: INodeTypeDescription = {
name: 'googleSheets', name: 'googleSheets',
icon: 'file:googleSheets.svg', icon: 'file:googleSheets.svg',
group: ['input', 'output'], group: ['input', 'output'],
version: [3, 4], version: [3, 4, 4.1],
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Read, update and write data to Google Sheets', description: 'Read, update and write data to Google Sheets',
defaults: { defaults: {

View file

@ -5,9 +5,9 @@ import type {
IPollFunctions, IPollFunctions,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow'; import { NodeOperationError } from 'n8n-workflow';
import { apiRequest } from '../transport';
import { utils as xlsxUtils } from 'xlsx'; import { utils as xlsxUtils } from 'xlsx';
import get from 'lodash/get'; import get from 'lodash/get';
import { apiRequest } from '../transport';
import type { import type {
ILookupValues, ILookupValues,
ISheetUpdateData, ISheetUpdateData,
@ -553,6 +553,53 @@ export class GoogleSheet {
return { updateData, appendData }; return { updateData, appendData };
} }
/**
* Updates data in a sheet
*
* @param {IDataObject[]} inputData Data to update Sheet with
* @param {string} range The range to look for data
* @param {number} dataStartRowIndex Index of the first row which contains data
* @param {string[][]} columnNamesList The column names to use
* @returns {Promise<string[][]>}
* @memberof GoogleSheet
*/
prepareDataForUpdatingByRowNumber(
inputData: IDataObject[],
range: string,
columnNamesList: string[][],
) {
const decodedRange = this.getDecodedSheetRange(range);
const columnNames = columnNamesList[0];
const updateData: ISheetUpdateData[] = [];
for (const item of inputData) {
const updateRowIndex = item.row_number as number;
for (const name of columnNames) {
if (name === 'row_number') continue;
if (item[name] === undefined || item[name] === null) continue;
const columnToUpdate = this.getColumnWithOffset(
decodedRange.start?.column || 'A',
columnNames.indexOf(name),
);
let updateValue = item[name] as string;
if (typeof updateValue === 'object') {
try {
updateValue = JSON.stringify(updateValue);
} catch (error) {}
}
updateData.push({
range: `${decodedRange.name}!${columnToUpdate}${updateRowIndex}`,
values: [[updateValue]],
});
}
}
return { updateData };
}
/** /**
* Looks for a specific value in a column and if it gets found it returns the whole row * Looks for a specific value in a column and if it gets found it returns the whole row
* *

View file

@ -315,3 +315,10 @@ export function sortLoadOptions(data: INodePropertyOptions[] | INodeListSearchIt
return returnData; return returnData;
} }
export function cellFormatDefault(nodeVersion: number) {
if (nodeVersion < 4.1) {
return 'RAW';
}
return 'USER_ENTERED';
}

View file

@ -1,6 +1,11 @@
import type { IDataObject, ILoadOptionsFunctions, ResourceMapperFields } from 'n8n-workflow'; import type {
IDataObject,
ILoadOptionsFunctions,
ResourceMapperField,
ResourceMapperFields,
} from 'n8n-workflow';
import { GoogleSheet } from '../helpers/GoogleSheet'; import { GoogleSheet } from '../helpers/GoogleSheet';
import type { ResourceLocator } from '../helpers/GoogleSheets.types'; import { ROW_NUMBER, type ResourceLocator } from '../helpers/GoogleSheets.types';
import { getSpreadsheetId } from '../helpers/GoogleSheets.utils'; import { getSpreadsheetId } from '../helpers/GoogleSheets.utils';
export async function getMappingColumns( export async function getMappingColumns(
@ -22,8 +27,8 @@ export async function getMappingColumns(
const sheetData = await sheet.getData(`${sheetName}!1:1`, 'FORMATTED_VALUE'); const sheetData = await sheet.getData(`${sheetName}!1:1`, 'FORMATTED_VALUE');
const columns = sheet.testFilter(sheetData || [], 0, 0).filter((col) => col !== ''); const columns = sheet.testFilter(sheetData || [], 0, 0).filter((col) => col !== '');
const columnData: ResourceMapperFields = {
fields: columns.map((col) => ({ const fields: ResourceMapperField[] = columns.map((col) => ({
id: col, id: col,
displayName: col, displayName: col,
required: false, required: false,
@ -31,8 +36,23 @@ export async function getMappingColumns(
display: true, display: true,
type: 'string', type: 'string',
canBeUsedToMatch: true, canBeUsedToMatch: true,
})), }));
};
return columnData; const operation = this.getNodeParameter('operation', 0) as string;
if (operation === 'update') {
fields.push({
id: ROW_NUMBER,
displayName: ROW_NUMBER,
required: false,
defaultMatch: false,
display: true,
type: 'string',
canBeUsedToMatch: true,
readOnly: true,
removed: true,
});
}
return { fields };
} }