Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function onOpen() {SpreadsheetApp.getUi().createMenu("Scripts").addItem("Number by Colors", "numberColors").addToUi();}
- function numberColors() {
- // ⬇ script 'Settings' below. UPDATE IF RESTRUCTURING OR RECOLORING SHEET! ⬇
- var colorCol = 6, // 6 is F
- numberCol = 7, // 7 is G
- firstDataRow = 2, // first row in which to begin color checks (to skip headers)
- palette = [
- "#ffffff", // white (or your spreadsheet default)
- "#d9ead3", // light green 3
- "#d9d9d9", // light gray 1
- "#f4cccc" // light red 3
- ];
- // ⬆ script 'Settings' above. UPDATE IF RESTRUCTURING OR RECOLORING SHEET! ⬆
- var numbers = [],
- activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(),
- dataNumRows = activeSheet.getMaxRows()-(firstDataRow-1),
- colorColColors = activeSheet.getRange(2, colorCol, dataNumRows, 1).getBackgrounds();
- for (var rowIndex in colorColColors) {
- numbers[rowIndex] = [];
- var rowColor = colorColColors[rowIndex].toString(),
- colorIndex = palette.indexOf(rowColor);
- if (colorIndex == -1) {
- numbers[rowIndex].push(rowColor); // custom output when cell color is missing from script palette.
- } else if (colorIndex == 0) {
- numbers[rowIndex].push(""); // custom output for first palette color (spreadsheet default). "" means blank.
- } else {
- numbers[rowIndex].push(colorIndex);
- }
- }
- activeSheet.getRange(2, numberCol, dataNumRows, 1).setValues(numbers);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement