mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-21 02:56:40 -08:00
⚡ Add create:spreadsheet operation (#1234)
* ⚡ Add create:spreadsheet operation * ⚡ Small changes * ⚡ Improvements to Google Sheets Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
parent
532503b69f
commit
d5da7865b9
|
@ -21,6 +21,10 @@ import {
|
|||
ValueRenderOption,
|
||||
} from './GoogleSheet';
|
||||
|
||||
import {
|
||||
googleApiRequest,
|
||||
} from './GenericFunctions';
|
||||
|
||||
export class GoogleSheets implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Google Sheets ',
|
||||
|
@ -28,6 +32,7 @@ export class GoogleSheets implements INodeType {
|
|||
icon: 'file:googlesheets.png',
|
||||
group: ['input', 'output'],
|
||||
version: 1,
|
||||
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
||||
description: 'Read, update and write data to Google Sheets',
|
||||
defaults: {
|
||||
name: 'Google Sheets',
|
||||
|
@ -76,10 +81,35 @@ export class GoogleSheets implements INodeType {
|
|||
],
|
||||
default: 'serviceAccount',
|
||||
},
|
||||
{
|
||||
displayName: 'Resource',
|
||||
name: 'resource',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Spreadsheet',
|
||||
value: 'spreadsheet',
|
||||
},
|
||||
{
|
||||
name: 'Sheet',
|
||||
value: 'sheet',
|
||||
},
|
||||
|
||||
],
|
||||
default: 'sheet',
|
||||
description: 'The operation to perform.',
|
||||
},
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'sheet',
|
||||
],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
name: 'Append',
|
||||
|
@ -123,6 +153,13 @@ export class GoogleSheets implements INodeType {
|
|||
displayName: 'Sheet ID',
|
||||
name: 'sheetId',
|
||||
type: 'string',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'sheet',
|
||||
],
|
||||
},
|
||||
},
|
||||
default: '',
|
||||
required: true,
|
||||
description: 'The ID of the Google Sheet.<br />Found as part of the sheet URL https://docs.google.com/spreadsheets/d/{ID}/',
|
||||
|
@ -132,6 +169,11 @@ export class GoogleSheets implements INodeType {
|
|||
name: 'range',
|
||||
type: 'string',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'sheet',
|
||||
],
|
||||
},
|
||||
hide: {
|
||||
operation: [
|
||||
'delete',
|
||||
|
@ -158,6 +200,9 @@ export class GoogleSheets implements INodeType {
|
|||
},
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'sheet',
|
||||
],
|
||||
operation: [
|
||||
'delete',
|
||||
],
|
||||
|
@ -254,6 +299,9 @@ export class GoogleSheets implements INodeType {
|
|||
type: 'boolean',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'sheet',
|
||||
],
|
||||
operation: [
|
||||
'read',
|
||||
],
|
||||
|
@ -269,6 +317,9 @@ export class GoogleSheets implements INodeType {
|
|||
default: 'data',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'sheet',
|
||||
],
|
||||
operation: [
|
||||
'read',
|
||||
],
|
||||
|
@ -289,6 +340,9 @@ export class GoogleSheets implements INodeType {
|
|||
type: 'boolean',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'sheet',
|
||||
],
|
||||
operation: [
|
||||
'update',
|
||||
],
|
||||
|
@ -304,6 +358,9 @@ export class GoogleSheets implements INodeType {
|
|||
default: 'data',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'sheet',
|
||||
],
|
||||
operation: [
|
||||
'update',
|
||||
],
|
||||
|
@ -327,6 +384,11 @@ export class GoogleSheets implements INodeType {
|
|||
},
|
||||
default: 1,
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'sheet',
|
||||
],
|
||||
},
|
||||
hide: {
|
||||
operation: [
|
||||
'append',
|
||||
|
@ -352,6 +414,11 @@ export class GoogleSheets implements INodeType {
|
|||
minValue: 0,
|
||||
},
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'sheet',
|
||||
],
|
||||
},
|
||||
hide: {
|
||||
operation: [
|
||||
'clear',
|
||||
|
@ -379,6 +446,9 @@ export class GoogleSheets implements INodeType {
|
|||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'sheet',
|
||||
],
|
||||
operation: [
|
||||
'lookup',
|
||||
],
|
||||
|
@ -394,6 +464,9 @@ export class GoogleSheets implements INodeType {
|
|||
placeholder: 'frank@example.com',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'sheet',
|
||||
],
|
||||
operation: [
|
||||
'lookup',
|
||||
],
|
||||
|
@ -412,6 +485,9 @@ export class GoogleSheets implements INodeType {
|
|||
default: 'id',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'sheet',
|
||||
],
|
||||
operation: [
|
||||
'update',
|
||||
],
|
||||
|
@ -431,6 +507,9 @@ export class GoogleSheets implements INodeType {
|
|||
default: {},
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'sheet',
|
||||
],
|
||||
operation: [
|
||||
'append',
|
||||
'lookup',
|
||||
|
@ -566,6 +645,144 @@ export class GoogleSheets implements INodeType {
|
|||
],
|
||||
},
|
||||
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'spreadsheet',
|
||||
],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
name: 'Create',
|
||||
value: 'create',
|
||||
description: 'Create a spreadsheet',
|
||||
},
|
||||
],
|
||||
default: 'create',
|
||||
description: 'The operation to perform.',
|
||||
},
|
||||
// ----------------------------------
|
||||
// spreadsheet:create
|
||||
// ----------------------------------
|
||||
{
|
||||
displayName: 'Title',
|
||||
name: 'title',
|
||||
type: 'string',
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'spreadsheet',
|
||||
],
|
||||
operation: [
|
||||
'create',
|
||||
],
|
||||
},
|
||||
},
|
||||
description: 'The title of the spreadsheet.',
|
||||
},
|
||||
{
|
||||
displayName: 'Sheets',
|
||||
name: 'sheetsUi',
|
||||
placeholder: 'Add Sheet',
|
||||
type: 'fixedCollection',
|
||||
typeOptions: {
|
||||
multipleValues: true,
|
||||
},
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
name: 'sheetValues',
|
||||
displayName: 'Sheet',
|
||||
values: [
|
||||
{
|
||||
displayName: 'Sheet Properties',
|
||||
name: 'propertiesUi',
|
||||
placeholder: 'Add Property',
|
||||
type: 'collection',
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Hidden',
|
||||
name: 'hidden',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
description: 'If the Sheet should be hidden in the UI',
|
||||
},
|
||||
{
|
||||
displayName: 'Title',
|
||||
name: 'title',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description: 'Title of the property to create',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Options',
|
||||
name: 'options',
|
||||
type: 'collection',
|
||||
placeholder: 'Add Option',
|
||||
default: {},
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'spreadsheet',
|
||||
],
|
||||
operation: [
|
||||
'create',
|
||||
],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Locale',
|
||||
name: 'locale',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'en_US',
|
||||
description: 'The locale of the spreadsheet in one of the following formats:<br /><ul><li>en (639-1)</li><li>fil (639-2 if no 639-1 format exists)</li><li>en_US (combination of ISO language an country)</li><ul>',
|
||||
},
|
||||
{
|
||||
displayName: 'Recalculation Interval',
|
||||
name: 'autoRecalc',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Default',
|
||||
value: '',
|
||||
description: 'Default value',
|
||||
},
|
||||
{
|
||||
name: 'On Change',
|
||||
value: 'ON_CHANGE',
|
||||
description: 'Volatile functions are updated on every change.',
|
||||
},
|
||||
{
|
||||
name: 'Minute',
|
||||
value: 'MINUTE',
|
||||
description: 'Volatile functions are updated on every change and every minute.',
|
||||
},
|
||||
{
|
||||
name: 'Hour',
|
||||
value: 'HOUR',
|
||||
description: ' Volatile functions are updated on every change and hourly.',
|
||||
},
|
||||
],
|
||||
default: '',
|
||||
description: 'Cell recalculation interval options.',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
|
@ -602,12 +819,16 @@ export class GoogleSheets implements INodeType {
|
|||
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
|
||||
const operation = this.getNodeParameter('operation', 0) as string;
|
||||
const resource = this.getNodeParameter('resource', 0) as string;
|
||||
|
||||
if (resource === 'sheet') {
|
||||
|
||||
const spreadsheetId = this.getNodeParameter('sheetId', 0) as string;
|
||||
|
||||
const sheet = new GoogleSheet(spreadsheetId, this);
|
||||
|
||||
const operation = this.getNodeParameter('operation', 0) as string;
|
||||
|
||||
let range = '';
|
||||
if (operation !== 'delete') {
|
||||
range = this.getNodeParameter('range', 0) as string;
|
||||
|
@ -786,6 +1007,61 @@ export class GoogleSheets implements INodeType {
|
|||
return this.prepareOutputData(items);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (resource === 'spreadsheet') {
|
||||
|
||||
const returnData: IDataObject[] = [];
|
||||
|
||||
let responseData;
|
||||
|
||||
if (operation === 'create') {
|
||||
// ----------------------------------
|
||||
// create
|
||||
// ----------------------------------
|
||||
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/create
|
||||
|
||||
for (let i = 0; i < this.getInputData().length; i++) {
|
||||
|
||||
const title = this.getNodeParameter('title', i) as string;
|
||||
const sheetsUi = this.getNodeParameter('sheetsUi', i, {}) as IDataObject;
|
||||
|
||||
const body = {
|
||||
properties: {
|
||||
title,
|
||||
autoRecalc: undefined as undefined | string,
|
||||
locale: undefined as undefined | string,
|
||||
},
|
||||
sheets: [] as IDataObject[],
|
||||
};
|
||||
|
||||
const options = this.getNodeParameter('options', i, {}) as IDataObject;
|
||||
|
||||
if (Object.keys(sheetsUi).length) {
|
||||
const data = [];
|
||||
const sheets = sheetsUi.sheetValues as IDataObject[];
|
||||
for (const sheet of sheets) {
|
||||
const properties = sheet.propertiesUi as IDataObject;
|
||||
if (properties) {
|
||||
data.push({ properties });
|
||||
}
|
||||
}
|
||||
body.sheets = data;
|
||||
}
|
||||
|
||||
body.properties!.autoRecalc = options.autoRecalc ? (options.autoRecalc as string) : undefined;
|
||||
body.properties!.locale = options.locale ? (options.locale as string) : undefined;
|
||||
|
||||
responseData = await googleApiRequest.call(this, 'POST', `/v4/spreadsheets`, body);
|
||||
|
||||
returnData.push(responseData);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return [this.helpers.returnJsonArray(returnData)];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue