Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
Improving efficiency of adding a column to an array in a loop?

So I wrote some code that goes to a book's ID based on a "dictionary" and takes the data from all the sheets in that book after the second sheet. That data is then aggregated into a single array. My question centers around the idea of improving the for loop in the if statement. That is adding the "supplier name" to the front of the subarray that represents a row in the sheet/data. I was told that this is inefficient as the code has to go through each subarray and add a value.

Is there a more efficient way of doing this? I did it this way because I am copying the values of a range which are stored as an array of arrays. So to add data, I have to reaccess the subarrays. Is it possible to add the new data (supplier name) at the same time that the values are being copied? would this be more efficient? It was recommended to use an arrow function, however, I am not familiar with their usage.

function aggregate() {
   var combinedData = []
   var idArray = {
   "suppliername":"id",
   };
for (var supplierName in idArray){
   var sBook = SpreadsheetApp.openById(idArray[supplierName]);
   var sheets = sBook.getSheets();

   for (var index = 2; index <sheets.length; index++){
      var sheet = sheets[index];
      var dataLength = sheet.getRange("E5:E").getValues().filter(String).length;

      if(dataLength != 0){
         var dataRange = sheet.getRange(5,2,dataLength,14);
         var dataValues = dataRange.getValues();
      
         for (row in dataValues) {
           dataValues[row].unshift(supplierName);
         };
         combinedData = combinedData.concat(dataValues);
      };
   };
};

var dataLength = combinedData.length;
const dSheet = SpreadsheetApp.openById("id").getSheets()[0];
dSheet.getRange(2,1,dSheet.getMaxRows(),dSheet.getMaxColumns()).clearContent();
var dRange = dSheet.getRange(2,1,dataLength,15);
dRange.setValues(combinedData);
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In your script, how about the following modification?

From:

for (row in dataValues) {
  dataValues[row].unshift(supplierName);
};
combinedData = combinedData.concat(dataValues);

To:

combinedData = combinedData.concat(dataValues.map(e => [supplierName].concat(e)));

or

combinedData = [...combinedData, ...dataValues.map(e => [supplierName, ...e])];

References:

  • map()
  • Spread syntax
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda