diff --git a/.gitignore b/.gitignore index 8a26df8..06fa212 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ yarn-error.log .*.json .vscode .env +.DS_Store # Version file: this file will be used only in Apps Script project # after running "yarn push" command. See package.json diff --git a/src/Code.js b/src/Code.js index 20a1ee4..07579a2 100644 --- a/src/Code.js +++ b/src/Code.js @@ -515,9 +515,25 @@ function MBCUSTOMQUERY(events, groupBy, orderBy, limit, offset) { } console.log(`Results: ${JSON.stringify(results)}`); - // turn the array of objects into a flat array + const headers = []; + let numEmptyColumns = 0; + // eslint-disable-next-line no-restricted-syntax, guard-for-in + for (let col = 0; numEmptyColumns < payload.events.length; col++) { + numEmptyColumns = 0; + // eslint-disable-next-line no-restricted-syntax + for (const event of payload.events) { + if (event.select[col]) { + headers.push(event.select[col].alias); + } else { + numEmptyColumns++; + } + } + } + const objArr = results.result.rows; - return objectArrayToArray(objArr); + + // turn the array of objects into a flat array + return objectArrayToArray(objArr, headers); } /** diff --git a/src/Code.spec.js b/src/Code.spec.js index 4c3ac1c..ea07b86 100644 --- a/src/Code.spec.js +++ b/src/Code.spec.js @@ -473,8 +473,8 @@ function testRunner(testSheetURL) { 0, ], expected: [ - ['amount', 'sender'], - [1e+27, '0x89d048be68575f2b56a999ba24faacabd1b919fb'], + ['sender', 'amount'], + ['0x89d048be68575f2b56a999ba24faacabd1b919fb', 1e+27], ], }, { @@ -495,10 +495,10 @@ function testRunner(testSheetURL) { 0, ], expected: [ - ['amount', 'sender'], - [1e+27, '0x89d048be68575f2b56a999ba24faacabd1b919fb'], - [1000000000000000000, '0xa616eed6ad7a0cf5d2388301a710c273ca955e05'], - [1000000000000000000, '0xbac1cd4051c378bf900087ccc445d7e7d02ad745'], + ['sender', 'amount'], + ['0x89d048be68575f2b56a999ba24faacabd1b919fb', 1e+27], + ['0xa616eed6ad7a0cf5d2388301a710c273ca955e05', 1000000000000000000], + ['0xbac1cd4051c378bf900087ccc445d7e7d02ad745', 1000000000000000000], ], }, { @@ -515,12 +515,12 @@ function testRunner(testSheetURL) { ], ], expected: [ - ['amount', 'sender'], - [1000000000000000000, '0xa616eed6ad7a0cf5d2388301a710c273ca955e05'], - [1000000000000000000, '0xbac1cd4051c378bf900087ccc445d7e7d02ad745'], - [50, '0x0d6c3707a98bce1a56247555c8b74242705b8acf'], - [50000000000000000000, '0x0d6c3707a98bce1a56247555c8b74242705b8acf'], - [50000000000000000000, '0x005080f78567f8001115f1eee835dd0151bea476'], + ['sender', 'amount'], + ['0xa616eed6ad7a0cf5d2388301a710c273ca955e05', 1000000000000000000], + ['0xbac1cd4051c378bf900087ccc445d7e7d02ad745', 1000000000000000000], + ['0x0d6c3707a98bce1a56247555c8b74242705b8acf', 50], + ['0x0d6c3707a98bce1a56247555c8b74242705b8acf', 50000000000000000000], + ['0x005080f78567f8001115f1eee835dd0151bea476', 50000000000000000000], ], }, { @@ -541,29 +541,93 @@ function testRunner(testSheetURL) { 1, ], expected: [ - ['amount', 'sender'], - [1000000000000000000, '0xa616eed6ad7a0cf5d2388301a710c273ca955e05'], + ['sender', 'amount'], + ['0xa616eed6ad7a0cf5d2388301a710c273ca955e05', 1000000000000000000], ], }, - // TODO: try after https://github.com/curvegrid/multibaas-for-google-sheets/issues/53 { - name: 'TestMBCUSTOMQUERY with wrong selected range', - skip: true, + name: 'TestMBCUSTOMQUERY with multiple events and filters', + skip: false, only: false, debug: false, func: MBCUSTOMQUERY, isTemplate: false, args: [ [ - ['eventName', 'alias', 'index', 'aggregator', 'alias', 'index', 'aggregator', undefined], - ['LogDeposited(address,uint256)', 'sender', 0, '', 'amount', 1, '', undefined], + [ + 'eventName', + 'alias', + 'index', + 'aggregator', + 'alias', + 'index', + 'aggregator', + 'rule', + 'operand', + 'operator', + 'value', + 'rule', + 'operand', + 'operator', + 'value', + 'rule', + 'operand', + 'operator', + 'value', + ], + [ + 'LogDeposited(address,uint256)', + 'sender4', + 0, + '', + 'amount4', + 1, + '', + 'and', + 'input0', + 'equal', + '0x89d048be68575f2b56a999ba24faacabd1b919fb', + 'and', + 'input1', + 'greaterthan', + 1, + 'and:and', + 'block_number', + 'greaterthan', + 1, + ], + [ + 'LogSent(address,uint256)', + 'receiver2', + 0, + '', + 'amount2', + 1, + '', + 'and', + 'input0', + 'equal', + '0x0d6c3707a98bce1a56247555c8b74242705b8acf', + 'and', + 'input1', + 'greaterthan', + 1, + 'and:and', + 'block_number', + 'greaterthan', + 1, + ], ], '', '', - 1, - 1, + 3, + 0, + ], + expected: [ + ['sender4', 'receiver2', 'amount4', 'amount2'], + ['0x89d048be68575f2b56a999ba24faacabd1b919fb', '', 1e+27, ''], + ['', '0x0d6c3707a98bce1a56247555c8b74242705b8acf', '', 1e+18], ], - expected: ['#ERROR!'], }, { name: 'TestMBCUSTOMQUERYTEMPLATE with 2 filters', diff --git a/src/library/Util.js b/src/library/Util.js index 4328460..d6208ff 100644 --- a/src/library/Util.js +++ b/src/library/Util.js @@ -404,7 +404,7 @@ function eventsToArray(entries) { return rows; } -function objectArrayToArray(objArr) { +function objectArrayToArray(objArr, headersPreset) { const rows = []; if (!Array.isArray(objArr) || objArr.length < 1) { @@ -412,7 +412,12 @@ function objectArrayToArray(objArr) { } // header row: just take the keys from the first row - const headers = keysFromObj(objArr[0], true); + let headers; + if (!Array.isArray(headersPreset)) { + headers = keysFromObj(objArr[0], false); + } else { + headers = headersPreset; + } rows.push(headers); // body rows