Skip to content

Commit

Permalink
Adjusting operations "orders/table" and "orders/done".
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomanuel committed Feb 19, 2021
1 parent acaf09a commit aa74a01
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
14 changes: 10 additions & 4 deletions tasks/do-orders-done.gs
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,24 @@ 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);
// }, []);
// 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..");
Expand Down
23 changes: 17 additions & 6 deletions tasks/do-orders-table.gs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -451,19 +452,28 @@ 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());
}

/**
* Returns ALL the rows contained in ALL defined sheets as order tables
*/
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,
Expand All @@ -472,6 +482,7 @@ function BinDoOrdersTable() {
run,
init,
execute,
getRows
getRows,
hasSheets
};
}

0 comments on commit aa74a01

Please sign in to comment.