0

I have this AppsScript code. I would like to store the data from the variables "groups" and "selectedSubGroups" in the global variable "globalVariable", but right now my code is just overwriting the content that was previously in the variable "globalVariable". Could someone tell me what is wrong? Thank you very much in advance!

function insertSubGroups() {
  // Obtener la hoja de cálculo y las dos hojas que necesitamos
  var spreadsheet = SpreadsheetApp.getActive();
  var calculosSheet = spreadsheet.getSheetByName("Calculos");
  var resultadosSheet = spreadsheet.getSheetByName("Resultados");
  resultadosSheet.getRange(8, 1, resultadosSheet.getLastRow() - 7, 1).removeCheckboxes();
  resultadosSheet.getRange(8, 2, resultadosSheet.getLastRow() - 7, 1).clear();

  // Obtener los datos de la hoja "Calculos"
  var groups = calculosSheet.getRange("A2:A196").getValues();
  var subGroups = calculosSheet.getRange("C3:C202").getValues();

  // Obtener el valor del desplegable en la celda B6 de la hoja "Resultados"
  var group = resultadosSheet.getRange("B6").getValue();

  // Iterar a través del arreglo "groups" para encontrar el índice del grupo específico
  var groupIndex = -1;
  for (var i = 0; i < groups.length; i++) {
    if (groups[i] == group) {
      groupIndex = i;
      break;
    }
  }

  // Si se ha encontrado el índice del grupo, iterar a través del arreglo "subGroups" y recopilar los subgrupos que pertenecen al grupo específico
  if (groupIndex != -1) {
    var subGroupList = [];
    for (var i = groupIndex; i < subGroups.length; i++) {
      if (subGroups[i] == "") {
        break;
      }
      subGroupList.push(subGroups[i]);
    }
    // Mostrar los subgrupos en la hoja "Resultados" a partir de la celda B8
    resultadosSheet.getRange(8, 2, subGroupList.length, 1).setValues(subGroupList.map(function(subGroup) {
      return [subGroup];
    }));
    // Añadir un checkbox a la izquierda de cada subgrupo
    var checkboxRange = resultadosSheet.getRange(8, 1, subGroupList.length, 1);
    checkboxRange.setDataValidation(SpreadsheetApp.newDataValidation().setAllowInvalid(false).requireCheckbox().build());
  }
}

var globalVariable = [];


function saveSubGroups() {
  // Declarar la variable para almacenar la información del grupo y subgrupos seleccionados
  var selectedData = [];

  // Obtener la hoja de cálculo y las dos hojas que necesitamos
  var spreadsheet = SpreadsheetApp.getActive();
  var resultadosSheet = spreadsheet.getSheetByName("Resultados");

  // Obtener el valor del desplegable en la celda B6 de la hoja "Resultados"
  var group = resultadosSheet.getRange("B6").getValue();

  // Recopilar todos los subgrupos que estén marcados con el checkbox
  var subGroupValues = resultadosSheet.getRange(8, 2, resultadosSheet.getLastRow() - 7, 1).getValues();
  var subGroupCheckboxes = resultadosSheet.getRange(8, 1, resultadosSheet.getLastRow() - 7, 1).getValues();
  var selectedSubGroups = [];
  for (var i = 0; i < subGroupValues.length; i++) {
    if (subGroupCheckboxes[i][0]) {
      selectedSubGroups.push(subGroupValues[i][0]);
    }
  }

  // Guardar la información del grupo y subgrupos seleccionados en la variable selectedData
  selectedData.push({
    group: group,
    subGroups: selectedSubGroups
  });

  // Guardar la información del grupo y subgrupos seleccionados en la variable globalVariable
  globalVariable.push({
    group: group,
    selectedSubGroups: selectedSubGroups
  });
  
  // Mostrar por consola la información de la globalVariable
  for (var i = 0; i < globalVariable.length; i++) {
    console.log(globalVariable[i].group + ": " + globalVariable[i].selectedSubGroups.join(", "));
  }

}

I've been doing some research on "push", but no matter how much I've tried I can't seem to fully understand it.

  • https://stackoverflow.com/questions/65150194/how-to-use-global-variables-while-avoiding-permission-errors – Cooper Dec 28 '22 at 18:32

1 Answers1

1

Apps Script global variables are separately managed for each runtime instance. The globalVariable array will accumulate values pushed to it and retain them only as long the script is running. When it exits, the globalVariable array gets erased, and when you run the script again, it will be initialized as an empty array.

To retain values between script invocations, use the Properties Service or the CacheService.

doubleunary
  • 13,842
  • 3
  • 18
  • 51