fix(basic.gblib): Trying to fix FIND error on filtering.

This commit is contained in:
Rodrigo Rodriguez 2021-03-09 13:29:40 -03:00
parent ae89c411c7
commit 5422e69841

View file

@ -303,6 +303,7 @@ export class SystemKeywords {
} }
// Creates workbook session that will be discarded. // Creates workbook session that will be discarded.
const filter = args[0].split('='); const filter = args[0].split('=');
const columnName = filter[0]; const columnName = filter[0];
const value = filter[1]; const value = filter[1];
@ -314,6 +315,8 @@ export class SystemKeywords {
.api(`${baseUrl}/drive/items/${document.id}/workbook/worksheets('${sheets.value[0].name}')/range(address='A1:Z100')`) .api(`${baseUrl}/drive/items/${document.id}/workbook/worksheets('${sheets.value[0].name}')/range(address='A1:Z100')`)
.get(); .get();
// Increments columnIndex by looping until find a column match.
let columnIndex = 0; let columnIndex = 0;
const header = results.text[0]; const header = results.text[0];
for (; columnIndex < header.length; columnIndex++) { for (; columnIndex < header.length; columnIndex++) {
@ -324,31 +327,41 @@ export class SystemKeywords {
// As BASIC uses arrays starting with 1 (one) as index, // As BASIC uses arrays starting with 1 (one) as index,
// a ghost element is added at 0 (zero) position. // a ghost element is added at 0 (zero) position.
let array = [];
array.push({ 'this is a base 1': 'array' }); let table = [];
table.push({ 'this is a hidden base 0': 'element' });
let foundIndex = 0; let foundIndex = 0;
// Fills the row variable.
for (; foundIndex < results.text.length; foundIndex++) { for (; foundIndex < results.text.length; foundIndex++) {
let result = results.text[foundIndex][columnIndex];
GBLog.info(`FIND DEBUG result on foundIndex: ${foundIndex} columnIndex: ${columnIndex}: ${result}`);
// Filter results action. // Filter results action.
if (results.text[foundIndex][columnIndex].toLowerCase() === value.toLowerCase()) {
let output = {}; if (result && result.toLowerCase() === value.toLowerCase()) {
const row = results.text[foundIndex]; let row = {};
for (let colIndex = 0; colIndex < row.length; colIndex++) { const xlRow = results.text[foundIndex];
output[header[colIndex]] = row[colIndex]; for (let colIndex = 0; colIndex < xlRow.length; colIndex++) {
row[header[colIndex]] = xlRow[colIndex];
} }
output['line'] = foundIndex + 1; row['line'] = foundIndex + 1;
array.push(output); table.push(row);
} }
} }
if (array.length === 1) { if (table.length === 1) {
GBLog.info(`BASIC: FIND the data set is EMPTY (zero results).`); GBLog.info(`BASIC: FIND returned no results (zero rows).`);
return null; return null;
} else if (array.length === 2) { } else if (table.length === 2) {
GBLog.info(`BASIC: FIND single result: ${array[0]}.`); GBLog.info(`BASIC: FIND returned single result: ${table[0]}.`);
return array[1]; return table[1];
} else { } else {
GBLog.info(`BASIC: FIND multiple result count: ${array.length}.`); GBLog.info(`BASIC: FIND returned multiple results (Count): ${table.length}.`);
return array; return table;
} }
} }