IAmMoonie

% Decay Cell Values

Aug 23rd, 2024
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.25 KB | Source Code | 0 0
  1. /**
  2.  * @constant {Object} CONFIG
  3.  * @property {boolean} useCustomMenu - Set to true to use the custom menu for user input for range and decay amount.
  4.  * @property {number} decayPercentage - The default percentage of decay to apply.
  5.  * @property {number} chunkSize - The maximum number of rows to process in a single batch.
  6.  */
  7. const CONFIG = {
  8.   useCustomMenu: false,
  9.   decayPercentage: 10,
  10.   chunkSize: 1000
  11. };
  12.  
  13. /**
  14.  * Adds a custom menu to the Google Sheet when opened.
  15.  */
  16. function onOpen() {
  17.   const ui = SpreadsheetApp.getUi();
  18.   ui.createMenu("Decay Operations")
  19.     .addItem("Apply Decay", "startDecay_")
  20.     .addToUi();
  21. }
  22.  
  23. /**
  24.  * Starts the decay process based on the configuration.
  25.  */
  26. function startDecay_() {
  27.   if (CONFIG.useCustomMenu) {
  28.     showDecayDialog_();
  29.   } else {
  30.     const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  31.     const range = sheet.getActiveRange();
  32.     Decay(range, CONFIG.decayPercentage);
  33.   }
  34. }
  35.  
  36. /**
  37.  * Shows a dialog to the user to input the range and decay amount.
  38.  */
  39. function showDecayDialog_() {
  40.   const ui = SpreadsheetApp.getUi();
  41.   const rangeResponse = ui.prompt(
  42.     "Decay Operation",
  43.     "Enter the range (e.g., A1:A100) to apply decay:",
  44.     ui.ButtonSet.OK_CANCEL
  45.   );
  46.   if (rangeResponse.getSelectedButton() == ui.Button.CANCEL) return;
  47.   const decayResponse = ui.prompt(
  48.     "Decay Operation",
  49.     "Enter the decay percentage (e.g., 10 for 10%):",
  50.     ui.ButtonSet.OK_CANCEL
  51.   );
  52.   if (decayResponse.getSelectedButton() == ui.Button.CANCEL) return;
  53.   const range = rangeResponse.getResponseText();
  54.   const decayPercentage = parseFloat(decayResponse.getResponseText());
  55.   if (
  56.     isNaN(decayPercentage) ||
  57.     decayPercentage <= 0 ||
  58.     decayPercentage >= 100
  59.   ) {
  60.     ui.alert(
  61.       "Invalid decay percentage. Please enter a number between 1 and 99."
  62.     );
  63.     return;
  64.   }
  65.   Decay(range, decayPercentage);
  66. }
  67.  
  68. /**
  69.  * Applies decay to the values in the specified range of the Google Sheet.
  70.  * Processes the data in chunks if the dataset is larger than the configured chunk size.
  71.  *
  72.  * @param {string|GoogleAppsScript.Spreadsheet.Range} range - The range to decay, can be A1 notation or a Range object.
  73.  * @param {number} decayPercentage - The percentage of decay to apply.
  74.  */
  75. function Decay(range, decayPercentage) {
  76.   const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  77.   const selectedRange =
  78.     typeof range === "string" ? sheet.getRange(range) : range;
  79.   const values = selectedRange.getValues();
  80.   const decayFactor = 1 - decayPercentage / 100;
  81.   if (values.length > CONFIG.chunkSize) {
  82.     processInChunks_(selectedRange, values, decayFactor, CONFIG.chunkSize);
  83.   } else {
  84.     applyDecay_(values, decayFactor);
  85.     selectedRange.setValues(values);
  86.   }
  87.   SpreadsheetApp.flush();
  88. }
  89. const processInChunks_ = (range, values, decayFactor, chunkSize) => {
  90.   for (let start = 0; start < values.length; start += chunkSize) {
  91.     const chunk = values.slice(start, start + chunkSize);
  92.     applyDecay_(chunk, decayFactor);
  93.     range.offset(start, 0, chunk.length, 1).setValues(chunk);
  94.   }
  95. };
  96. const applyDecay_ = (values, decayFactor) => {
  97.   values.forEach((row) => {
  98.     if (typeof row[0] === "number") row[0] *= decayFactor;
  99.   });
  100. };
  101.  
Advertisement
Add Comment
Please, Sign In to add comment