Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var colors = [
  2.   "#e6b8af", "#f4cccc", "#fce5cd", "#fff2cc", "#d9ead3",
  3.   "#d0e0e3", "#c9daf8", "#cfe2f3", "#d9d2e9", "#ead1dc",
  4.   "#dd7e6b", "#ea9999", "#f9cb9c", "#ffe599", "#b6d7a8",
  5.   "#a2c4c9", "#a4c2f4", "#9fc5e8", "#b4a7d6", "#d5a6bd",
  6.   "#cc4125", "#e06666", "#f6b26b", "#ffd966", "#93c47d",
  7.   "#76a5af", "#6d9eeb", "#6fa8dc", "#8e7cc3", "#c27ba0"
  8. ];
  9.  
  10. var attentionColors = ['#00ffff', '#ff00ff', '#800080'];
  11.  
  12. function groupValues(values) {
  13.   return values.reduce(function(acc, row, index) {
  14.     var bid = Bid(row, index);
  15.     if (bid.carName === '') {
  16.       acc.push([]);
  17.     } else {
  18.       acc[acc.length - 1].push(bid);
  19.     }
  20.     return acc;
  21.   }, [[]])
  22.   .filter(function(row) {
  23.     return row.length;
  24.   })
  25.   .map(function(group) {
  26.     return Group(group);
  27.   });
  28. }
  29.  
  30. function Bid(row, index) {
  31.   return {
  32.     index: index,
  33.     bidType: row[1],
  34.     carName: row[2],
  35.     yesNo: row[3],
  36.     buyer: row[4].split(' ')[0] || '',
  37.     bid: row[4].split(' ')[1] || null,
  38.     wantsToBuy: +row[5],
  39.     additional: row[6]
  40.   };
  41. }
  42.  
  43. function Group(bids) {
  44.   return {
  45.     buyer: bids[0].buyer,
  46.     wantsToBuy: bids[0].wantsToBuy,
  47.     bids: bids,
  48.     hasActive: bids.some(function(bid) { return bid.bidType === '!'; })
  49.   };
  50. }
  51.  
  52. function colorRows(groups, sheet) {
  53.   var groupColor = 0;
  54.  
  55.   groups.forEach(function(group) {
  56.     if (group.wantsToBuy !== 0) {
  57.       group.bids.forEach(function(row) {
  58.         if (group.hasActive) {
  59.           return;
  60.         }
  61.         var range = sheet.getRange(row.index + 1, 3);
  62.         var isAttentionColor = (function() {
  63.           var background = range.getBackground();
  64.          
  65.           return attentionColors.some(function(color) {
  66.             return color === background;
  67.           });
  68.         })();
  69.        
  70.        
  71.         if (isAttentionColor) {
  72.           return;
  73.         }
  74.        
  75.         range.setBackground('none');
  76.         range.setBackground(colors[groupColor]);
  77.       });
  78.       groupColor = (groupColor + 1) % colors.length;
  79.     }
  80.   });
  81. }
  82.  
  83. function highlight() {
  84.   var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  85.   var sheets = spreadsheet.getSheets();
  86.   sheets.forEach(function(sheet) {
  87.     var dataRange = sheet.getDataRange();
  88.     var values = dataRange.getValues();
  89.     var groups = groupValues(values);
  90.     colorRows(groups, sheet);
  91.   });
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement