Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @constant {Object} CONFIG
- * @property {boolean} useCustomMenu - Set to true to use the custom menu for user input for range and decay amount.
- * @property {number} decayPercentage - The default percentage of decay to apply.
- * @property {number} chunkSize - The maximum number of rows to process in a single batch.
- */
- const CONFIG = {
- useCustomMenu: false,
- decayPercentage: 10,
- chunkSize: 1000
- };
- /**
- * Adds a custom menu to the Google Sheet when opened.
- */
- function onOpen() {
- const ui = SpreadsheetApp.getUi();
- ui.createMenu("Decay Operations")
- .addItem("Apply Decay", "startDecay_")
- .addToUi();
- }
- /**
- * Starts the decay process based on the configuration.
- */
- function startDecay_() {
- if (CONFIG.useCustomMenu) {
- showDecayDialog_();
- } else {
- const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
- const range = sheet.getActiveRange();
- Decay(range, CONFIG.decayPercentage);
- }
- }
- /**
- * Shows a dialog to the user to input the range and decay amount.
- */
- function showDecayDialog_() {
- const ui = SpreadsheetApp.getUi();
- const rangeResponse = ui.prompt(
- "Decay Operation",
- "Enter the range (e.g., A1:A100) to apply decay:",
- ui.ButtonSet.OK_CANCEL
- );
- if (rangeResponse.getSelectedButton() == ui.Button.CANCEL) return;
- const decayResponse = ui.prompt(
- "Decay Operation",
- "Enter the decay percentage (e.g., 10 for 10%):",
- ui.ButtonSet.OK_CANCEL
- );
- if (decayResponse.getSelectedButton() == ui.Button.CANCEL) return;
- const range = rangeResponse.getResponseText();
- const decayPercentage = parseFloat(decayResponse.getResponseText());
- if (
- isNaN(decayPercentage) ||
- decayPercentage <= 0 ||
- decayPercentage >= 100
- ) {
- ui.alert(
- "Invalid decay percentage. Please enter a number between 1 and 99."
- );
- return;
- }
- Decay(range, decayPercentage);
- }
- /**
- * Applies decay to the values in the specified range of the Google Sheet.
- * Processes the data in chunks if the dataset is larger than the configured chunk size.
- *
- * @param {string|GoogleAppsScript.Spreadsheet.Range} range - The range to decay, can be A1 notation or a Range object.
- * @param {number} decayPercentage - The percentage of decay to apply.
- */
- function Decay(range, decayPercentage) {
- const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
- const selectedRange =
- typeof range === "string" ? sheet.getRange(range) : range;
- const values = selectedRange.getValues();
- const decayFactor = 1 - decayPercentage / 100;
- if (values.length > CONFIG.chunkSize) {
- processInChunks_(selectedRange, values, decayFactor, CONFIG.chunkSize);
- } else {
- applyDecay_(values, decayFactor);
- selectedRange.setValues(values);
- }
- SpreadsheetApp.flush();
- }
- const processInChunks_ = (range, values, decayFactor, chunkSize) => {
- for (let start = 0; start < values.length; start += chunkSize) {
- const chunk = values.slice(start, start + chunkSize);
- applyDecay_(chunk, decayFactor);
- range.offset(start, 0, chunk.length, 1).setValues(chunk);
- }
- };
- const applyDecay_ = (values, decayFactor) => {
- values.forEach((row) => {
- if (typeof row[0] === "number") row[0] *= decayFactor;
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment