Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //PROTOTYPE for an object that
- /*
- is constructed from 2d array
- pulls headers from first row
- has getters and filters as needed
- has setter for sheet update at row level
- ...
- */
- function SheetInterface(ss,sheetName) {
- this.sheet = ss.getSheetByName(sheetName);
- this.data = this.sheet.getDataRange().getValues();
- this.headers = this.data.shift();
- }
- SheetInterface.prototype = {
- constructor: SheetInterface,
- filtered: function(filterObject, colsArray){
- this.data = this.getFiltered(filterObject, colsArray);
- }, //rewrites this object's data
- getFiltered: function(filterObject,colsArray) {
- var that = this;
- var keys = Object.keys(filterObject);
- var filtered = this.data.filter(function(thisRow){
- return keys.every(function(thisKey){
- var comparison = that.headers.indexOf(thisKey);
- return thisRow[comparison] === filterObject[thisKey];
- })
- })
- if (filtered.length === 0 ) return false; //EARLY return if no matches
- if (!colsArray) return filtered; //EARLY RETURN if no 2nd argument
- var result = [];
- var headerIndices = [];
- for (var i in colsArray) {
- headerIndices.push(this.getIndexOf(colsArray[i]))
- // Logger.log(this.getIndexOf(colsArray[i]) + ' ' + colsArray[i]);
- }
- for (var j in filtered) {
- var row = [];
- for (var i in headerIndices) {
- var thisSubset = filtered[j][headerIndices[i]];
- row.push(thisSubset);
- }
- result.push(row);
- }
- Logger.log(result);
- return result;
- }, //subset of columns in rows that match every key/value pair in filterObject
- getMatchingIndices: function(filterArray) {
- var data = this.data;
- var indices = [];
- for (var row in data) {
- var truthy = filterArray.every(function(thisCriteria){
- return data[row].indexOf(thisCriteria) > -1;
- })
- if (truthy) indices.push(row);
- }
- return indices;
- }, //array of index values for rows that match filterArray
- getMatching: function(filterArray) {
- var data = this.data;
- var rows = [];
- for (var row in data) {
- var truthy = filterArray.every(function(thisCriteria){
- return data[row].indexOf(thisCriteria) > -1;
- })
- if (truthy) rows.push(data[row]);
- }
- return rows;
- }, //array of data rows that match filterArray
- getFirstMatch: function(filterArray) {
- var result;
- var truthy = this.data.some(function(row){
- result = row;
- return filterArray.every(function(criteria){
- return row.indexOf(criteria) > -1;
- })
- })
- return (truthy ? result : -1);
- }, //more efficient when you are sure there's just one or none
- getArrayOfUnique: function(keys){ //String []
- var that = this;
- var result = [];
- this.data.forEach(function(value){
- var temp = [];
- keys.forEach(function(key){
- temp.push(value[that.getIndexOf(key)]);
- })
- if (!result.some(function(added){ return temp.every(function(v,i) { return v === added[i] }) }) ) { //if not exist some row in added[] that matches
- result.push(temp);
- }
- })
- return result;
- }, //returns array of first occurence of each unique match
- getUniqueValuesByKey: function(key) { //String
- var index = this.headers.indexOf(key);
- var result = [];
- for (var i in this.data) {
- if (result.indexOf(this.data[i][index]) === -1) {
- result.push(this.data[i][index]);
- }
- }
- return result;
- }, //returns array of unique answers to header (single) key
- getGroupedSheetRows: function(config) {
- //config is subset of headers, method returns array of arrays of grouped data arrays
- var that = this;
- var a = this.getArrayOfUnique(config);
- var b = [];
- a.forEach(function(unique){ //iterate the data
- var thisSpec = {}; //constructing a match object for this one
- config.forEach(function(v,i){ //iterate the subset of headers
- thisSpec[v] = unique[i]; //set key (header item) to this evaluee value
- })
- var c = that.getFiltered(thisSpec); //use spec to get full data
- if (c!== []) b.push([thisSpec, c]);
- });
- return b;
- }, //b = [[[data]]]; b.length = number of unique combinations of config column values
- };
- // USAGE
- // 1. build object from spreadsheet ID and actual tab name
- // var ss = SpreadsheetApp.openById("1MlRNNCjl2qg83FAwLEpgMsiaXGkrhPXCIJTe6QkvQtY");
- // var contestants = new SheetInterface(ss,"Boilerplate");
- // 2. use methods to get subsets of data
- // var a = contestants.getFiltered(["Reading Freud I", "2017 Fall"]);
- // var b = contestants.getMatchingIndices(["Case Conference", "2017 Fall", "Mixed"]);
- // var c = contestants.getFirstMatch(["Reading Freud I", "2017 Fall"]);
- // var d = contestants.getUniqueArray("Candidate");
- //
- SheetInterface.prototype.updateRow = function(index) {
- var numCols = this.sheet.getLastColumn();
- var values = [this.data[index]];
- this.sheet
- .getRange(+index+2,1,1,numCols)
- .setValues(values);
- } //writes current state of data back to sheet at row corresponding to data index
- SheetInterface.prototype.getIndexOf = function(headerItem) {
- return this.headers.indexOf(headerItem);
- } //returns column index or -1
Add Comment
Please, Sign In to add comment