2019-11-30 14:55:22 -08:00
import { sheets_v4 } from 'googleapis' ;
2019-06-23 03:35:23 -07:00
import { IExecuteFunctions } from 'n8n-core' ;
import {
IDataObject ,
2019-11-30 14:55:22 -08:00
ILoadOptionsFunctions ,
2019-06-23 03:35:23 -07:00
INodeExecutionData ,
2019-11-30 14:55:22 -08:00
INodePropertyOptions ,
2019-06-23 03:35:23 -07:00
INodeType ,
INodeTypeDescription ,
} from 'n8n-workflow' ;
2019-07-10 07:14:55 -07:00
import {
GoogleSheet ,
IGoogleAuthCredentials ,
2019-08-27 11:27:00 -07:00
ILookupValues ,
2019-07-10 07:14:55 -07:00
ISheetUpdateData ,
2019-11-30 14:55:22 -08:00
IToDelete ,
2019-10-20 12:21:55 -07:00
ValueInputOption ,
ValueRenderOption ,
2019-07-10 07:14:55 -07:00
} from './GoogleSheet' ;
2019-06-23 03:35:23 -07:00
export class GoogleSheets implements INodeType {
description : INodeTypeDescription = {
displayName : 'Google Sheets ' ,
name : 'googleSheets' ,
icon : 'file:googlesheets.png' ,
group : [ 'input' , 'output' ] ,
version : 1 ,
description : 'Read, update and write data to Google Sheets' ,
defaults : {
name : 'Google Sheets' ,
color : '#995533' ,
} ,
inputs : [ 'main' ] ,
outputs : [ 'main' ] ,
credentials : [
{
name : 'googleApi' ,
required : true ,
}
] ,
properties : [
{
displayName : 'Operation' ,
name : 'operation' ,
type : 'options' ,
options : [
{
name : 'Append' ,
value : 'append' ,
description : 'Appends the data to a Sheet' ,
} ,
2019-11-02 14:29:52 -07:00
{
name : 'Clear' ,
value : 'clear' ,
description : 'Clears data from a Sheet' ,
} ,
2019-11-30 14:55:22 -08:00
{
name : 'Delete' ,
value : 'delete' ,
description : 'Delete columns and rows from a Sheet' ,
} ,
2019-08-27 11:27:00 -07:00
{
name : 'Lookup' ,
value : 'lookup' ,
description : 'Looks for a specific column value and then returns the matching row'
} ,
2019-06-23 03:35:23 -07:00
{
name : 'Read' ,
value : 'read' ,
description : 'Reads data from a Sheet'
} ,
{
name : 'Update' ,
value : 'update' ,
description : 'Updates rows in a sheet'
} ,
] ,
default : 'read' ,
description : 'The operation to perform.' ,
} ,
// ----------------------------------
// All
// ----------------------------------
{
displayName : 'Sheet ID' ,
name : 'sheetId' ,
type : 'string' ,
default : '' ,
required : true ,
2019-11-16 13:33:03 -08:00
description : 'The ID of the Google Sheet.<br />Found as part of the sheet URL https://docs.google.com/spreadsheets/d/{ID}/' ,
2019-06-23 03:35:23 -07:00
} ,
{
displayName : 'Range' ,
name : 'range' ,
type : 'string' ,
2019-11-30 14:55:22 -08:00
displayOptions : {
hide : {
operation : [
'delete'
] ,
} ,
} ,
2019-06-23 03:35:23 -07:00
default : 'A:F' ,
required : true ,
2019-11-16 13:33:03 -08:00
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.<br />If it contains multiple sheets it can also be<br />added like this: "MySheet!A:F"' ,
2019-06-23 03:35:23 -07:00
} ,
2019-07-10 07:14:55 -07:00
2019-11-30 14:55:22 -08:00
// ----------------------------------
// Delete
// ----------------------------------
{
displayName : 'To Delete' ,
name : 'toDelete' ,
placeholder : 'Add Columns/Rows to delete' ,
description : 'Deletes colums and rows from a sheet.' ,
type : 'fixedCollection' ,
typeOptions : {
multipleValues : true ,
} ,
displayOptions : {
show : {
operation : [
'delete'
] ,
} ,
} ,
default : { } ,
options : [
{
displayName : 'Columns' ,
name : 'columns' ,
values : [
{
displayName : 'Sheet' ,
name : 'sheetId' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getSheets' ,
} ,
options : [ ] ,
default : '' ,
required : true ,
description : 'The sheet to delete columns from' ,
} ,
{
displayName : 'Start Index' ,
name : 'startIndex' ,
type : 'number' ,
typeOptions : {
minValue : 0 ,
} ,
default : 0 ,
description : 'The start index (0 based and inclusive) of column to delete.' ,
} ,
{
displayName : 'Amount' ,
name : 'amount' ,
type : 'number' ,
typeOptions : {
minValue : 1 ,
} ,
default : 1 ,
description : 'Number of columns to delete.' ,
} ,
]
} ,
{
displayName : 'Rows' ,
name : 'rows' ,
values : [
{
displayName : 'Sheet' ,
name : 'sheetId' ,
type : 'options' ,
typeOptions : {
loadOptionsMethod : 'getSheets' ,
} ,
options : [ ] ,
default : '' ,
required : true ,
description : 'The sheet to delete columns from' ,
} ,
{
displayName : 'Start Index' ,
name : 'startIndex' ,
type : 'number' ,
typeOptions : {
minValue : 0 ,
} ,
default : 0 ,
description : 'The start index (0 based and inclusive) of row to delete.' ,
} ,
{
displayName : 'Amount' ,
name : 'amount' ,
type : 'number' ,
typeOptions : {
minValue : 1 ,
} ,
default : 1 ,
description : 'Number of rows to delete.' ,
} ,
]
} ,
] ,
} ,
2019-07-10 07:14:55 -07:00
// ----------------------------------
// Read
// ----------------------------------
2019-06-23 03:35:23 -07:00
{
2019-07-10 07:14:55 -07:00
displayName : 'RAW Data' ,
name : 'rawData' ,
type : 'boolean' ,
displayOptions : {
show : {
operation : [
'read'
] ,
} ,
} ,
default : false ,
description : 'If the data should be returned RAW instead of parsed into keys according to their header.' ,
} ,
{
displayName : 'Data Property' ,
name : 'dataProperty' ,
type : 'string' ,
default : 'data' ,
displayOptions : {
show : {
operation : [
'read'
] ,
rawData : [
true
] ,
} ,
2019-06-23 03:35:23 -07:00
} ,
2019-07-10 07:14:55 -07:00
description : 'The name of the property into which to write the RAW data.' ,
} ,
// ----------------------------------
// Update
// ----------------------------------
{
displayName : 'RAW Data' ,
name : 'rawData' ,
type : 'boolean' ,
displayOptions : {
show : {
operation : [
'update'
] ,
} ,
} ,
default : false ,
description : 'If the data supplied is RAW instead of parsed into keys.' ,
} ,
{
displayName : 'Data Property' ,
name : 'dataProperty' ,
type : 'string' ,
default : 'data' ,
displayOptions : {
show : {
operation : [
'update'
] ,
rawData : [
true
] ,
} ,
} ,
description : 'The name of the property from which to read the RAW data.' ,
2019-06-23 03:35:23 -07:00
} ,
// ----------------------------------
2019-08-27 11:27:00 -07:00
// Read & Update & lookupColumn
2019-06-23 03:35:23 -07:00
// ----------------------------------
{
displayName : 'Data Start Row' ,
name : 'dataStartRow' ,
type : 'number' ,
typeOptions : {
minValue : 1 ,
} ,
default : 1 ,
displayOptions : {
hide : {
operation : [
2019-07-10 07:14:55 -07:00
'append' ,
2019-11-02 14:29:52 -07:00
'clear' ,
2019-11-30 14:55:22 -08:00
'delete' ,
2019-07-10 07:14:55 -07:00
] ,
rawData : [
true
2019-06-23 03:35:23 -07:00
] ,
} ,
} ,
description : 'Index of the first row which contains<br />the actual data and not the keys. Starts with 0.' ,
} ,
2019-07-10 07:14:55 -07:00
// ----------------------------------
// Mixed
// ----------------------------------
{
displayName : 'Key Row' ,
name : 'keyRow' ,
type : 'number' ,
typeOptions : {
minValue : 0 ,
} ,
displayOptions : {
hide : {
2019-11-02 14:29:52 -07:00
operation : [
'clear' ,
2019-11-30 14:55:22 -08:00
'delete' ,
2019-11-02 14:29:52 -07:00
] ,
2019-07-10 07:14:55 -07:00
rawData : [
true
] ,
} ,
} ,
default : 0 ,
2019-11-16 13:33:03 -08:00
description : 'Index of the row which contains the keys. Starts at 0.<br />The incoming node data is matched to the keys for assignment. The matching is case sensitve.' ,
2019-07-10 07:14:55 -07:00
} ,
2019-06-23 03:35:23 -07:00
2019-08-27 11:27:00 -07:00
// ----------------------------------
// lookup
// ----------------------------------
{
displayName : 'Lookup Column' ,
name : 'lookupColumn' ,
type : 'string' ,
default : '' ,
placeholder : 'Email' ,
required : true ,
displayOptions : {
show : {
operation : [
'lookup'
] ,
} ,
} ,
description : 'The name of the column in which to look for value.' ,
} ,
{
displayName : 'Lookup Value' ,
name : 'lookupValue' ,
type : 'string' ,
default : '' ,
placeholder : 'frank@example.com' ,
displayOptions : {
show : {
operation : [
'lookup'
] ,
} ,
} ,
description : 'The value to look for in column.' ,
} ,
2019-06-23 03:35:23 -07:00
// ----------------------------------
// Update
// ----------------------------------
{
displayName : 'Key' ,
name : 'key' ,
type : 'string' ,
default : 'id' ,
displayOptions : {
show : {
operation : [
'update'
] ,
2019-07-10 07:14:55 -07:00
rawData : [
false
] ,
2019-06-23 03:35:23 -07:00
} ,
} ,
description : 'The name of the key to identify which<br />data should be updated in the sheet.' ,
} ,
2019-10-20 12:21:55 -07:00
{
displayName : 'Options' ,
name : 'options' ,
type : 'collection' ,
placeholder : 'Add Option' ,
default : { } ,
2019-11-02 14:29:52 -07:00
displayOptions : {
show : {
operation : [
'append' ,
'lookup' ,
'read' ,
'update' ,
] ,
} ,
} ,
2019-10-20 12:21:55 -07:00
options : [
2020-03-26 14:18:11 -07:00
{
displayName : 'Continue If Response Is Empty' ,
name : 'continue' ,
type : 'boolean' ,
default : false ,
displayOptions : {
show : {
'/operation' : [
'lookup' ,
'read' ,
] ,
} ,
} ,
description : 'By default, the workflow stops executing if the lookup/read does not return values' ,
} ,
2019-10-31 12:16:20 -07:00
{
displayName : 'Return All Matches' ,
name : 'returnAllMatches' ,
type : 'boolean' ,
default : false ,
displayOptions : {
show : {
'/operation' : [
'lookup' ,
] ,
} ,
} ,
description : 'By default only the first result gets returned. If options gets set all found matches get returned.' ,
} ,
2019-10-20 12:21:55 -07:00
{
displayName : 'Value Input Mode' ,
name : 'valueInputMode' ,
type : 'options' ,
displayOptions : {
show : {
'/operation' : [
'append' ,
'update' ,
] ,
} ,
} ,
options : [
{
name : 'RAW' ,
value : 'RAW' ,
description : 'The values will not be parsed and will be stored as-is.' ,
} ,
{
name : 'User Entered' ,
value : 'USER_ENTERED' ,
description : 'The values will be parsed as if the user typed them into the UI. Numbers will stay as numbers, but strings may be converted to numbers, dates, etc. following the same rules that are applied when entering text into a cell via the Google Sheets UI.'
} ,
] ,
default : 'RAW' ,
description : 'Determines how data should be interpreted.' ,
} ,
{
displayName : 'Value Render Mode' ,
name : 'valueRenderMode' ,
type : 'options' ,
displayOptions : {
show : {
'/operation' : [
'lookup' ,
'read' ,
] ,
} ,
} ,
options : [
{
name : 'Formatted Value' ,
value : 'FORMATTED_VALUE' ,
description : 'Values will be calculated & formatted in the reply according to the cell\'s formatting.Formatting is based on the spreadsheet\'s locale, not the requesting user\'s locale.For example, if A1 is 1.23 and A2 is =A1 and formatted as currency, then A2 would return "$1.23".' ,
} ,
{
name : 'Formula' ,
value : 'FORMULA' ,
description : ' Values will not be calculated. The reply will include the formulas. For example, if A1 is 1.23 and A2 is =A1 and formatted as currency, then A2 would return "=A1".' ,
} ,
{
name : 'Unformatted Value' ,
value : 'UNFORMATTED_VALUE' ,
description : 'Values will be calculated, but not formatted in the reply. For example, if A1 is 1.23 and A2 is =A1 and formatted as currency, then A2 would return the number 1.23.'
} ,
] ,
default : 'UNFORMATTED_VALUE' ,
description : 'Determines how values should be rendered in the output.' ,
} ,
{
displayName : 'Value Render Mode' ,
name : 'valueRenderMode' ,
type : 'options' ,
displayOptions : {
show : {
'/operation' : [
'update' ,
] ,
'/rawData' : [
false
] ,
} ,
} ,
options : [
{
name : 'Formatted Value' ,
value : 'FORMATTED_VALUE' ,
description : 'Values will be calculated & formatted in the reply according to the cell\'s formatting.Formatting is based on the spreadsheet\'s locale, not the requesting user\'s locale.For example, if A1 is 1.23 and A2 is =A1 and formatted as currency, then A2 would return "$1.23".' ,
} ,
{
name : 'Formula' ,
value : 'FORMULA' ,
description : ' Values will not be calculated. The reply will include the formulas. For example, if A1 is 1.23 and A2 is =A1 and formatted as currency, then A2 would return "=A1".' ,
} ,
{
name : 'Unformatted Value' ,
value : 'UNFORMATTED_VALUE' ,
description : 'Values will be calculated, but not formatted in the reply. For example, if A1 is 1.23 and A2 is =A1 and formatted as currency, then A2 would return the number 1.23.'
} ,
] ,
default : 'UNFORMATTED_VALUE' ,
description : 'Determines how values should be rendered in the output.' ,
} ,
] ,
}
2019-06-23 03:35:23 -07:00
] ,
} ;
2019-11-30 14:55:22 -08:00
methods = {
loadOptions : {
// Get all the sheets in a Spreadsheet
async getSheets ( this : ILoadOptionsFunctions ) : Promise < INodePropertyOptions [ ] > {
const spreadsheetId = this . getCurrentNodeParameter ( 'sheetId' ) as string ;
const credentials = this . getCredentials ( 'googleApi' ) ;
if ( credentials === undefined ) {
throw new Error ( 'No credentials got returned!' ) ;
}
const googleCredentials = {
email : credentials.email ,
privateKey : credentials.privateKey ,
} as IGoogleAuthCredentials ;
const sheet = new GoogleSheet ( spreadsheetId , googleCredentials ) ;
const responseData = await sheet . spreadsheetGetSheets ( ) ;
if ( responseData === undefined ) {
throw new Error ( 'No data got returned' ) ;
}
const returnData : INodePropertyOptions [ ] = [ ] ;
for ( const sheet of responseData . sheets ! ) {
if ( sheet . properties ! . sheetType !== 'GRID' ) {
continue ;
}
returnData . push ( {
name : sheet.properties ! . title as string ,
value : sheet.properties ! . sheetId as unknown as string ,
} ) ;
}
return returnData ;
} ,
} ,
} ;
2019-06-23 03:35:23 -07:00
async execute ( this : IExecuteFunctions ) : Promise < INodeExecutionData [ ] [ ] > {
const spreadsheetId = this . getNodeParameter ( 'sheetId' , 0 ) as string ;
const credentials = this . getCredentials ( 'googleApi' ) ;
if ( credentials === undefined ) {
throw new Error ( 'No credentials got returned!' ) ;
}
const googleCredentials = {
email : credentials.email ,
privateKey : credentials.privateKey ,
} as IGoogleAuthCredentials ;
const sheet = new GoogleSheet ( spreadsheetId , googleCredentials ) ;
const operation = this . getNodeParameter ( 'operation' , 0 ) as string ;
2019-11-30 14:55:22 -08:00
let range = '' ;
if ( operation !== 'delete' ) {
range = this . getNodeParameter ( 'range' , 0 ) as string ;
}
2019-10-20 12:21:55 -07:00
const options = this . getNodeParameter ( 'options' , 0 , { } ) as IDataObject ;
const valueInputMode = ( options . valueInputMode || 'RAW' ) as ValueInputOption ;
2019-10-23 02:21:33 -07:00
const valueRenderMode = ( options . valueRenderMode || 'UNFORMATTED_VALUE' ) as ValueRenderOption ;
2019-06-23 03:35:23 -07:00
if ( operation === 'append' ) {
// ----------------------------------
// append
// ----------------------------------
2019-07-10 07:14:55 -07:00
const keyRow = this . getNodeParameter ( 'keyRow' , 0 ) as number ;
2019-06-23 03:35:23 -07:00
const items = this . getInputData ( ) ;
const setData : IDataObject [ ] = [ ] ;
items . forEach ( ( item ) = > {
setData . push ( item . json ) ;
} ) ;
// Convert data into array format
2019-10-20 12:21:55 -07:00
const data = await sheet . appendSheetData ( setData , range , keyRow , valueInputMode ) ;
2019-06-23 03:35:23 -07:00
// TODO: Should add this data somewhere
// TODO: Should have something like add metadata which does not get passed through
2019-11-02 14:29:52 -07:00
return this . prepareOutputData ( items ) ;
} else if ( operation === 'clear' ) {
// ----------------------------------
// clear
// ----------------------------------
await sheet . clearData ( range ) ;
const items = this . getInputData ( ) ;
2019-11-30 14:55:22 -08:00
return this . prepareOutputData ( items ) ;
} else if ( operation === 'delete' ) {
// ----------------------------------
// delete
// ----------------------------------
const requests : sheets_v4.Schema$Request [ ] = [ ] ;
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 : range.startIndex + range . amount ,
}
}
} ) ;
} ) ;
}
}
const data = await sheet . spreadsheetBatchUpdate ( requests ) ;
const items = this . getInputData ( ) ;
2019-06-23 03:35:23 -07:00
return this . prepareOutputData ( items ) ;
2019-08-27 11:27:00 -07:00
} else if ( operation === 'lookup' ) {
// ----------------------------------
// lookup
// ----------------------------------
2019-10-20 12:21:55 -07:00
const sheetData = await sheet . getData ( range , valueRenderMode ) ;
2019-08-27 11:27:00 -07:00
if ( sheetData === undefined ) {
return [ ] ;
}
const dataStartRow = this . getNodeParameter ( 'dataStartRow' , 0 ) as number ;
const keyRow = this . getNodeParameter ( 'keyRow' , 0 ) as number ;
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 ,
} ) ;
}
2020-03-26 14:18:11 -07:00
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 = [ ] ;
}
2019-08-27 11:27:00 -07:00
return [ this . helpers . returnJsonArray ( returnData ) ] ;
2019-06-23 03:35:23 -07:00
} else if ( operation === 'read' ) {
// ----------------------------------
// read
// ----------------------------------
2019-07-10 07:14:55 -07:00
const rawData = this . getNodeParameter ( 'rawData' , 0 ) as boolean ;
2019-06-23 03:35:23 -07:00
2019-10-20 12:21:55 -07:00
const sheetData = await sheet . getData ( range , valueRenderMode ) ;
2019-06-23 03:35:23 -07:00
let returnData : IDataObject [ ] ;
if ( ! sheetData ) {
returnData = [ ] ;
2019-07-10 07:14:55 -07:00
} else if ( rawData === true ) {
const dataProperty = this . getNodeParameter ( 'dataProperty' , 0 ) as string ;
returnData = [
{
[ dataProperty ] : sheetData ,
}
] ;
2019-06-23 03:35:23 -07:00
} else {
2019-07-10 07:14:55 -07:00
const dataStartRow = this . getNodeParameter ( 'dataStartRow' , 0 ) as number ;
const keyRow = this . getNodeParameter ( 'keyRow' , 0 ) as number ;
2019-06-23 03:35:23 -07:00
returnData = sheet . structureArrayDataByColumn ( sheetData , keyRow , dataStartRow ) ;
}
2020-03-26 14:18:11 -07:00
if ( returnData . length === 0 && options . continue ) {
returnData = [ { } ] ;
}
2019-06-23 03:35:23 -07:00
return [ this . helpers . returnJsonArray ( returnData ) ] ;
} else if ( operation === 'update' ) {
// ----------------------------------
// update
// ----------------------------------
2019-07-10 07:14:55 -07:00
const rawData = this . getNodeParameter ( 'rawData' , 0 ) as boolean ;
2019-06-23 03:35:23 -07:00
const items = this . getInputData ( ) ;
2019-07-10 07:14:55 -07:00
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 [ ] [ ] ,
} ) ;
}
2019-06-23 03:35:23 -07:00
2019-10-20 12:21:55 -07:00
const data = await sheet . batchUpdate ( updateData , valueInputMode ) ;
2019-07-10 07:14:55 -07:00
} else {
const keyName = this . getNodeParameter ( 'key' , 0 ) as string ;
const keyRow = this . getNodeParameter ( 'keyRow' , 0 ) as number ;
const dataStartRow = this . getNodeParameter ( 'dataStartRow' , 0 ) as number ;
const setData : IDataObject [ ] = [ ] ;
items . forEach ( ( item ) = > {
setData . push ( item . json ) ;
} ) ;
2019-06-23 03:35:23 -07:00
2019-10-20 12:21:55 -07:00
const data = await sheet . updateSheetData ( setData , keyName , range , keyRow , dataStartRow , valueInputMode , valueRenderMode ) ;
2019-07-10 07:14:55 -07:00
}
2019-06-23 03:35:23 -07:00
// TODO: Should add this data somewhere
// TODO: Should have something like add metadata which does not get passed through
2019-07-10 07:14:55 -07:00
2019-06-23 03:35:23 -07:00
return this . prepareOutputData ( items ) ;
}
return [ ] ;
}
}