mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-12 21:37:32 -08:00
fix(Google Sheets Node): Tweaks (#7357)
Github issue / Community forum post (link here to close automatically):
This commit is contained in:
parent
a2d2e3dda7
commit
d8531a53b9
|
@ -11,7 +11,7 @@ export class GoogleSheets extends VersionedNodeType {
|
|||
name: 'googleSheets',
|
||||
icon: 'file:googleSheets.svg',
|
||||
group: ['input', 'output'],
|
||||
defaultVersion: 4,
|
||||
defaultVersion: 4.1,
|
||||
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
||||
description: 'Read, update and write data to Google Sheets',
|
||||
};
|
||||
|
@ -21,6 +21,7 @@ export class GoogleSheets extends VersionedNodeType {
|
|||
2: new GoogleSheetsV1(baseDescription),
|
||||
3: new GoogleSheetsV2(baseDescription),
|
||||
4: new GoogleSheetsV2(baseDescription),
|
||||
4.1: new GoogleSheetsV2(baseDescription),
|
||||
};
|
||||
|
||||
super(nodeVersions, baseDescription);
|
||||
|
|
|
@ -1,7 +1,12 @@
|
|||
import type { IExecuteFunctions, IDataObject, INodeExecutionData } from 'n8n-workflow';
|
||||
import type { SheetProperties, ValueInputOption } from '../../helpers/GoogleSheets.types';
|
||||
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';
|
||||
|
||||
export const description: SheetProperties = [
|
||||
|
@ -131,7 +136,7 @@ export const description: SheetProperties = [
|
|||
show: {
|
||||
resource: ['sheet'],
|
||||
operation: ['append'],
|
||||
'@version': [4],
|
||||
'@version': [4, 4.1],
|
||||
},
|
||||
hide: {
|
||||
...untilSheetSelected,
|
||||
|
@ -154,7 +159,7 @@ export const description: SheetProperties = [
|
|||
},
|
||||
},
|
||||
options: [
|
||||
...cellFormat,
|
||||
cellFormat,
|
||||
{
|
||||
displayName: 'Data Location on Sheet',
|
||||
name: 'locationDefine',
|
||||
|
@ -181,7 +186,11 @@ export const description: SheetProperties = [
|
|||
},
|
||||
],
|
||||
},
|
||||
handlingExtraData,
|
||||
{
|
||||
...handlingExtraData,
|
||||
displayOptions: { show: { '/columns.mappingMode': ['autoMapInputData'] } },
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
@ -227,7 +236,7 @@ export async function execute(
|
|||
setData,
|
||||
sheetName,
|
||||
headerRow,
|
||||
(options.cellFormat as ValueInputOption) || 'RAW',
|
||||
(options.cellFormat as ValueInputOption) || cellFormatDefault(nodeVersion),
|
||||
false,
|
||||
);
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ import type {
|
|||
} from '../../helpers/GoogleSheets.types';
|
||||
import { NodeOperationError } from 'n8n-workflow';
|
||||
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';
|
||||
|
||||
export const description: SheetProperties = [
|
||||
|
@ -172,7 +172,7 @@ export const description: SheetProperties = [
|
|||
show: {
|
||||
resource: ['sheet'],
|
||||
operation: ['appendOrUpdate'],
|
||||
'@version': [4],
|
||||
'@version': [4, 4.1],
|
||||
},
|
||||
hide: {
|
||||
...untilSheetSelected,
|
||||
|
@ -194,7 +194,15 @@ export const description: SheetProperties = [
|
|||
...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,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
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 valueInputMode = this.getNodeParameter(
|
||||
'options.cellFormat',
|
||||
0,
|
||||
cellFormatDefault(nodeVersion),
|
||||
) as ValueInputOption;
|
||||
|
||||
const options = this.getNodeParameter('options', 0, {});
|
||||
|
||||
const valueRenderMode = (options.valueRenderMode || 'UNFORMATTED_VALUE') as ValueRenderOption;
|
||||
|
@ -238,7 +253,7 @@ export async function execute(
|
|||
}
|
||||
|
||||
columnNames = sheetData[headerRow];
|
||||
const nodeVersion = this.getNode().typeVersion;
|
||||
|
||||
const newColumns = new Set<string>();
|
||||
|
||||
const columnsToMatchOn: string[] =
|
||||
|
@ -346,7 +361,7 @@ export async function execute(
|
|||
await sheet.updateRows(
|
||||
sheetName,
|
||||
[columnNames.concat([...newColumns])],
|
||||
(options.cellFormat as ValueInputOption) || 'RAW',
|
||||
(options.cellFormat as ValueInputOption) || cellFormatDefault(nodeVersion),
|
||||
headerRow + 1,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import type { INodeProperties } from 'n8n-workflow';
|
||||
|
||||
export const dataLocationOnSheet: INodeProperties[] = [
|
||||
{
|
||||
export const dataLocationOnSheet: INodeProperties = {
|
||||
displayName: 'Data Location on Sheet',
|
||||
name: 'dataLocationOnSheet',
|
||||
type: 'fixedCollection',
|
||||
|
@ -106,11 +105,9 @@ export const dataLocationOnSheet: INodeProperties[] = [
|
|||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
};
|
||||
|
||||
export const locationDefine: INodeProperties[] = [
|
||||
{
|
||||
export const locationDefine: INodeProperties = {
|
||||
displayName: 'Data Location on Sheet',
|
||||
name: 'locationDefine',
|
||||
type: 'fixedCollection',
|
||||
|
@ -146,11 +143,9 @@ export const locationDefine: INodeProperties[] = [
|
|||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
};
|
||||
|
||||
export const outputFormatting: INodeProperties[] = [
|
||||
{
|
||||
export const outputFormatting: INodeProperties = {
|
||||
displayName: 'Output Formatting',
|
||||
name: 'outputFormatting',
|
||||
type: 'fixedCollection',
|
||||
|
@ -209,35 +204,31 @@ export const outputFormatting: INodeProperties[] = [
|
|||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
};
|
||||
|
||||
export const cellFormat: INodeProperties[] = [
|
||||
{
|
||||
export const cellFormat: INodeProperties = {
|
||||
displayName: 'Cell Format',
|
||||
name: 'cellFormat',
|
||||
type: '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
|
||||
name: 'Let Google Sheets format',
|
||||
value: 'USER_ENTERED',
|
||||
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
|
||||
displayName: 'Handling extra fields in input',
|
||||
name: 'handlingExtraData',
|
||||
|
@ -266,5 +257,4 @@ export const handlingExtraData: INodeProperties[] = [
|
|||
},
|
||||
default: 'insertInNewColumn',
|
||||
description: "What do to with fields that don't match any columns in the Google Sheet",
|
||||
},
|
||||
];
|
||||
};
|
||||
|
|
|
@ -80,8 +80,8 @@ export const description: SheetProperties = [
|
|||
},
|
||||
},
|
||||
options: [
|
||||
...dataLocationOnSheet,
|
||||
...outputFormatting,
|
||||
dataLocationOnSheet,
|
||||
outputFormatting,
|
||||
{
|
||||
displayName: 'When Filter Has Multiple Matches',
|
||||
name: 'returnAllMatches',
|
||||
|
|
|
@ -7,7 +7,7 @@ import type {
|
|||
} from '../../helpers/GoogleSheets.types';
|
||||
import { NodeOperationError } from 'n8n-workflow';
|
||||
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';
|
||||
|
||||
export const description: SheetProperties = [
|
||||
|
@ -172,7 +172,7 @@ export const description: SheetProperties = [
|
|||
show: {
|
||||
resource: ['sheet'],
|
||||
operation: ['update'],
|
||||
'@version': [4],
|
||||
'@version': [4, 4.1],
|
||||
},
|
||||
hide: {
|
||||
...untilSheetSelected,
|
||||
|
@ -194,7 +194,15 @@ export const description: SheetProperties = [
|
|||
...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,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
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 valueInputMode = this.getNodeParameter(
|
||||
'options.cellFormat',
|
||||
0,
|
||||
cellFormatDefault(nodeVersion),
|
||||
) as ValueInputOption;
|
||||
|
||||
const options = this.getNodeParameter('options', 0, {});
|
||||
|
||||
const valueRenderMode = (options.valueRenderMode || 'UNFORMATTED_VALUE') as ValueRenderOption;
|
||||
|
||||
const locationDefineOptions = (options.locationDefine as IDataObject)?.values as IDataObject;
|
||||
|
||||
const nodeVersion = this.getNode().typeVersion;
|
||||
|
||||
let headerRow = 0;
|
||||
let firstDataRow = 1;
|
||||
|
||||
|
@ -254,6 +267,7 @@ export async function execute(
|
|||
// TODO: Add support for multiple columns to match on in the next overhaul
|
||||
const keyIndex = columnNames.indexOf(columnsToMatchOn[0]);
|
||||
|
||||
//not used when updating row
|
||||
const columnValues = await sheet.getColumnValues(
|
||||
range,
|
||||
keyIndex,
|
||||
|
@ -276,7 +290,7 @@ export async function execute(
|
|||
if (handlingExtraDataOption === 'ignoreIt') {
|
||||
data.push(items[i].json);
|
||||
}
|
||||
if (handlingExtraDataOption === 'error') {
|
||||
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', {
|
||||
|
@ -287,7 +301,7 @@ export async function execute(
|
|||
});
|
||||
data.push(items[i].json);
|
||||
}
|
||||
if (handlingExtraDataOption === 'insertInNewColumn') {
|
||||
if (handlingExtraDataOption === 'insertInNewColumn' && columnsToMatchOn[0] !== 'row_number') {
|
||||
Object.keys(items[i].json).forEach((key) => {
|
||||
if (!columnNames.includes(key)) {
|
||||
newColumns.add(key);
|
||||
|
@ -350,12 +364,18 @@ export async function execute(
|
|||
await sheet.updateRows(
|
||||
sheetName,
|
||||
[columnNames.concat([...newColumns])],
|
||||
(options.cellFormat as ValueInputOption) || 'RAW',
|
||||
(options.cellFormat as ValueInputOption) || cellFormatDefault(nodeVersion),
|
||||
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,
|
||||
columnsToMatchOn[0],
|
||||
range,
|
||||
|
@ -366,6 +386,7 @@ export async function execute(
|
|||
[columnNames.concat([...newColumns])],
|
||||
columnValues,
|
||||
);
|
||||
}
|
||||
|
||||
updateData.push(...preparedData.updateData);
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ export const versionDescription: INodeTypeDescription = {
|
|||
name: 'googleSheets',
|
||||
icon: 'file:googleSheets.svg',
|
||||
group: ['input', 'output'],
|
||||
version: [3, 4],
|
||||
version: [3, 4, 4.1],
|
||||
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
||||
description: 'Read, update and write data to Google Sheets',
|
||||
defaults: {
|
||||
|
|
|
@ -5,9 +5,9 @@ import type {
|
|||
IPollFunctions,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeOperationError } from 'n8n-workflow';
|
||||
import { apiRequest } from '../transport';
|
||||
import { utils as xlsxUtils } from 'xlsx';
|
||||
import get from 'lodash/get';
|
||||
import { apiRequest } from '../transport';
|
||||
import type {
|
||||
ILookupValues,
|
||||
ISheetUpdateData,
|
||||
|
@ -553,6 +553,53 @@ export class GoogleSheet {
|
|||
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
|
||||
*
|
||||
|
|
|
@ -315,3 +315,10 @@ export function sortLoadOptions(data: INodePropertyOptions[] | INodeListSearchIt
|
|||
|
||||
return returnData;
|
||||
}
|
||||
|
||||
export function cellFormatDefault(nodeVersion: number) {
|
||||
if (nodeVersion < 4.1) {
|
||||
return 'RAW';
|
||||
}
|
||||
return 'USER_ENTERED';
|
||||
}
|
||||
|
|
|
@ -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 type { ResourceLocator } from '../helpers/GoogleSheets.types';
|
||||
import { ROW_NUMBER, type ResourceLocator } from '../helpers/GoogleSheets.types';
|
||||
import { getSpreadsheetId } from '../helpers/GoogleSheets.utils';
|
||||
|
||||
export async function getMappingColumns(
|
||||
|
@ -22,8 +27,8 @@ export async function getMappingColumns(
|
|||
const sheetData = await sheet.getData(`${sheetName}!1:1`, 'FORMATTED_VALUE');
|
||||
|
||||
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,
|
||||
displayName: col,
|
||||
required: false,
|
||||
|
@ -31,8 +36,23 @@ export async function getMappingColumns(
|
|||
display: true,
|
||||
type: 'string',
|
||||
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 };
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue