Advertisement
Guest User

Code.gs

a guest
Nov 6th, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. /*
  2. * NOTE:
  3. * I have also added the following to appsscript.json:
  4. *
  5. * "oauthScopes": [
  6. * "https://www.googleapis.com/auth/spreadsheets.currentonly",
  7. * "https://www.googleapis.com/auth/spreadsheets"
  8. * ]
  9. *
  10. */
  11.  
  12.  
  13. //Used to save the mergedRanges so they can be put back together
  14. var mergedRanges;
  15.  
  16. //Adds a menu item to run the calcHourlySplitByColour function:
  17. function onOpen() {
  18. var ss = SpreadsheetApp.getActiveSpreadsheet();
  19. var menuEntries = [];
  20. menuEntries.push({name: "Calculate Hourly Split", functionName: "calcHourlySplitByColour"});
  21. ss.addMenu("CustomFunctions", menuEntries);
  22. };
  23.  
  24. function calcHourlySplitByColour() {
  25. var activeRange = SpreadsheetApp.getActiveRange();
  26. var activeSheet = activeRange.getSheet();
  27. var formula = activeRange.getFormula();
  28.  
  29. var rangeToSearch = "'Sheet1'!B2:H30"; //Range to count for the colours
  30. var purpleColourRef = "'Sheet1'!J2"; //Where to get the bg colour to count
  31. var purpleColourCountResult = "'Sheet1'!K2"; //Where to store the result
  32.  
  33. //Break apart the merged cells
  34. breakApartRange(rangeToSearch);
  35.  
  36. var purpleColourCount = countColouredCells2(rangeToSearch, purpleColourRef);
  37.  
  38. //Stitch the previously merged cells back together
  39. reMergeRange(rangeToSearch);
  40.  
  41. //Save the result to the spreadsheet
  42. saveResult(purpleColourCount, purpleColourCountResult) //Saves value of param1 to the location of param2 (note: I haven't included this function in the script)
  43. };
  44.  
  45. function breakApartRange(breakRange){
  46. var activeRange = SpreadsheetApp.getActiveRange();
  47. var activeSheet = activeRange.getSheet();
  48. var formula = activeRange.getFormula();
  49. var fullRange = activeSheet.getRange(breakRange);
  50.  
  51. //break merged cells
  52. mergedRanges = fullRange.getMergedRanges();
  53. mergedRanges.forEach(range => range.setValue(range.breakApart().getValue()));
  54. };
  55.  
  56. function reMergeRange(breakRange){
  57. var activeRange = SpreadsheetApp.getActiveRange();
  58. var activeSheet = activeRange.getSheet();
  59. var formula = activeRange.getFormula();
  60. var fullRange = activeSheet.getRange(breakRange);
  61.  
  62. //re merge cells
  63. mergedRanges.forEach(range => range.merge());
  64. };
  65.  
  66. //Gets the count of cells with a particular colour within the range. Merged cells count as 1
  67. function countColouredCells2(countRange,colorRef) {
  68. var activeRange = SpreadsheetApp.getActiveSpreadsheet().getRangeByName(countRange);
  69. var activeSheet = activeRange.getSheet();
  70. var formula = activeRange.getFormula();
  71.  
  72. SpreadsheetApp.getUi().alert("1st Pop" + activeRange.getFontFamily());
  73. var rangeA1Notation = formula.match(/\((.*)\,/).pop();
  74. var range = activeSheet.getRange(rangeA1Notation);
  75. var bg = range.getBackgrounds();
  76. var values = range.getValues();
  77.  
  78. SpreadsheetApp.getUi().alert("2nd Pop");
  79. var colorCellA1Notation = formula.match(/\,(.*)\)/).pop();
  80. var colorCell = activeSheet.getRange(colorCellA1Notation);
  81. var color = colorCell.getBackground();
  82.  
  83. SpreadsheetApp.getUi().alert("Post 2nd Pop");
  84. var count = 0;
  85.  
  86. for(var i=0;i<bg.length;i++)
  87. for(var j=0;j<bg[0].length;j++)
  88. if( bg[i][j] == color )
  89. count=count+1;
  90. return count;
  91. };
  92.  
  93. /*
  94. * NOTE:
  95. * The above is my attempt at refactoring the below method:
  96. *
  97. * Gets the count of cells with a particular colour within the range. Merged cells count as 1
  98. * Works from spreadsheet but don't know what to pass in/how to pass in as SpreadsheetApp.getActiveRange(); is just null
  99. *
  100. * function countColouredCells(countRange,colorRef) {
  101. * var activeRange = SpreadsheetApp.getActiveRange();
  102. * var activeSheet = activeRange.getSheet();
  103. * var formula = activeRange.getFormula();
  104. *
  105. * var rangeA1Notation = formula.match(/\((.*)\,/).pop();
  106. * var range = activeSheet.getRange(rangeA1Notation);
  107. * var bg = range.getBackgrounds();
  108. * var values = range.getValues();
  109. *
  110. * var colorCellA1Notation = formula.match(/\,(.*)\)/).pop();
  111. * var colorCell = activeSheet.getRange(colorCellA1Notation);
  112. * var color = colorCell.getBackground();
  113. *
  114. * var count = 0;
  115. *
  116. * for(var i=0;i<bg.length;i++)
  117. * for(var j=0;j<bg[0].length;j++)
  118. * if( bg[i][j] == color )
  119. * count=count+1;
  120. * return count;
  121. * };
  122. *
  123. *
  124. */
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement