Skip to content

Instantly share code, notes, and snippets.

@oshliaer
Last active May 2, 2017 14:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oshliaer/87d6a13aa22b63794ab383aba4a31d15 to your computer and use it in GitHub Desktop.
Save oshliaer/87d6a13aa22b63794ab383aba4a31d15 to your computer and use it in GitHub Desktop.
function onOpen(){
SpreadsheetApp.getUi().createMenu('Color Detector').addItem('Detect colors', 'colorDetector').addToUi();
}
function colorDetector() {
var currentsheet = 'Production';
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(currentsheet);
var startRow = 1;
var numRows = sheet.getLastRow();
var rightColumn = sheet.getLastColumn();
sheet.activate();
var range = sheet.getRange(startRow, 1, numRows, rightColumn);
var bgcolors = range.getBackgrounds().map(function(row){
return [row.getUnique().join(', ')];
});
sheet.getRange(startRow, 1, numRows).setValues(bgcolors);
SpreadsheetApp.flush();
}
Array.prototype.getUnique = function(){
var u = {}, a = [];
for(var i = 0, l = this.length; i < l; ++i){
if(u.hasOwnProperty(this[i])) {
continue;
}
a.push(this[i]);
u[this[i]] = 1;
}
return a;
}
@chaosbunker
Copy link

Cool, thanks for this. I am trying to use this for a slightly different purpose, but am a bit stuck.

I have a spreadsheet with 2 sheets. one called "green" and one called "all". in sheet "all" the cells A1:Z1 are used to name each column and A2:A, B2:B etc. have different values that the user can either set the background to green or nothing. now when the user marked certain cells green in the "all" sheet , i want to have the function go through each cell of that sheet, detect if the cell is green and if yes, take that value und add it to the first free cell of the "green" sheet, in the same column - starting from row 2, because row one is again used to name the columns in the same way as in "all".

Now is my use case too different from this or does it make sense to work with this as a foundation? I don't have much experience yet with javaScript or GAS and the getUnique function confuses me still

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment