🐛 Fix Google Sheets update bug with spaces

This commit is contained in:
Jan Oberhauser 2020-10-15 13:28:31 +02:00
parent 8f84cbcbce
commit 046873e66b
2 changed files with 28 additions and 14 deletions

View file

@ -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<string[][]> {
// 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!');

View file

@ -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) {