Guest User

Untitled

a guest
Oct 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. //PROTOTYPE for an object that
  2. /*
  3. is constructed from 2d array
  4. pulls headers from first row
  5. has getters and filters as needed
  6. has setter for sheet update at row level
  7. ...
  8.  
  9. */
  10. function SheetInterface(ss,sheetName) {
  11. this.sheet = ss.getSheetByName(sheetName);
  12. this.data = this.sheet.getDataRange().getValues();
  13. this.headers = this.data.shift();
  14.  
  15. }
  16.  
  17. SheetInterface.prototype = {
  18. constructor: SheetInterface,
  19.  
  20. filtered: function(filterObject, colsArray){
  21. this.data = this.getFiltered(filterObject, colsArray);
  22. }, //rewrites this object's data
  23.  
  24. getFiltered: function(filterObject,colsArray) {
  25. var that = this;
  26. var keys = Object.keys(filterObject);
  27. var filtered = this.data.filter(function(thisRow){
  28. return keys.every(function(thisKey){
  29. var comparison = that.headers.indexOf(thisKey);
  30. return thisRow[comparison] === filterObject[thisKey];
  31. })
  32. })
  33. if (filtered.length === 0 ) return false; //EARLY return if no matches
  34. if (!colsArray) return filtered; //EARLY RETURN if no 2nd argument
  35.  
  36. var result = [];
  37. var headerIndices = [];
  38. for (var i in colsArray) {
  39. headerIndices.push(this.getIndexOf(colsArray[i]))
  40. // Logger.log(this.getIndexOf(colsArray[i]) + ' ' + colsArray[i]);
  41. }
  42. for (var j in filtered) {
  43. var row = [];
  44. for (var i in headerIndices) {
  45. var thisSubset = filtered[j][headerIndices[i]];
  46. row.push(thisSubset);
  47. }
  48. result.push(row);
  49. }
  50. Logger.log(result);
  51. return result;
  52. }, //subset of columns in rows that match every key/value pair in filterObject
  53.  
  54. getMatchingIndices: function(filterArray) {
  55. var data = this.data;
  56. var indices = [];
  57. for (var row in data) {
  58. var truthy = filterArray.every(function(thisCriteria){
  59. return data[row].indexOf(thisCriteria) > -1;
  60. })
  61. if (truthy) indices.push(row);
  62. }
  63. return indices;
  64. }, //array of index values for rows that match filterArray
  65.  
  66. getMatching: function(filterArray) {
  67. var data = this.data;
  68. var rows = [];
  69. for (var row in data) {
  70. var truthy = filterArray.every(function(thisCriteria){
  71. return data[row].indexOf(thisCriteria) > -1;
  72. })
  73. if (truthy) rows.push(data[row]);
  74. }
  75. return rows;
  76. }, //array of data rows that match filterArray
  77.  
  78. getFirstMatch: function(filterArray) {
  79. var result;
  80. var truthy = this.data.some(function(row){
  81. result = row;
  82. return filterArray.every(function(criteria){
  83. return row.indexOf(criteria) > -1;
  84. })
  85. })
  86. return (truthy ? result : -1);
  87. }, //more efficient when you are sure there's just one or none
  88.  
  89. getArrayOfUnique: function(keys){ //String []
  90. var that = this;
  91. var result = [];
  92. this.data.forEach(function(value){
  93. var temp = [];
  94. keys.forEach(function(key){
  95. temp.push(value[that.getIndexOf(key)]);
  96. })
  97. if (!result.some(function(added){ return temp.every(function(v,i) { return v === added[i] }) }) ) { //if not exist some row in added[] that matches
  98. result.push(temp);
  99. }
  100.  
  101. })
  102. return result;
  103. }, //returns array of first occurence of each unique match
  104.  
  105. getUniqueValuesByKey: function(key) { //String
  106. var index = this.headers.indexOf(key);
  107. var result = [];
  108. for (var i in this.data) {
  109. if (result.indexOf(this.data[i][index]) === -1) {
  110. result.push(this.data[i][index]);
  111. }
  112. }
  113. return result;
  114. }, //returns array of unique answers to header (single) key
  115.  
  116. getGroupedSheetRows: function(config) {
  117. //config is subset of headers, method returns array of arrays of grouped data arrays
  118. var that = this;
  119. var a = this.getArrayOfUnique(config);
  120. var b = [];
  121. a.forEach(function(unique){ //iterate the data
  122. var thisSpec = {}; //constructing a match object for this one
  123. config.forEach(function(v,i){ //iterate the subset of headers
  124. thisSpec[v] = unique[i]; //set key (header item) to this evaluee value
  125. })
  126. var c = that.getFiltered(thisSpec); //use spec to get full data
  127. if (c!== []) b.push([thisSpec, c]);
  128. });
  129.  
  130. return b;
  131. }, //b = [[[data]]]; b.length = number of unique combinations of config column values
  132.  
  133. };
  134. // USAGE
  135.  
  136. // 1. build object from spreadsheet ID and actual tab name
  137. // var ss = SpreadsheetApp.openById("1MlRNNCjl2qg83FAwLEpgMsiaXGkrhPXCIJTe6QkvQtY");
  138. // var contestants = new SheetInterface(ss,"Boilerplate");
  139.  
  140. // 2. use methods to get subsets of data
  141. // var a = contestants.getFiltered(["Reading Freud I", "2017 Fall"]);
  142. // var b = contestants.getMatchingIndices(["Case Conference", "2017 Fall", "Mixed"]);
  143. // var c = contestants.getFirstMatch(["Reading Freud I", "2017 Fall"]);
  144. // var d = contestants.getUniqueArray("Candidate");
  145. //
  146. SheetInterface.prototype.updateRow = function(index) {
  147. var numCols = this.sheet.getLastColumn();
  148. var values = [this.data[index]];
  149. this.sheet
  150. .getRange(+index+2,1,1,numCols)
  151. .setValues(values);
  152. } //writes current state of data back to sheet at row corresponding to data index
  153.  
  154. SheetInterface.prototype.getIndexOf = function(headerItem) {
  155. return this.headers.indexOf(headerItem);
  156. } //returns column index or -1
Add Comment
Please, Sign In to add comment