From 29031a5e13110ff07c545634f8920dddba127280 Mon Sep 17 00:00:00 2001 From: Jonathon Broughton Date: Wed, 1 Nov 2017 14:13:22 +0000 Subject: [PATCH] Revert "about page" This reverts commit 81bfadd92aaa364fe085b0f329b26b5a240764f2. --- Addon.gs | 81 ++++++++++++++++++++++++++++ Functions/Arrays/ARRAY_JOIN.gs | 27 ++++++++++ Functions/Arrays/ROW_SUM.gs | 15 ++++++ Functions/Lookups/INDIRECT_LOOKUP.gs | 57 ++++++++++++++++++++ Functions/Lookups/INDIRECT_SUM.gs | 35 ++++++++++++ html/about.html | 11 ---- 6 files changed, 215 insertions(+), 11 deletions(-) create mode 100644 Addon.gs create mode 100644 Functions/Arrays/ARRAY_JOIN.gs create mode 100644 Functions/Arrays/ROW_SUM.gs create mode 100644 Functions/Lookups/INDIRECT_LOOKUP.gs create mode 100644 Functions/Lookups/INDIRECT_SUM.gs delete mode 100644 html/about.html diff --git a/Addon.gs b/Addon.gs new file mode 100644 index 0000000..a757350 --- /dev/null +++ b/Addon.gs @@ -0,0 +1,81 @@ +/** + * Creates a menu entry in the Google Docs UI when the document is opened. + * + * @param {object} e The event parameter for a simple onOpen trigger. To + * determine which authorization mode (ScriptApp.AuthMode) the trigger is + * running in, inspect e.authMode. + */ +function onOpen() { + 'use strict'; + try { + + var ui, context, menu, sheetType, docType, type; + + try { + ui = DocumentApp.getUi(); + context = DocumentApp; + } catch (err) { + Logger.log(err); + ui = SpreadsheetApp.getUi(); + context = SpreadsheetApp; + } + + menu = ui.createAddonMenu(); + + if (context === SpreadsheetApp) { + + try { + sheetType = JSON.parse(PropertiesService.getDocumentProperties().getProperty('sheetType')); + } catch (enabled) { + Logger.log(enabled); + } + type = (sheetType) ? sheetType.type : null; + + menu.addItem('Add Custom Functions', 'addFunctions'); + + } else if (context === DocumentApp) { + try { + docType = JSON.parse(PropertiesService.getDocumentProperties() + .getProperty('docType')); + } catch (enabled) { + Logger.log(enabled); + } + + // Documents do not have custom functions. + + } + + GRFNTools.appsuiteType = type; + + menu.addToUi(); + + } catch (elseErr) { + Logger.log(elseErr); + Logger.log('Not a supported Google Drive App'); + } +} + +/** + * Runs when the add-on is installed. + * + * @param {object} e The event parameter for a simple onInstall trigger. To + * determine which authorization mode (ScriptApp.AuthMode) the trigger is + * running in, inspect e.authMode. (In practice, onInstall triggers always + * run in AuthMode.FULL, but onOpen triggers may be AuthMode.LIMITED or + * AuthMode.NONE.) + */ +function onInstall(e) { + onOpen(e); +} + +/** + * Displays a confirmation of function installation. + */ +function addFunctions() { + var title = 'stardotbmp/google-sheet-functions'; + var message = 'The functions are now available in ' + + 'this spreadsheet. More information is available in the function help ' + + 'box that appears when you start using them in a forumula.'; + var ui = SpreadsheetApp.getUi(); + ui.alert(title, message, ui.ButtonSet.OK); +} \ No newline at end of file diff --git a/Functions/Arrays/ARRAY_JOIN.gs b/Functions/Arrays/ARRAY_JOIN.gs new file mode 100644 index 0000000..e045144 --- /dev/null +++ b/Functions/Arrays/ARRAY_JOIN.gs @@ -0,0 +1,27 @@ +/** +* Applys a JOIN on an array or range. JOIN() doesn't work with ArrayFormula. This function works. +* +* @param {string} delimeter The delimeter to use between array values int he joned string. +* @param {Range} range The specified range of cells. +* @param {boolean} all A flag swith between returning all values in two-dimensions as a single result or to return rows joined * +* @customfunction +*/ +function ARRAY_JOIN(delimeter, range, all) { + + delimeter = delimeter || ""; + all = all ? all != false : false; + + var result = []; + + if (!range.map) { + return "Invalid Parameters"; + } + + result = range.map(function (row, i) { + return row.filter(function(col) { + return col != ''; + }).join(delimeter); + }) + + return !all ? result.map(function (r) { return [r]; }) : [result.join(delimeter)]; +} \ No newline at end of file diff --git a/Functions/Arrays/ROW_SUM.gs b/Functions/Arrays/ROW_SUM.gs new file mode 100644 index 0000000..03f9f96 --- /dev/null +++ b/Functions/Arrays/ROW_SUM.gs @@ -0,0 +1,15 @@ +/** +* Returns a sum for each row of a given array. +* @param {range} range The range or array to sum. +* @customfunction +*/ +function ROW_SUM(range) { + + if (!range.map) { + range = [[range]]; + } + + return range.map(function(row){ + return row.reduce(function(a, b) { return a + b; }, 0); + }); +} \ No newline at end of file diff --git a/Functions/Lookups/INDIRECT_LOOKUP.gs b/Functions/Lookups/INDIRECT_LOOKUP.gs new file mode 100644 index 0000000..11486dc --- /dev/null +++ b/Functions/Lookups/INDIRECT_LOOKUP.gs @@ -0,0 +1,57 @@ +/** +* Returns an array of indirect single cell references from different sheets. +* @param {A2:A} sheets The column containing sheets' names. +* @param {"E1"} ref The range to return from each sheet. +* @param {string} key Value to match +* @param {int} column The column from which to return a value +* @param {int} headers The number of header rows to ignore in the lookup range +* @customfunction +*/ +function INDIRECT_LOOKUP(sheets, ref, keys, column, headers) { + var ss = SpreadsheetApp.getActiveSpreadsheet(), + result = []; + + headers = headers || 0; + + var sheetList = (!sheets.map) ? [[sheets]] : sheets; + var keyList = (!keys.map) ? [[keys]] : keys; + + if (keyList.length != sheetList.length) { + if (keyList.length == 1) { + keyList = Array(sheetList.length).fill(keys); + } else { + throw("Matching Keys should equal the number of referenced sheets"); + } + } + + for (var i = 0, l = sheets.length; i < l; i+=1) + { + if (sheets[i] !='') { + + var values = ss.getSheetByName(sheets[i][0]).getRange(ref).getValues().slice(headers); + + var columnValues = values.filter(function(row){ + + return row[0] == keyList[i]; + + }).map(function(row, i){ return row[column-1]; }); + + result.push(columnValues); + } else { + result.push([0]); + } + } + + // balance column in results + var maxCols = result.reduce(function(count, row){ return (count < row.length) ? row.length : count; },0); + + result = result.map(function(row){ + if (row.length < maxCols) { + return row.concat(Array(maxCols - row.length).fill(0)); + } + return row; + }); + + //return JSON.stringify(result); + return result; +} \ No newline at end of file diff --git a/Functions/Lookups/INDIRECT_SUM.gs b/Functions/Lookups/INDIRECT_SUM.gs new file mode 100644 index 0000000..1c2da80 --- /dev/null +++ b/Functions/Lookups/INDIRECT_SUM.gs @@ -0,0 +1,35 @@ +/** +* Returns an array of indirect single cell references from different sheets. +* @param {A2:A} sheets The column containing sheets' names or an array of sheet names +* @param {"E1:E50"} range The single column range reference to totalise from each sheet. +* @customfunction +*/ +function INDIRECT_SUM(sheets, range) { + var ss = SpreadsheetApp.getActiveSpreadsheet(), + result = []; + + if (!sheets.map) { + sheets = [sheets]; + } + + for (var i in sheets) + { + if (sheets[i] =='') break; + +// result.push(sheets[i]); + + var values = ss.getSheetByName(sheets[i][0]).getRange(range).getValues() + var sum = values.reduce(function(total, row) { + + if (row.length > 1) { + throw("Only a single Column may be totalised"); + } + + return total += row[0]; + + }, 0); + + result.push(sum); + } + return result; +} \ No newline at end of file diff --git a/html/about.html b/html/about.html deleted file mode 100644 index f39f6dc..0000000 --- a/html/about.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - -