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,
|
ValueRenderOption,
|
||||||
} from './GoogleSheet';
|
} from './GoogleSheet';
|
||||||
|
|
||||||
|
import {
|
||||||
|
googleApiRequest,
|
||||||
|
} from './GenericFunctions';
|
||||||
|
|
||||||
export class GoogleSheets implements INodeType {
|
export class GoogleSheets implements INodeType {
|
||||||
description: INodeTypeDescription = {
|
description: INodeTypeDescription = {
|
||||||
displayName: 'Google Sheets ',
|
displayName: 'Google Sheets ',
|
||||||
|
@ -28,6 +32,7 @@ export class GoogleSheets implements INodeType {
|
||||||
icon: 'file:googlesheets.png',
|
icon: 'file:googlesheets.png',
|
||||||
group: ['input', 'output'],
|
group: ['input', 'output'],
|
||||||
version: 1,
|
version: 1,
|
||||||
|
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: {
|
||||||
name: 'Google Sheets',
|
name: 'Google Sheets',
|
||||||
|
@ -76,10 +81,35 @@ export class GoogleSheets implements INodeType {
|
||||||
],
|
],
|
||||||
default: 'serviceAccount',
|
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',
|
displayName: 'Operation',
|
||||||
name: 'operation',
|
name: 'operation',
|
||||||
type: 'options',
|
type: 'options',
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
resource: [
|
||||||
|
'sheet',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
options: [
|
options: [
|
||||||
{
|
{
|
||||||
name: 'Append',
|
name: 'Append',
|
||||||
|
@ -123,6 +153,13 @@ export class GoogleSheets implements INodeType {
|
||||||
displayName: 'Sheet ID',
|
displayName: 'Sheet ID',
|
||||||
name: 'sheetId',
|
name: 'sheetId',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
resource: [
|
||||||
|
'sheet',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
default: '',
|
default: '',
|
||||||
required: true,
|
required: true,
|
||||||
description: 'The ID of the Google Sheet.<br />Found as part of the sheet URL https://docs.google.com/spreadsheets/d/{ID}/',
|
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',
|
name: 'range',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
resource: [
|
||||||
|
'sheet',
|
||||||
|
],
|
||||||
|
},
|
||||||
hide: {
|
hide: {
|
||||||
operation: [
|
operation: [
|
||||||
'delete',
|
'delete',
|
||||||
|
@ -158,6 +200,9 @@ export class GoogleSheets implements INodeType {
|
||||||
},
|
},
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
|
resource: [
|
||||||
|
'sheet',
|
||||||
|
],
|
||||||
operation: [
|
operation: [
|
||||||
'delete',
|
'delete',
|
||||||
],
|
],
|
||||||
|
@ -254,6 +299,9 @@ export class GoogleSheets implements INodeType {
|
||||||
type: 'boolean',
|
type: 'boolean',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
|
resource: [
|
||||||
|
'sheet',
|
||||||
|
],
|
||||||
operation: [
|
operation: [
|
||||||
'read',
|
'read',
|
||||||
],
|
],
|
||||||
|
@ -269,6 +317,9 @@ export class GoogleSheets implements INodeType {
|
||||||
default: 'data',
|
default: 'data',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
|
resource: [
|
||||||
|
'sheet',
|
||||||
|
],
|
||||||
operation: [
|
operation: [
|
||||||
'read',
|
'read',
|
||||||
],
|
],
|
||||||
|
@ -289,6 +340,9 @@ export class GoogleSheets implements INodeType {
|
||||||
type: 'boolean',
|
type: 'boolean',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
|
resource: [
|
||||||
|
'sheet',
|
||||||
|
],
|
||||||
operation: [
|
operation: [
|
||||||
'update',
|
'update',
|
||||||
],
|
],
|
||||||
|
@ -304,6 +358,9 @@ export class GoogleSheets implements INodeType {
|
||||||
default: 'data',
|
default: 'data',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
|
resource: [
|
||||||
|
'sheet',
|
||||||
|
],
|
||||||
operation: [
|
operation: [
|
||||||
'update',
|
'update',
|
||||||
],
|
],
|
||||||
|
@ -327,6 +384,11 @@ export class GoogleSheets implements INodeType {
|
||||||
},
|
},
|
||||||
default: 1,
|
default: 1,
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
resource: [
|
||||||
|
'sheet',
|
||||||
|
],
|
||||||
|
},
|
||||||
hide: {
|
hide: {
|
||||||
operation: [
|
operation: [
|
||||||
'append',
|
'append',
|
||||||
|
@ -352,6 +414,11 @@ export class GoogleSheets implements INodeType {
|
||||||
minValue: 0,
|
minValue: 0,
|
||||||
},
|
},
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
|
show: {
|
||||||
|
resource: [
|
||||||
|
'sheet',
|
||||||
|
],
|
||||||
|
},
|
||||||
hide: {
|
hide: {
|
||||||
operation: [
|
operation: [
|
||||||
'clear',
|
'clear',
|
||||||
|
@ -379,6 +446,9 @@ export class GoogleSheets implements INodeType {
|
||||||
required: true,
|
required: true,
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
|
resource: [
|
||||||
|
'sheet',
|
||||||
|
],
|
||||||
operation: [
|
operation: [
|
||||||
'lookup',
|
'lookup',
|
||||||
],
|
],
|
||||||
|
@ -394,6 +464,9 @@ export class GoogleSheets implements INodeType {
|
||||||
placeholder: 'frank@example.com',
|
placeholder: 'frank@example.com',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
|
resource: [
|
||||||
|
'sheet',
|
||||||
|
],
|
||||||
operation: [
|
operation: [
|
||||||
'lookup',
|
'lookup',
|
||||||
],
|
],
|
||||||
|
@ -412,6 +485,9 @@ export class GoogleSheets implements INodeType {
|
||||||
default: 'id',
|
default: 'id',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
|
resource: [
|
||||||
|
'sheet',
|
||||||
|
],
|
||||||
operation: [
|
operation: [
|
||||||
'update',
|
'update',
|
||||||
],
|
],
|
||||||
|
@ -431,6 +507,9 @@ export class GoogleSheets implements INodeType {
|
||||||
default: {},
|
default: {},
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
|
resource: [
|
||||||
|
'sheet',
|
||||||
|
],
|
||||||
operation: [
|
operation: [
|
||||||
'append',
|
'append',
|
||||||
'lookup',
|
'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,188 +819,247 @@ export class GoogleSheets implements INodeType {
|
||||||
|
|
||||||
|
|
||||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||||
const spreadsheetId = this.getNodeParameter('sheetId', 0) as string;
|
|
||||||
|
|
||||||
const sheet = new GoogleSheet(spreadsheetId, this);
|
|
||||||
|
|
||||||
const operation = this.getNodeParameter('operation', 0) as string;
|
const operation = this.getNodeParameter('operation', 0) as string;
|
||||||
|
const resource = this.getNodeParameter('resource', 0) as string;
|
||||||
|
|
||||||
let range = '';
|
if (resource === 'sheet') {
|
||||||
if (operation !== 'delete') {
|
|
||||||
range = this.getNodeParameter('range', 0) as string;
|
|
||||||
}
|
|
||||||
|
|
||||||
const options = this.getNodeParameter('options', 0, {}) as IDataObject;
|
const spreadsheetId = this.getNodeParameter('sheetId', 0) as string;
|
||||||
|
|
||||||
const valueInputMode = (options.valueInputMode || 'RAW') as ValueInputOption;
|
const sheet = new GoogleSheet(spreadsheetId, this);
|
||||||
const valueRenderMode = (options.valueRenderMode || 'UNFORMATTED_VALUE') as ValueRenderOption;
|
|
||||||
|
|
||||||
if (operation === 'append') {
|
let range = '';
|
||||||
// ----------------------------------
|
if (operation !== 'delete') {
|
||||||
// append
|
range = this.getNodeParameter('range', 0) as string;
|
||||||
// ----------------------------------
|
|
||||||
const keyRow = parseInt(this.getNodeParameter('keyRow', 0) as string, 10);
|
|
||||||
|
|
||||||
const items = this.getInputData();
|
|
||||||
|
|
||||||
const setData: IDataObject[] = [];
|
|
||||||
items.forEach((item) => {
|
|
||||||
setData.push(item.json);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Convert data into array format
|
|
||||||
const data = await sheet.appendSheetData(setData, sheet.encodeRange(range), keyRow, valueInputMode);
|
|
||||||
|
|
||||||
// TODO: Should add this data somewhere
|
|
||||||
// TODO: Should have something like add metadata which does not get passed through
|
|
||||||
|
|
||||||
return this.prepareOutputData(items);
|
|
||||||
} else if (operation === 'clear') {
|
|
||||||
// ----------------------------------
|
|
||||||
// clear
|
|
||||||
// ----------------------------------
|
|
||||||
|
|
||||||
await sheet.clearData(sheet.encodeRange(range));
|
|
||||||
|
|
||||||
const items = this.getInputData();
|
|
||||||
return this.prepareOutputData(items);
|
|
||||||
} else if (operation === 'delete') {
|
|
||||||
// ----------------------------------
|
|
||||||
// delete
|
|
||||||
// ----------------------------------
|
|
||||||
|
|
||||||
const requests: IDataObject[] = [];
|
|
||||||
|
|
||||||
const toDelete = this.getNodeParameter('toDelete', 0) as IToDelete;
|
|
||||||
|
|
||||||
const deletePropertyToDimensions: IDataObject = {
|
|
||||||
'columns': 'COLUMNS',
|
|
||||||
'rows': 'ROWS',
|
|
||||||
};
|
|
||||||
|
|
||||||
for (const propertyName of Object.keys(deletePropertyToDimensions)) {
|
|
||||||
if (toDelete[propertyName] !== undefined) {
|
|
||||||
toDelete[propertyName]!.forEach(range => {
|
|
||||||
requests.push({
|
|
||||||
deleteDimension: {
|
|
||||||
range: {
|
|
||||||
sheetId: range.sheetId,
|
|
||||||
dimension: deletePropertyToDimensions[propertyName] as string,
|
|
||||||
startIndex: range.startIndex,
|
|
||||||
endIndex: parseInt(range.startIndex.toString(), 10) + parseInt(range.amount.toString(), 10),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await sheet.spreadsheetBatchUpdate(requests);
|
const options = this.getNodeParameter('options', 0, {}) as IDataObject;
|
||||||
|
|
||||||
const items = this.getInputData();
|
const valueInputMode = (options.valueInputMode || 'RAW') as ValueInputOption;
|
||||||
return this.prepareOutputData(items);
|
const valueRenderMode = (options.valueRenderMode || 'UNFORMATTED_VALUE') as ValueRenderOption;
|
||||||
} else if (operation === 'lookup') {
|
|
||||||
// ----------------------------------
|
|
||||||
// lookup
|
|
||||||
// ----------------------------------
|
|
||||||
|
|
||||||
const sheetData = await sheet.getData(sheet.encodeRange(range), valueRenderMode);
|
if (operation === 'append') {
|
||||||
|
// ----------------------------------
|
||||||
if (sheetData === undefined) {
|
// append
|
||||||
return [];
|
// ----------------------------------
|
||||||
}
|
|
||||||
|
|
||||||
const dataStartRow = parseInt(this.getNodeParameter('dataStartRow', 0) as string, 10);
|
|
||||||
const keyRow = parseInt(this.getNodeParameter('keyRow', 0) as string, 10);
|
|
||||||
|
|
||||||
const items = this.getInputData();
|
|
||||||
|
|
||||||
const lookupValues: ILookupValues[] = [];
|
|
||||||
for (let i = 0; i < items.length; i++) {
|
|
||||||
lookupValues.push({
|
|
||||||
lookupColumn: this.getNodeParameter('lookupColumn', i) as string,
|
|
||||||
lookupValue: this.getNodeParameter('lookupValue', i) as string,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let returnData = await sheet.lookupValues(sheetData, keyRow, dataStartRow, lookupValues, options.returnAllMatches as boolean | undefined);
|
|
||||||
|
|
||||||
if (returnData.length === 0 && options.continue && options.returnAllMatches) {
|
|
||||||
returnData = [{}];
|
|
||||||
} else if (returnData.length === 1 && Object.keys(returnData[0]).length === 0 && !options.continue && !options.returnAllMatches) {
|
|
||||||
returnData = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
return [this.helpers.returnJsonArray(returnData)];
|
|
||||||
} else if (operation === 'read') {
|
|
||||||
// ----------------------------------
|
|
||||||
// read
|
|
||||||
// ----------------------------------
|
|
||||||
|
|
||||||
const rawData = this.getNodeParameter('rawData', 0) as boolean;
|
|
||||||
|
|
||||||
const sheetData = await sheet.getData(sheet.encodeRange(range), valueRenderMode);
|
|
||||||
|
|
||||||
let returnData: IDataObject[];
|
|
||||||
if (!sheetData) {
|
|
||||||
returnData = [];
|
|
||||||
} else if (rawData === true) {
|
|
||||||
const dataProperty = this.getNodeParameter('dataProperty', 0) as string;
|
|
||||||
returnData = [
|
|
||||||
{
|
|
||||||
[dataProperty]: sheetData,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
} else {
|
|
||||||
const dataStartRow = parseInt(this.getNodeParameter('dataStartRow', 0) as string, 10);
|
|
||||||
const keyRow = parseInt(this.getNodeParameter('keyRow', 0) as string, 10);
|
const keyRow = parseInt(this.getNodeParameter('keyRow', 0) as string, 10);
|
||||||
|
|
||||||
returnData = sheet.structureArrayDataByColumn(sheetData, keyRow, dataStartRow);
|
const items = this.getInputData();
|
||||||
}
|
|
||||||
|
|
||||||
if (returnData.length === 0 && options.continue) {
|
|
||||||
returnData = [{}];
|
|
||||||
}
|
|
||||||
|
|
||||||
return [this.helpers.returnJsonArray(returnData)];
|
|
||||||
} else if (operation === 'update') {
|
|
||||||
// ----------------------------------
|
|
||||||
// update
|
|
||||||
// ----------------------------------
|
|
||||||
|
|
||||||
const rawData = this.getNodeParameter('rawData', 0) as boolean;
|
|
||||||
|
|
||||||
const items = this.getInputData();
|
|
||||||
|
|
||||||
if (rawData === true) {
|
|
||||||
const dataProperty = this.getNodeParameter('dataProperty', 0) as string;
|
|
||||||
|
|
||||||
const updateData: ISheetUpdateData[] = [];
|
|
||||||
for (let i = 0; i < items.length; i++) {
|
|
||||||
updateData.push({
|
|
||||||
range,
|
|
||||||
values: items[i].json[dataProperty] as string[][],
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await sheet.batchUpdate(updateData, valueInputMode);
|
|
||||||
} else {
|
|
||||||
const keyName = this.getNodeParameter('key', 0) as string;
|
|
||||||
const keyRow = parseInt(this.getNodeParameter('keyRow', 0) as string, 10);
|
|
||||||
const dataStartRow = parseInt(this.getNodeParameter('dataStartRow', 0) as string, 10);
|
|
||||||
|
|
||||||
const setData: IDataObject[] = [];
|
const setData: IDataObject[] = [];
|
||||||
items.forEach((item) => {
|
items.forEach((item) => {
|
||||||
setData.push(item.json);
|
setData.push(item.json);
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await sheet.updateSheetData(setData, keyName, range, keyRow, dataStartRow, valueInputMode, valueRenderMode);
|
// Convert data into array format
|
||||||
|
const data = await sheet.appendSheetData(setData, sheet.encodeRange(range), keyRow, valueInputMode);
|
||||||
|
|
||||||
|
// TODO: Should add this data somewhere
|
||||||
|
// TODO: Should have something like add metadata which does not get passed through
|
||||||
|
|
||||||
|
return this.prepareOutputData(items);
|
||||||
|
} else if (operation === 'clear') {
|
||||||
|
// ----------------------------------
|
||||||
|
// clear
|
||||||
|
// ----------------------------------
|
||||||
|
|
||||||
|
await sheet.clearData(sheet.encodeRange(range));
|
||||||
|
|
||||||
|
const items = this.getInputData();
|
||||||
|
return this.prepareOutputData(items);
|
||||||
|
} else if (operation === 'delete') {
|
||||||
|
// ----------------------------------
|
||||||
|
// delete
|
||||||
|
// ----------------------------------
|
||||||
|
|
||||||
|
const requests: IDataObject[] = [];
|
||||||
|
|
||||||
|
const toDelete = this.getNodeParameter('toDelete', 0) as IToDelete;
|
||||||
|
|
||||||
|
const deletePropertyToDimensions: IDataObject = {
|
||||||
|
'columns': 'COLUMNS',
|
||||||
|
'rows': 'ROWS',
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const propertyName of Object.keys(deletePropertyToDimensions)) {
|
||||||
|
if (toDelete[propertyName] !== undefined) {
|
||||||
|
toDelete[propertyName]!.forEach(range => {
|
||||||
|
requests.push({
|
||||||
|
deleteDimension: {
|
||||||
|
range: {
|
||||||
|
sheetId: range.sheetId,
|
||||||
|
dimension: deletePropertyToDimensions[propertyName] as string,
|
||||||
|
startIndex: range.startIndex,
|
||||||
|
endIndex: parseInt(range.startIndex.toString(), 10) + parseInt(range.amount.toString(), 10),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await sheet.spreadsheetBatchUpdate(requests);
|
||||||
|
|
||||||
|
const items = this.getInputData();
|
||||||
|
return this.prepareOutputData(items);
|
||||||
|
} else if (operation === 'lookup') {
|
||||||
|
// ----------------------------------
|
||||||
|
// lookup
|
||||||
|
// ----------------------------------
|
||||||
|
|
||||||
|
const sheetData = await sheet.getData(sheet.encodeRange(range), valueRenderMode);
|
||||||
|
|
||||||
|
if (sheetData === undefined) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const dataStartRow = parseInt(this.getNodeParameter('dataStartRow', 0) as string, 10);
|
||||||
|
const keyRow = parseInt(this.getNodeParameter('keyRow', 0) as string, 10);
|
||||||
|
|
||||||
|
const items = this.getInputData();
|
||||||
|
|
||||||
|
const lookupValues: ILookupValues[] = [];
|
||||||
|
for (let i = 0; i < items.length; i++) {
|
||||||
|
lookupValues.push({
|
||||||
|
lookupColumn: this.getNodeParameter('lookupColumn', i) as string,
|
||||||
|
lookupValue: this.getNodeParameter('lookupValue', i) as string,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let returnData = await sheet.lookupValues(sheetData, keyRow, dataStartRow, lookupValues, options.returnAllMatches as boolean | undefined);
|
||||||
|
|
||||||
|
if (returnData.length === 0 && options.continue && options.returnAllMatches) {
|
||||||
|
returnData = [{}];
|
||||||
|
} else if (returnData.length === 1 && Object.keys(returnData[0]).length === 0 && !options.continue && !options.returnAllMatches) {
|
||||||
|
returnData = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [this.helpers.returnJsonArray(returnData)];
|
||||||
|
} else if (operation === 'read') {
|
||||||
|
// ----------------------------------
|
||||||
|
// read
|
||||||
|
// ----------------------------------
|
||||||
|
|
||||||
|
const rawData = this.getNodeParameter('rawData', 0) as boolean;
|
||||||
|
|
||||||
|
const sheetData = await sheet.getData(sheet.encodeRange(range), valueRenderMode);
|
||||||
|
|
||||||
|
let returnData: IDataObject[];
|
||||||
|
if (!sheetData) {
|
||||||
|
returnData = [];
|
||||||
|
} else if (rawData === true) {
|
||||||
|
const dataProperty = this.getNodeParameter('dataProperty', 0) as string;
|
||||||
|
returnData = [
|
||||||
|
{
|
||||||
|
[dataProperty]: sheetData,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
const dataStartRow = parseInt(this.getNodeParameter('dataStartRow', 0) as string, 10);
|
||||||
|
const keyRow = parseInt(this.getNodeParameter('keyRow', 0) as string, 10);
|
||||||
|
|
||||||
|
returnData = sheet.structureArrayDataByColumn(sheetData, keyRow, dataStartRow);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (returnData.length === 0 && options.continue) {
|
||||||
|
returnData = [{}];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [this.helpers.returnJsonArray(returnData)];
|
||||||
|
} else if (operation === 'update') {
|
||||||
|
// ----------------------------------
|
||||||
|
// update
|
||||||
|
// ----------------------------------
|
||||||
|
|
||||||
|
const rawData = this.getNodeParameter('rawData', 0) as boolean;
|
||||||
|
|
||||||
|
const items = this.getInputData();
|
||||||
|
|
||||||
|
if (rawData === true) {
|
||||||
|
const dataProperty = this.getNodeParameter('dataProperty', 0) as string;
|
||||||
|
|
||||||
|
const updateData: ISheetUpdateData[] = [];
|
||||||
|
for (let i = 0; i < items.length; i++) {
|
||||||
|
updateData.push({
|
||||||
|
range,
|
||||||
|
values: items[i].json[dataProperty] as string[][],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await sheet.batchUpdate(updateData, valueInputMode);
|
||||||
|
} else {
|
||||||
|
const keyName = this.getNodeParameter('key', 0) as string;
|
||||||
|
const keyRow = parseInt(this.getNodeParameter('keyRow', 0) as string, 10);
|
||||||
|
const dataStartRow = parseInt(this.getNodeParameter('dataStartRow', 0) as string, 10);
|
||||||
|
|
||||||
|
const setData: IDataObject[] = [];
|
||||||
|
items.forEach((item) => {
|
||||||
|
setData.push(item.json);
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await sheet.updateSheetData(setData, keyName, range, keyRow, dataStartRow, valueInputMode, valueRenderMode);
|
||||||
|
}
|
||||||
|
// TODO: Should add this data somewhere
|
||||||
|
// TODO: Should have something like add metadata which does not get passed through
|
||||||
|
|
||||||
|
|
||||||
|
return this.prepareOutputData(items);
|
||||||
}
|
}
|
||||||
// TODO: Should add this data somewhere
|
|
||||||
// TODO: Should have something like add metadata which does not get passed through
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
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 [];
|
return [];
|
||||||
|
|
Loading…
Reference in a new issue