From aa74a0148dfbfd1808cb684c1c67217d52dfeaf6 Mon Sep 17 00:00:00 2001 From: Diego Calero Date: Thu, 18 Feb 2021 23:32:35 -0300 Subject: [PATCH] Adjusting operations "orders/table" and "orders/done". --- tasks/do-orders-done.gs | 14 ++++++++++---- tasks/do-orders-table.gs | 23 +++++++++++++++++------ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/tasks/do-orders-done.gs b/tasks/do-orders-done.gs index c45db8d..00734ab 100644 --- a/tasks/do-orders-done.gs +++ b/tasks/do-orders-done.gs @@ -49,7 +49,14 @@ function BinDoOrdersDone() { function execute(range_or_cell, options) { // const ticker_against = options["ticker"]; Logger.log("[BinDoOrdersDone] Running.."); - + + const ot = BinDoOrdersTable(); + const has_sheets = ot.hasSheets(); + if (!has_sheets) { + console.error("[BinDoOrdersDone] It seems that we didn't find any sheet in the spreadsheet with the 'orders/table' operation in it!"); + return [["ERROR: This operation requires at least ONE sheet in the spreadsheet with the 'orders/table' operation in it!"]]; + } + // const range = BinUtils().getRangeOrCell(range_or_cell) || []; // const data = range.reduce(function(rows, asset) { // return rows.concat(asset); @@ -57,10 +64,9 @@ function BinDoOrdersDone() { // const parsed = parse(data, options); // Get ALL the rows contained in ALL defined sheets as order tables! - const data = BinDoOrdersTable().getRows(); + const data = ot.getRows(); if (!data.length) { - console.error("[BinDoOrdersDone] It seems that we didn't find any sheet in the spreadsheet with the 'orders/table' operation in it!"); - return [["- no results to display - WARNING: This operation requires at least ONE sheet in the spreadsheet with the 'orders/table' operation in it!"]]; + return [["- no orders to display yet -"]]; } const parsed = parse(data, options); Logger.log("[BinDoOrdersDone] Returning "+data.length+" orders.."); diff --git a/tasks/do-orders-table.gs b/tasks/do-orders-table.gs index fba5577..7f6054c 100644 --- a/tasks/do-orders-table.gs +++ b/tasks/do-orders-table.gs @@ -275,8 +275,8 @@ function BinDoOrdersTable() { // Set initial stats values _initCellValue(sheet, "B2"); _initCellValue(sheet, "E2", "waiting for 1st poll run"); - _initCellValue(sheet, "H2"); - _initCellValue(sheet, "J2"); + _initCellValue(sheet, "H2", 0); + _initCellValue(sheet, "J2", 0); // Remove extra rows (if any) const row_min = Math.max(header_size+1, sheet.getLastRow()); @@ -408,7 +408,8 @@ function BinDoOrdersTable() { function _updateStats(sheet, saved_data) { // Calculate total orders per pair - const pairs = sheet.getRange("D"+(header_size+1)+":D"+sheet.getLastRow()).getValues(); + const row_min = Math.max(header_size+1, sheet.getLastRow()); + const pairs = sheet.getRange("D"+(header_size+1)+":D"+row_min).getValues(); const [count, totals] = pairs.reduce(function([count, acc], [pair]) { if (pair) { acc[pair] = 1 + (acc[pair]||0); @@ -451,7 +452,7 @@ function BinDoOrdersTable() { * Get the FULL data range for given sheet */ function _getSheetDataRange(sheet) { - return sheet.getRange(header_size+1, 1, sheet.getLastRow()-header_size, sheet.getLastColumn()); + return sheet.getRange(header_size+1, 1, sheet.getLastRow(), sheet.getLastColumn()); } /** @@ -459,11 +460,20 @@ function BinDoOrdersTable() { */ function getRows() { return _findSheets().reduce(function(rows, sheet) { // Go through each sheet found - const values = _getSheetDataRange(sheet).getValues(); + const values = _getSheetDataRange(sheet).getValues().filter(function(val) { + return val && val[0]; // Filter possible empty rows! + }); return values && values.length ? rows.concat(values) : rows; }, []); } + /** + * Returns true if at least ONE sheet in the spreadsheet is defined as orders table + */ + function hasSheets() { + return _findSheets().length > 0; + } + // Return just what's needed from outside! return { tag, @@ -472,6 +482,7 @@ function BinDoOrdersTable() { run, init, execute, - getRows + getRows, + hasSheets }; } \ No newline at end of file