Add Google Docs node (#1831)

*  Add Google Docs node

* Implement continueOnFail

* Add insert:Table and insert/delete:TableRow,TableColumn

* Lint fixes

* Fix typos, casing and enhance code readability

* Enhance code readability & apply review changes

*  Review Google Docs node

* Apply review changes

* Minor fix

* Improvements

* Clean up

* Enhance inputs descriptions

* Removed unused type fields

* Minor fix

*  Small improvements

*  Small change

* Use Document URL insead of ID and support adding content at creation

* Refactored node to make it more user friendly

* Improve get operation

* Add simple output to get operation

* Add service account

* Apply review suggestions

* Improvements

* Enable continueOnFail

*  Minor improvements

Co-authored-by: dali <servfrdali@yahoo.fr>
Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
Ricardo Espinoza 2021-06-27 05:48:24 -04:00 committed by GitHub
parent ce885e5071
commit 2ec52cf207
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 1878 additions and 0 deletions

View file

@ -0,0 +1,27 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
const scopes = [
'https://www.googleapis.com/auth/documents',
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
];
export class GoogleDocsOAuth2Api implements ICredentialType {
name = 'googleDocsOAuth2Api';
extends = [
'googleOAuth2Api',
];
displayName = 'Google Docs OAuth2 API';
documentationUrl = 'google';
properties = [
{
displayName: 'Scope',
name: 'scope',
type: 'hidden' as NodePropertyTypes,
default: scopes.join(' '),
},
];
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,142 @@
import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
IDataObject,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';
import * as moment from 'moment-timezone';
import * as jwt from 'jsonwebtoken';
export async function googleApiRequest(
this: IExecuteFunctions | ILoadOptionsFunctions,
method: string,
endpoint: string,
body: IDataObject = {},
qs?: IDataObject,
uri?: string,
) {
const authenticationMethod = this.getNodeParameter('authentication', 0, 'serviceAccount') as string;
const options: OptionsWithUri = {
headers: {
'Content-Type': 'application/json',
},
method,
body,
qs,
uri: uri || `https://docs.googleapis.com/v1${endpoint}`,
json: true,
};
if (!Object.keys(body).length) {
delete options.body;
}
try {
if (authenticationMethod === 'serviceAccount') {
const credentials = this.getCredentials('googleApi');
if (credentials === undefined) {
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
}
const { access_token } = await getAccessToken.call(this, credentials as IDataObject);
options.headers!.Authorization = `Bearer ${access_token}`;
return await this.helpers.request!(options);
} else {
//@ts-ignore
return await this.helpers.requestOAuth2.call(this, 'googleDocsOAuth2Api', options);
}
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
}
export async function googleApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, endpoint: string, body: IDataObject = {}, qs?: IDataObject, uri?: string): Promise<any> { // tslint:disable-line:no-any
const returnData: IDataObject[] = [];
let responseData;
const query: IDataObject = { ...qs };
query.maxResults = 100;
query.pageSize = 100;
do {
responseData = await googleApiRequest.call(this, method, endpoint, body, query, uri);
query.pageToken = responseData['nextPageToken'];
returnData.push.apply(returnData, responseData[propertyName]);
} while (
responseData['nextPageToken'] !== undefined &&
responseData['nextPageToken'] !== ''
);
return returnData;
}
function getAccessToken(this: IExecuteFunctions | ILoadOptionsFunctions, credentials: IDataObject): Promise<IDataObject> {
//https://developers.google.com/identity/protocols/oauth2/service-account#httprest
const scopes = [
'https://www.googleapis.com/auth/documents',
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
];
const now = moment().unix();
const signature = jwt.sign(
{
'iss': credentials.email as string,
'sub': credentials.delegatedEmail || credentials.email as string,
'scope': scopes.join(' '),
'aud': `https://oauth2.googleapis.com/token`,
'iat': now,
'exp': now + 3600,
},
credentials.privateKey as string,
{
algorithm: 'RS256',
header: {
'kid': credentials.privateKey as string,
'typ': 'JWT',
'alg': 'RS256',
},
},
);
const options: OptionsWithUri = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
method: 'POST',
form: {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: signature,
},
uri: 'https://oauth2.googleapis.com/token',
json: true,
};
return this.helpers.request!(options);
}
export const hasKeys = (obj = {}) => Object.keys(obj).length > 0;
export const extractID = (url: string) => {
const regex = new RegExp('https://docs.google.com/document/d/([a-zA-Z0-9-_]+)/');
const results = regex.exec(url);
return results ? results[1] : undefined;
};
export const upperFirst = (str: string) => {
return str[0].toUpperCase() + str.substr(1);
};

View file

@ -0,0 +1,20 @@
{
"node": "n8n-nodes-base.googleDocs",
"nodeVersion": "1.0",
"codexVersion": "1.0",
"categories": [
"Miscellaneous"
],
"resources": {
"credentialDocumentation": [
{
"url": "https://docs.n8n.io/credentials/google"
}
],
"primaryDocumentation": [
{
"url": "https://docs.n8n.io/nodes/n8n-nodes-base.googleDocs/"
}
]
}
}

View file

@ -0,0 +1,461 @@
import {
IExecuteFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
NodeApiError,
} from 'n8n-workflow';
import {
extractID,
googleApiRequest,
googleApiRequestAllItems,
hasKeys,
upperFirst,
} from './GenericFunctions';
import {
documentFields,
documentOperations,
} from './DocumentDescription';
import {
IUpdateBody,
IUpdateFields,
} from './interfaces';
export class GoogleDocs implements INodeType {
description: INodeTypeDescription = {
displayName: 'Google Docs',
name: 'googleDocs',
icon: 'file:googleDocs.svg',
group: ['input'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume Google Docs API.',
defaults: {
name: 'Google Docs',
color: '#1a73e8',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'googleApi',
required: true,
displayOptions: {
show: {
authentication: [
'serviceAccount',
],
},
},
},
{
name: 'googleDocsOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Service Account',
value: 'serviceAccount',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'serviceAccount',
},
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Document',
value: 'document',
},
],
default: 'document',
description: 'The resource to operate on.',
},
...documentOperations,
...documentFields,
],
};
methods = {
loadOptions: {
// Get all the drives to display them to user so that he can
// select them easily
async getDrives(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [
{
name: 'My Drive',
value: 'myDrive',
},
{
name: 'Shared with me',
value: 'sharedWithMe',
},
];
let drives;
try {
drives = await googleApiRequestAllItems.call(this, 'drives', 'GET', '', {}, {}, 'https://www.googleapis.com/drive/v3/drives');
} catch (error) {
throw new NodeApiError(this.getNode(), error, { message: 'Error in loading Drives' });
}
for (const drive of drives) {
returnData.push({
name: drive.name as string,
value: drive.id as string,
});
}
return returnData;
},
async getFolders(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [
{
name: '/',
value: 'default',
},
];
const driveId = this.getNodeParameter('driveId');
const qs = {
q: `mimeType = \'application/vnd.google-apps.folder\' ${driveId === 'sharedWithMe' ? 'and sharedWithMe = true' : ' and \'root\' in parents'}`,
...(driveId && driveId !== 'myDrive' && driveId !== 'sharedWithMe') ? { driveId } : {},
};
let folders;
try {
folders = await googleApiRequestAllItems.call(this, 'files', 'GET', '', {}, qs, 'https://www.googleapis.com/drive/v3/files');
} catch (error) {
throw new NodeApiError(this.getNode(), error, { message: 'Error in loading Folders' });
}
for (const folder of folders) {
returnData.push({
name: folder.name as string,
value: folder.id as string,
});
}
return returnData;
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const length = items.length;
let responseData;
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < length; i++) {
try {
if (resource === 'document') {
if (operation === 'create') {
// https://developers.google.com/docs/api/reference/rest/v1/documents/create
const folderId = this.getNodeParameter('folderId', i) as string;
const body: IDataObject = {
name: this.getNodeParameter('title', i) as string,
mimeType: 'application/vnd.google-apps.document',
...(folderId && folderId !== 'default') ? { parents: [folderId] } : {},
};
responseData = await googleApiRequest.call(this, 'POST', '', body, {}, 'https://www.googleapis.com/drive/v3/files');
} else if (operation === 'get') {
// https://developers.google.com/docs/api/reference/rest/v1/documents/get
const documentURL = this.getNodeParameter('documentURL', i) as string;
const simple = this.getNodeParameter('simple', i) as boolean;
let documentId = extractID(documentURL);
if (!documentId) {
documentId = documentURL;
}
responseData = await googleApiRequest.call(this, 'GET', `/documents/${documentId}`);
if (simple) {
const content = (responseData.body.content as IDataObject[])
.reduce((arr: string[], contentItem) => {
if (contentItem && contentItem.paragraph) {
const texts = ((contentItem.paragraph as IDataObject).elements as IDataObject[])
.map(element => {
if (element && element.textRun) {
return (element.textRun as IDataObject).content as string;
}
}) as string[];
arr = [...arr, ...texts];
}
return arr;
}, [])
.join('');
responseData = {
documentId,
content,
};
}
} else if (operation === 'update') {
// https://developers.google.com/docs/api/reference/rest/v1/documents/batchUpdate
const documentURL = this.getNodeParameter('documentURL', i) as string;
let documentId = extractID(documentURL);
const simple = this.getNodeParameter('simple', i) as boolean;
const actionsUi = this.getNodeParameter('actionsUi', i) as {
actionFields: IDataObject[]
};
const { writeControlObject } = this.getNodeParameter('updateFields', i) as IUpdateFields;
if (!documentId) {
documentId = documentURL;
}
const body = {
requests: [],
} as IUpdateBody;
if (hasKeys(writeControlObject)) {
const { control, value } = writeControlObject;
body.writeControl = {
[control]: value,
};
}
if (actionsUi) {
let requestBody: IDataObject;
actionsUi.actionFields.forEach(actionField => {
const { action, object } = actionField;
if (object === 'positionedObject') {
if (action === 'delete') {
requestBody = {
objectId: actionField.objectId,
};
}
} else if (object === 'pageBreak') {
if (action === 'insert') {
const { insertSegment, segmentId, locationChoice, index } = actionField;
requestBody = {
[locationChoice as string]: {
segmentId: (insertSegment !== 'body') ? segmentId : '',
...(locationChoice === 'location') ? { index } : {},
},
};
}
} else if (object === 'table') {
if (action === 'insert') {
const { rows, columns, insertSegment, locationChoice, segmentId, index } = actionField;
requestBody = {
rows,
columns,
[locationChoice as string]: {
segmentId: (insertSegment !== 'body') ? segmentId : '',
...(locationChoice === 'location') ? { index } : {},
},
};
}
} else if (object === 'footer') {
if (action === 'create') {
const { insertSegment, locationChoice, segmentId, index } = actionField;
requestBody = {
type: 'DEFAULT',
sectionBreakLocation: {
segmentId: (insertSegment !== 'body') ? segmentId : '',
...(locationChoice === 'location') ? { index } : {},
},
};
} else if (action === 'delete') {
requestBody = {
footerId: actionField.footerId,
};
}
} else if (object === 'header') {
if (action === 'create') {
const { insertSegment, locationChoice, segmentId, index } = actionField;
requestBody = {
type: 'DEFAULT',
sectionBreakLocation: {
segmentId: (insertSegment !== 'body') ? segmentId : '',
...(locationChoice === 'location') ? { index } : {},
},
};
} else if (action === 'delete') {
requestBody = {
headerId: actionField.headerId,
};
}
} else if (object === 'tableColumn') {
if (action === 'insert') {
const { insertPosition, rowIndex, columnIndex, insertSegment, segmentId, index } = actionField;
requestBody = {
insertRight: insertPosition,
tableCellLocation: {
rowIndex,
columnIndex,
tableStartLocation: { segmentId: (insertSegment !== 'body') ? segmentId : '', index, },
},
};
} else if (action === 'delete') {
const { rowIndex, columnIndex, insertSegment, segmentId, index } = actionField;
requestBody = {
tableCellLocation: {
rowIndex,
columnIndex,
tableStartLocation: { segmentId: (insertSegment !== 'body') ? segmentId : '', index, },
},
};
}
} else if (object === 'tableRow') {
if (action === 'insert') {
const { insertPosition, rowIndex, columnIndex, insertSegment, segmentId, index } = actionField;
requestBody = {
insertBelow: insertPosition,
tableCellLocation: {
rowIndex,
columnIndex,
tableStartLocation: { segmentId: (insertSegment !== 'body') ? segmentId : '', index, },
},
};
} else if (action === 'delete') {
const { rowIndex, columnIndex, insertSegment, segmentId, index } = actionField;
requestBody = {
tableCellLocation: {
rowIndex,
columnIndex,
tableStartLocation: { segmentId: (insertSegment !== 'body') ? segmentId : '', index, },
},
};
}
} else if (object === 'text') {
if (action === 'insert') {
const { text, locationChoice, insertSegment, segmentId, index } = actionField;
requestBody = {
text,
[locationChoice as string]: {
segmentId: (insertSegment !== 'body') ? segmentId : '',
...(locationChoice === 'location') ? { index } : {},
},
};
} else if (action === 'replaceAll') {
const { text, replaceText, matchCase } = actionField;
requestBody = {
replaceText,
containsText: { text, matchCase },
};
}
} else if (object === 'paragraphBullets') {
if (action === 'create') {
const { bulletPreset, startIndex, insertSegment, segmentId, endIndex } = actionField;
requestBody = {
bulletPreset,
range: { segmentId: (insertSegment !== 'body') ? segmentId : '', startIndex, endIndex },
};
} else if (action === 'delete') {
const { startIndex, insertSegment, segmentId, endIndex } = actionField;
requestBody = {
range: { segmentId: (insertSegment !== 'body') ? segmentId : '', startIndex, endIndex },
};
}
} else if (object === 'namedRange') {
if (action === 'create') {
const { name, insertSegment, segmentId, startIndex, endIndex } = actionField;
requestBody = {
name,
range: { segmentId: (insertSegment !== 'body') ? segmentId : '', startIndex, endIndex },
};
} else if (action === 'delete') {
const { namedRangeReference, value } = actionField;
requestBody = {
[namedRangeReference as string]: value,
};
}
}
body.requests.push({
[`${action}${upperFirst(object as string)}`]: requestBody,
});
});
}
responseData = await googleApiRequest.call(this, 'POST', `/documents/${documentId}:batchUpdate`, body);
if (simple === true) {
if (Object.keys(responseData.replies[0]).length !== 0) {
const key = Object.keys(responseData.replies[0])[0];
responseData = responseData.replies[0][key];
} else {
responseData = {};
}
}
responseData.documentId = documentId;
}
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: error.message });
continue;
}
throw error;
}
Array.isArray(responseData)
? returnData.push(...responseData)
: returnData.push(responseData);
}
return [this.helpers.returnJsonArray(returnData)];
}
}

View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-18 0 90 80" fill="#fff" fill-rule="evenodd" stroke="#000" stroke-linecap="round" stroke-linejoin="round"><use xlink:href="#A" x=".5" y=".5"/><symbol id="A" overflow="visible"><g stroke="none"><path fill="#548df6" d="M36 0l22 22v53a4.99 4.99 0 0 1-5 5H5a4.99 4.99 0 0 1-5-5V5a4.99 4.99 0 0 1 5-5z"/><path d="M14 40h30v3H14zm0 7h30v3H14zm0 8h30v3H14zm0 7h21v3H14z"/><path fill="#abd0fb" d="M36 0l22 22H41c-2.77 0-5-2.48-5-5.25z"/><path fill="#3e5bb9" d="M40.75 22L58 29.125V22z"/></g></symbol></svg>

After

Width:  |  Height:  |  Size: 591 B

View file

@ -0,0 +1,13 @@
import { IDataObject } from 'n8n-workflow';
export interface IUpdateBody extends IDataObject {
requests: IDataObject[];
writeControl?: { [key: string]: string };
}
export interface IUpdateFields {
writeControlObject: {
control: string,
value: string,
};
}

View file

@ -100,6 +100,7 @@
"dist/credentials/GoogleBooksOAuth2Api.credentials.js",
"dist/credentials/GoogleCalendarOAuth2Api.credentials.js",
"dist/credentials/GoogleContactsOAuth2Api.credentials.js",
"dist/credentials/GoogleDocsOAuth2Api.credentials.js",
"dist/credentials/GoogleCloudNaturalLanguageOAuth2Api.credentials.js",
"dist/credentials/GoogleDriveOAuth2Api.credentials.js",
"dist/credentials/GoogleFirebaseCloudFirestoreOAuth2Api.credentials.js",
@ -381,6 +382,7 @@
"dist/nodes/Google/Calendar/GoogleCalendar.node.js",
"dist/nodes/Google/CloudNaturalLanguage/GoogleCloudNaturalLanguage.node.js",
"dist/nodes/Google/Contacts/GoogleContacts.node.js",
"dist/nodes/Google/Docs/GoogleDocs.node.js",
"dist/nodes/Google/Drive/GoogleDrive.node.js",
"dist/nodes/Google/Firebase/CloudFirestore/CloudFirestore.node.js",
"dist/nodes/Google/Firebase/RealtimeDatabase/RealtimeDatabase.node.js",