mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 12:44:07 -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,
|
||||
{
|
||||
...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,270 +1,260 @@
|
|||
import type { INodeProperties } from 'n8n-workflow';
|
||||
|
||||
export const dataLocationOnSheet: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Data Location on Sheet',
|
||||
name: 'dataLocationOnSheet',
|
||||
type: 'fixedCollection',
|
||||
placeholder: 'Select Range',
|
||||
default: { values: { rangeDefinition: 'detectAutomatically' } },
|
||||
options: [
|
||||
{
|
||||
displayName: 'Values',
|
||||
name: 'values',
|
||||
values: [
|
||||
{
|
||||
displayName: 'Range Definition',
|
||||
name: 'rangeDefinition',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Detect Automatically',
|
||||
value: 'detectAutomatically',
|
||||
description: 'Automatically detect the data range',
|
||||
},
|
||||
{
|
||||
name: 'Specify Range (A1 Notation)',
|
||||
value: 'specifyRangeA1',
|
||||
description: 'Manually specify the data range',
|
||||
},
|
||||
{
|
||||
name: 'Specify Range (Rows)',
|
||||
value: 'specifyRange',
|
||||
description: 'Manually specify the data range',
|
||||
},
|
||||
],
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Read Rows Until',
|
||||
name: 'readRowsUntil',
|
||||
type: 'options',
|
||||
default: 'lastRowInSheet',
|
||||
options: [
|
||||
{
|
||||
name: 'First Empty Row',
|
||||
value: 'firstEmptyRow',
|
||||
},
|
||||
{
|
||||
name: 'Last Row In Sheet',
|
||||
value: 'lastRowInSheet',
|
||||
},
|
||||
],
|
||||
displayOptions: {
|
||||
show: {
|
||||
rangeDefinition: ['detectAutomatically'],
|
||||
},
|
||||
export const dataLocationOnSheet: INodeProperties = {
|
||||
displayName: 'Data Location on Sheet',
|
||||
name: 'dataLocationOnSheet',
|
||||
type: 'fixedCollection',
|
||||
placeholder: 'Select Range',
|
||||
default: { values: { rangeDefinition: 'detectAutomatically' } },
|
||||
options: [
|
||||
{
|
||||
displayName: 'Values',
|
||||
name: 'values',
|
||||
values: [
|
||||
{
|
||||
displayName: 'Range Definition',
|
||||
name: 'rangeDefinition',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Detect Automatically',
|
||||
value: 'detectAutomatically',
|
||||
description: 'Automatically detect the data range',
|
||||
},
|
||||
{
|
||||
name: 'Specify Range (A1 Notation)',
|
||||
value: 'specifyRangeA1',
|
||||
description: 'Manually specify the data range',
|
||||
},
|
||||
{
|
||||
name: 'Specify Range (Rows)',
|
||||
value: 'specifyRange',
|
||||
description: 'Manually specify the data range',
|
||||
},
|
||||
],
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Read Rows Until',
|
||||
name: 'readRowsUntil',
|
||||
type: 'options',
|
||||
default: 'lastRowInSheet',
|
||||
options: [
|
||||
{
|
||||
name: 'First Empty Row',
|
||||
value: 'firstEmptyRow',
|
||||
},
|
||||
{
|
||||
name: 'Last Row In Sheet',
|
||||
value: 'lastRowInSheet',
|
||||
},
|
||||
],
|
||||
displayOptions: {
|
||||
show: {
|
||||
rangeDefinition: ['detectAutomatically'],
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Header Row',
|
||||
name: 'headerRow',
|
||||
type: 'number',
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
},
|
||||
default: 1,
|
||||
description: "Index is relative to the set 'Range', first row index is 1",
|
||||
hint: 'Index of the row which contains the column names',
|
||||
displayOptions: {
|
||||
show: {
|
||||
rangeDefinition: ['specifyRange'],
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Header Row',
|
||||
name: 'headerRow',
|
||||
type: 'number',
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
},
|
||||
default: 1,
|
||||
description: "Index is relative to the set 'Range', first row index is 1",
|
||||
hint: 'Index of the row which contains the column names',
|
||||
displayOptions: {
|
||||
show: {
|
||||
rangeDefinition: ['specifyRange'],
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'First Data Row',
|
||||
name: 'firstDataRow',
|
||||
type: 'number',
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
},
|
||||
default: 2,
|
||||
description: "Index is relative to the set 'Range', first row index is 1",
|
||||
hint: 'Index of first row which contains the actual data',
|
||||
displayOptions: {
|
||||
show: {
|
||||
rangeDefinition: ['specifyRange'],
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'First Data Row',
|
||||
name: 'firstDataRow',
|
||||
type: 'number',
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
},
|
||||
default: 2,
|
||||
description: "Index is relative to the set 'Range', first row index is 1",
|
||||
hint: 'Index of first row which contains the actual data',
|
||||
displayOptions: {
|
||||
show: {
|
||||
rangeDefinition: ['specifyRange'],
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Range',
|
||||
name: 'range',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'A:Z',
|
||||
description:
|
||||
'The table range to read from or to append data to. See the Google <a href="https://developers.google.com/sheets/api/guides/values#writing">documentation</a> for the details.',
|
||||
hint: 'You can specify both the rows and the columns, e.g. C4:E7',
|
||||
displayOptions: {
|
||||
show: {
|
||||
rangeDefinition: ['specifyRangeA1'],
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Range',
|
||||
name: 'range',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'A:Z',
|
||||
description:
|
||||
'The table range to read from or to append data to. See the Google <a href="https://developers.google.com/sheets/api/guides/values#writing">documentation</a> for the details.',
|
||||
hint: 'You can specify both the rows and the columns, e.g. C4:E7',
|
||||
displayOptions: {
|
||||
show: {
|
||||
rangeDefinition: ['specifyRangeA1'],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
export const locationDefine: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Data Location on Sheet',
|
||||
name: 'locationDefine',
|
||||
type: 'fixedCollection',
|
||||
placeholder: 'Select Range',
|
||||
default: { values: {} },
|
||||
options: [
|
||||
{
|
||||
displayName: 'Values',
|
||||
name: 'values',
|
||||
values: [
|
||||
{
|
||||
displayName: 'Header Row',
|
||||
name: 'headerRow',
|
||||
type: 'number',
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
},
|
||||
default: 1,
|
||||
description: "Index is relative to the set 'Range', first row index is 1",
|
||||
hint: 'Index of the row which contains the column names',
|
||||
},
|
||||
{
|
||||
displayName: 'First Data Row',
|
||||
name: 'firstDataRow',
|
||||
type: 'number',
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
},
|
||||
default: 2,
|
||||
description: "Index is relative to the set 'Range', first row index is 1",
|
||||
hint: 'Index of first row which contains the actual data',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
export const outputFormatting: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Output Formatting',
|
||||
name: 'outputFormatting',
|
||||
type: 'fixedCollection',
|
||||
placeholder: 'Add Formatting',
|
||||
default: { values: { general: 'UNFORMATTED_VALUE', date: 'FORMATTED_STRING' } },
|
||||
options: [
|
||||
{
|
||||
displayName: 'Values',
|
||||
name: 'values',
|
||||
values: [
|
||||
{
|
||||
displayName: 'General Formatting',
|
||||
name: 'general',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
|
||||
name: 'Values (unformatted)',
|
||||
value: 'UNFORMATTED_VALUE',
|
||||
description:
|
||||
'Numbers stay as numbers, but any currency signs or special formatting is lost',
|
||||
},
|
||||
{
|
||||
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
|
||||
name: 'Values (formatted)',
|
||||
value: 'FORMATTED_VALUE',
|
||||
description:
|
||||
'Numbers are turned to text, and displayed as in Google Sheets (e.g. with commas or currency signs)',
|
||||
},
|
||||
{
|
||||
name: 'Formulas',
|
||||
value: 'FORMULA',
|
||||
},
|
||||
],
|
||||
default: '',
|
||||
description: 'Determines how values should be rendered in the output',
|
||||
},
|
||||
{
|
||||
displayName: 'Date Formatting',
|
||||
name: 'date',
|
||||
type: 'options',
|
||||
default: '',
|
||||
options: [
|
||||
{
|
||||
name: 'Formatted Text',
|
||||
value: 'FORMATTED_STRING',
|
||||
description: "As displayed in Google Sheets, e.g. '01/01/2022'",
|
||||
},
|
||||
{
|
||||
name: 'Serial Number',
|
||||
value: 'SERIAL_NUMBER',
|
||||
description: 'A number representing the number of days since Dec 30, 1899',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
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
|
||||
displayName: 'Handling extra fields in input',
|
||||
name: 'handlingExtraData',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Insert in New Column(s)',
|
||||
value: 'insertInNewColumn',
|
||||
description: 'Create a new column for extra data',
|
||||
},
|
||||
{
|
||||
name: 'Ignore Them',
|
||||
value: 'ignoreIt',
|
||||
description: 'Ignore extra data',
|
||||
},
|
||||
{
|
||||
name: 'Error',
|
||||
value: 'error',
|
||||
description: 'Throw an error',
|
||||
},
|
||||
],
|
||||
displayOptions: {
|
||||
show: {
|
||||
'/dataMode': ['autoMapInputData'],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
export const locationDefine: INodeProperties = {
|
||||
displayName: 'Data Location on Sheet',
|
||||
name: 'locationDefine',
|
||||
type: 'fixedCollection',
|
||||
placeholder: 'Select Range',
|
||||
default: { values: {} },
|
||||
options: [
|
||||
{
|
||||
displayName: 'Values',
|
||||
name: 'values',
|
||||
values: [
|
||||
{
|
||||
displayName: 'Header Row',
|
||||
name: 'headerRow',
|
||||
type: 'number',
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
},
|
||||
default: 1,
|
||||
description: "Index is relative to the set 'Range', first row index is 1",
|
||||
hint: 'Index of the row which contains the column names',
|
||||
},
|
||||
{
|
||||
displayName: 'First Data Row',
|
||||
name: 'firstDataRow',
|
||||
type: 'number',
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
},
|
||||
default: 2,
|
||||
description: "Index is relative to the set 'Range', first row index is 1",
|
||||
hint: 'Index of first row which contains the actual data',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
export const outputFormatting: INodeProperties = {
|
||||
displayName: 'Output Formatting',
|
||||
name: 'outputFormatting',
|
||||
type: 'fixedCollection',
|
||||
placeholder: 'Add Formatting',
|
||||
default: { values: { general: 'UNFORMATTED_VALUE', date: 'FORMATTED_STRING' } },
|
||||
options: [
|
||||
{
|
||||
displayName: 'Values',
|
||||
name: 'values',
|
||||
values: [
|
||||
{
|
||||
displayName: 'General Formatting',
|
||||
name: 'general',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
|
||||
name: 'Values (unformatted)',
|
||||
value: 'UNFORMATTED_VALUE',
|
||||
description:
|
||||
'Numbers stay as numbers, but any currency signs or special formatting is lost',
|
||||
},
|
||||
{
|
||||
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
|
||||
name: 'Values (formatted)',
|
||||
value: 'FORMATTED_VALUE',
|
||||
description:
|
||||
'Numbers are turned to text, and displayed as in Google Sheets (e.g. with commas or currency signs)',
|
||||
},
|
||||
{
|
||||
name: 'Formulas',
|
||||
value: 'FORMULA',
|
||||
},
|
||||
],
|
||||
default: '',
|
||||
description: 'Determines how values should be rendered in the output',
|
||||
},
|
||||
{
|
||||
displayName: 'Date Formatting',
|
||||
name: 'date',
|
||||
type: 'options',
|
||||
default: '',
|
||||
options: [
|
||||
{
|
||||
name: 'Formatted Text',
|
||||
value: 'FORMATTED_STRING',
|
||||
description: "As displayed in Google Sheets, e.g. '01/01/2022'",
|
||||
},
|
||||
{
|
||||
name: 'Serial Number',
|
||||
value: 'SERIAL_NUMBER',
|
||||
description: 'A number representing the number of days since Dec 30, 1899',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
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 Google Sheets format',
|
||||
value: 'USER_ENTERED',
|
||||
description: 'Cells are styled as if you typed the values into Google Sheets directly',
|
||||
},
|
||||
{
|
||||
// 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',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Insert in New Column(s)',
|
||||
value: 'insertInNewColumn',
|
||||
description: 'Create a new column for extra data',
|
||||
},
|
||||
{
|
||||
name: 'Ignore Them',
|
||||
value: 'ignoreIt',
|
||||
description: 'Ignore extra data',
|
||||
},
|
||||
{
|
||||
name: 'Error',
|
||||
value: 'error',
|
||||
description: 'Throw an error',
|
||||
},
|
||||
],
|
||||
displayOptions: {
|
||||
show: {
|
||||
'/dataMode': ['autoMapInputData'],
|
||||
},
|
||||
default: 'insertInNewColumn',
|
||||
description: "What do to with fields that don't match any columns in the Google Sheet",
|
||||
},
|
||||
];
|
||||
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,22 +364,29 @@ 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(
|
||||
data,
|
||||
columnsToMatchOn[0],
|
||||
range,
|
||||
headerRow,
|
||||
firstDataRow,
|
||||
valueRenderMode,
|
||||
false,
|
||||
[columnNames.concat([...newColumns])],
|
||||
columnValues,
|
||||
);
|
||||
let preparedData;
|
||||
if (columnsToMatchOn[0] === 'row_number') {
|
||||
preparedData = sheet.prepareDataForUpdatingByRowNumber(data, range, [
|
||||
columnNames.concat([...newColumns]),
|
||||
]);
|
||||
} else {
|
||||
preparedData = await sheet.prepareDataForUpdateOrUpsert(
|
||||
data,
|
||||
columnsToMatchOn[0],
|
||||
range,
|
||||
headerRow,
|
||||
firstDataRow,
|
||||
valueRenderMode,
|
||||
false,
|
||||
[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,17 +27,32 @@ 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) => ({
|
||||
id: col,
|
||||
displayName: col,
|
||||
|
||||
const fields: ResourceMapperField[] = columns.map((col) => ({
|
||||
id: col,
|
||||
displayName: col,
|
||||
required: false,
|
||||
defaultMatch: col === 'id',
|
||||
display: true,
|
||||
type: 'string',
|
||||
canBeUsedToMatch: true,
|
||||
}));
|
||||
|
||||
const operation = this.getNodeParameter('operation', 0) as string;
|
||||
|
||||
if (operation === 'update') {
|
||||
fields.push({
|
||||
id: ROW_NUMBER,
|
||||
displayName: ROW_NUMBER,
|
||||
required: false,
|
||||
defaultMatch: col === 'id',
|
||||
defaultMatch: false,
|
||||
display: true,
|
||||
type: 'string',
|
||||
canBeUsedToMatch: true,
|
||||
})),
|
||||
};
|
||||
readOnly: true,
|
||||
removed: true,
|
||||
});
|
||||
}
|
||||
|
||||
return columnData;
|
||||
return { fields };
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue