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

317
Vistas
How can I use arrays to make this code faster?

When I click "Add Item," I want the code to run and display the information in the table below the form.

function AddItem() {

  //DEFINE ALL ACTIVE SHEETS`
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  //DEFINE MENU SHEET  `        
  var poSheet = ss.getSheetByName("POF");
  var itemSheet = ss.getSheetByName("Products");

  `//GET NEXT ROW OF PO SHEET`
  var lastrowPO = poSheet.getLastRow() + 1;

  `//GET LAST ROW OF ITEM SHEET`
  var lastrowItem = itemSheet.getLastRow();

  `// GET VALUE OF PART AND QUANTITY`
  var part = poSheet.getRange('B15').getValue();
  var quantity = poSheet.getRange('B17').getValue();

  ` // GET UNIT PRICE FROM ITEM SHEET`
  for (var i = 2; i <= lastrowItem; i++) {
    if (part == itemSheet.getRange(i, 3).getValue()) {
      var part = itemSheet.getRange(i, 3).getValue();
      var unitCost = itemSheet.getRange(i, 5).getValue();
      var prodcode = itemSheet.getRange(i, 2).getValue();
    } else if (part == itemSheet.getRange(i, 2).getValue()) {
      var part = itemSheet.getRange(i, 3).getValue();
      var unitCost = itemSheet.getRange(i, 5).getValue();
      var prodcode = itemSheet.getRange(i, 2).getValue();
    }
  };

  // POPULATE PO SHEET
  poSheet.getRange(lastrowPO, 1).setValue(prodcode);
  poSheet.getRange(lastrowPO, 2).setValue(part);
  poSheet.getRange(lastrowPO, 3).setValue(quantity);
  poSheet.getRange(lastrowPO, 4).setValue(unitCost).setNumberFormat("#,###.00");

};
  1. I use this code to match text from sheet1 and grab a row of data from sheet2 matching that text and bring back data to sheet 1.
  2. It is taking a lot of time to run.
  3. I want to make it run faster. Can someone please guide me .
  4. I am an amateur coder.
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Description

I'm not able to test this but basically what I did was change your getValue/setValue to getValues/setValues. This typically improves performance dramatically.

Some parts of your logic I didn't quite understand but tried to duplicate your logic as is. For example, you change prodcode to part in the else block. And rather than continue looping once I've found a match I break out of the loop.

Using the items array row 1 of the spreadsheet is the first index 0 of the array. Likewise column 1 of the spreadsheet is the second index 0 of the array.

Code.gs

function AddItem() {

  //DEFINE ALL ACTIVE SHEETS`
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  //DEFINE MENU SHEET  `        
  var poSheet = ss.getSheetByName("POF");
  var itemSheet = ss.getSheetByName("Products");

  //GET NEXT ROW OF PO SHEET`
  var lastrowPO = poSheet.getLastRow() + 1;

  //GET LAST ROW OF ITEM SHEET`
  var lastrowItem = itemSheet.getLastRow();

  // GET VALUE OF PART AND QUANTITY`
  var part = poSheet.getRange('B15').getValue();
  var quantity = poSheet.getRange('B17').getValue();

  // Use getValues to get all data
  var items = itemSheet.getDataRange().getValues();

  // GET UNIT PRICE FROM ITEM SHEET
  // Here I'm assuming you want from row 2 to the last row
  // And I'm assuming you want the first occurance of part
  for( var i = 1; i < items.length; i++ ) {
    if( part == items[i][2] ) {
      var unitCost = items[i][4];
      var prodcode = items[i][1];
      break;
    } 
    else if( part == items[i][1] ) {
      var prodcode = part;
      part = items[i][2];
      var unitCost = items[i][4];
      break;
    }
  };

  // POPULATE PO SHEET
  // To use setValues it must pass a 2D array of 1 row
  poSheet.getRange(lastrowPO,1,1,4).setValues([[prodcode,part,quantity,unitCost]]);
  poSheet.getRange(lastrowPO, 4).setNumberFormat("#,###.00");

};

Reference

  • Best Practices
  • Range.getValues()
  • Range.setValues()
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