r/GoogleAppsScript • • Jul 08 '25

Question Why my code is so slow?

I am building a habit tracker, but is slow!

Is there something built not-optimized in the code or it is just because my spreadsheet is too big and has too many calculations being triggered in background after each checkbox is added?

Here is a screen-recording of script running: https://www.loom.com/share/5224942dab6e40b887f9cc0f2139063e?sid=ec92725d-596f-4d29-b1e7-77f113157301

Code is triggered after user inputs the days in which he wants to control his habits; script then adds checkboxes on desired days. User can also use shortcuts: "s" for all days, "du" for workdays and "fds" for weekends.

Previously, the process was so slow that 30s timeout was always hitted when all days was choosen. Then I optmized the spreadsheet, and now it is running faster, but it is far from user friendly, as you can see on the video.

Any sugestions of how can I improve performance? Thanks in advance!

function onEdit(e) {
  const sheet = e.source.getActiveSheet();
  const range = e.range;
   
  if (sheet && range.getColumn() === 16 && range.getRow() >= 24 && range.getRow() <= 43) {
    procesarFrecuenciaDias(sheet, range);
  } else if (sheet.getName() === "Metas" && range.getColumn() === 38) {
    const allSheets = e.source.getSheets();
    copiaFrequenciasMeta(sheet, range, allSheets);
  } else if (sheet.getName() === "Setup" && range.getA1Notation() === 'B42') {
    atualizarAbas();
  }
}

function procesarFrecuenciaDias(sheet, range) {
  const row = range.getRow();
  const checkRow = sheet.getRange(`X${row}:BB${row}`);
  checkRow.removeCheckboxes();

  const value = range.getValue();
  const dayRow = sheet.getRange("X22:BB22").getValues()[0];
  const numberRow = sheet.getRange("X23:BB23").getValues()[0];

  switch (value) {

      case 's': {
        dayRow.forEach((_, colIndex) => {
          if (!isNaN(numberRow[colIndex]) && numberRow[colIndex] !== "") {
          checkRow.getCell(1, colIndex + 1).insertCheckboxes();
          }
        });
      return;
      }
      case 'du': {
          const selectedDays = ["seg.", "ter.", "qua.", "qui.","sex."];    

          dayRow.forEach((day, colIndex) => {
            if (selectedDays.includes(day) && !isNaN(numberRow[colIndex]) && numberRow[colIndex] !== "") {
              checkRow.getCell(1, colIndex + 1).insertCheckboxes();
            }
          });
      return;
      }
      case 'fds': {
        const selectedDays = ["sáb.", "dom."];
        dayRow.forEach((day, colIndex) => {
          if (selectedDays.includes(day) && !isNaN(numberRow[colIndex]) && numberRow[colIndex] !== "") {
            checkRow.getCell(1, colIndex + 1).insertCheckboxes();
          }
        });
      return;
      }
      default:{
        const selectedNumbers = value
        .split(",")
        .map(num => parseInt(num.trim(), 10));
        const daysOfWeek = ["dom.", "seg.", "ter.", "qua.", "qui.", "sex.", "sáb."];  
        const selectedDays = selectedNumbers.map(num => daysOfWeek[num - 1]);
        dayRow.forEach((day, colIndex) => {
          if (selectedDays.includes(day) && !isNaN(numberRow[colIndex]) && numberRow[colIndex] !== "") {
            checkRow.getCell(1, colIndex + 1).insertCheckboxes();
          }
        });
        return;
      }
  }
}
5 Upvotes

9 comments sorted by

View all comments

Show parent comment

2

u/microbitewebsites Jul 08 '25

100% correct. My point was that on edit will be constantly be calling the function before the previous function would have time to finish, adding unnecessary slow down.