From 046873e66b37b78eb44743669741ce6b9df8f3ba Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Thu, 15 Oct 2020 13:28:31 +0200 Subject: [PATCH] :bug: Fix Google Sheets update bug with spaces --- .../nodes/Google/Sheet/GoogleSheet.ts | 30 +++++++++++++++---- .../nodes/Google/Sheet/GoogleSheets.node.ts | 12 +++----- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/packages/nodes-base/nodes/Google/Sheet/GoogleSheet.ts b/packages/nodes-base/nodes/Google/Sheet/GoogleSheet.ts index 63133c495b..4dea766914 100644 --- a/packages/nodes-base/nodes/Google/Sheet/GoogleSheet.ts +++ b/packages/nodes-base/nodes/Google/Sheet/GoogleSheet.ts @@ -65,6 +65,22 @@ export class GoogleSheet { } + /** + * Encodes the range that also none latin character work + * + * @param {string} range + * @returns {string} + * @memberof GoogleSheet + */ + encodeRange(range: string): string { + if (range.includes('!')) { + const [sheet, ranges] = range.split('!'); + range = `${encodeURIComponent(sheet)}!${ranges}`; + } + return range; + } + + /** * Clears values from a sheet * @@ -167,7 +183,7 @@ export class GoogleSheet { async appendData(range: string, data: string[][], valueInputMode: ValueInputOption) { const body = { - range, + range: decodeURIComponent(range), values: data, }; @@ -254,12 +270,14 @@ export class GoogleSheet { */ async updateSheetData(inputData: IDataObject[], indexKey: string, range: string, keyRowIndex: number, dataStartRowIndex: number, valueInputMode: ValueInputOption, valueRenderMode: ValueRenderOption): Promise { // Get current data in Google Sheet - let rangeStart: string, rangeEnd: string; + let rangeStart: string, rangeEnd: string, rangeFull: string; let sheet: string | undefined = undefined; if (range.includes('!')) { - [sheet, range] = range.split('!'); + [sheet, rangeFull] = range.split('!'); + } else { + rangeFull = range; } - [rangeStart, rangeEnd] = range.split(':'); + [rangeStart, rangeEnd] = rangeFull.split(':'); const rangeStartSplit = rangeStart.match(/([a-zA-Z]{1,10})([0-9]{0,10})/); const rangeEndSplit = rangeEnd.match(/([a-zA-Z]{1,10})([0-9]{0,10})/); @@ -270,7 +288,7 @@ export class GoogleSheet { const keyRowRange = `${sheet ? sheet + '!' : ''}${rangeStartSplit[1]}${dataStartRowIndex}:${rangeEndSplit[1]}${dataStartRowIndex}`; - const sheetDatakeyRow = await this.getData(keyRowRange, valueRenderMode); + const sheetDatakeyRow = await this.getData(this.encodeRange(keyRowRange), valueRenderMode); if (sheetDatakeyRow === undefined) { throw new Error('Could not retrieve the key row!'); @@ -290,7 +308,7 @@ export class GoogleSheet { const keyColumn = this.getColumnWithOffset(rangeStartSplit[1], keyIndex); const keyColumnRange = `${sheet ? sheet + '!' : ''}${keyColumn}${startRowIndex}:${keyColumn}${endRowIndex}`; - const sheetDataKeyColumn = await this.getData(keyColumnRange, valueRenderMode); + const sheetDataKeyColumn = await this.getData(this.encodeRange(keyColumnRange), valueRenderMode); if (sheetDataKeyColumn === undefined) { throw new Error('Could not retrieve the key column!'); diff --git a/packages/nodes-base/nodes/Google/Sheet/GoogleSheets.node.ts b/packages/nodes-base/nodes/Google/Sheet/GoogleSheets.node.ts index 8149c0f555..ba6cad2363 100644 --- a/packages/nodes-base/nodes/Google/Sheet/GoogleSheets.node.ts +++ b/packages/nodes-base/nodes/Google/Sheet/GoogleSheets.node.ts @@ -611,10 +611,6 @@ export class GoogleSheets implements INodeType { let range = ''; if (operation !== 'delete') { range = this.getNodeParameter('range', 0) as string; - if (range.includes('!')) { - const [sheet, ranges] = range.split('!'); - range = `${encodeURIComponent(sheet)}!${ranges}`; - } } const options = this.getNodeParameter('options', 0, {}) as IDataObject; @@ -636,7 +632,7 @@ export class GoogleSheets implements INodeType { }); // Convert data into array format - const data = await sheet.appendSheetData(setData, range, keyRow, valueInputMode); + 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 @@ -647,7 +643,7 @@ export class GoogleSheets implements INodeType { // clear // ---------------------------------- - await sheet.clearData(range); + await sheet.clearData(sheet.encodeRange(range)); const items = this.getInputData(); return this.prepareOutputData(items); @@ -691,7 +687,7 @@ export class GoogleSheets implements INodeType { // lookup // ---------------------------------- - const sheetData = await sheet.getData(range, valueRenderMode); + const sheetData = await sheet.getData(sheet.encodeRange(range), valueRenderMode); if (sheetData === undefined) { return []; @@ -726,7 +722,7 @@ export class GoogleSheets implements INodeType { const rawData = this.getNodeParameter('rawData', 0) as boolean; - const sheetData = await sheet.getData(range, valueRenderMode); + const sheetData = await sheet.getData(sheet.encodeRange(range), valueRenderMode); let returnData: IDataObject[]; if (!sheetData) {