title: How can I use arrays to make this code faster?
body: <p>When I click &quot;<a href="https://i.stack.imgur.com/0jmZM.png" rel="nofollow noreferrer">Add Item</a>,&quot; I want the code to run and display the information in the table below the form.</p>
  <pre><code>function AddItem() {
  
    //DEFINE ALL ACTIVE SHEETS`
    var ss = SpreadsheetApp.getActiveSpreadsheet();
  
    //DEFINE MENU SHEET  `        
    var poSheet = ss.getSheetByName(&quot;POF&quot;);
    var itemSheet = ss.getSheetByName(&quot;Products&quot;);
  
    `//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 &lt;= 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(&quot;#,###.00&quot;);
  
  };
  </code></pre>
  <ol>
  <li>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.</li>
  <li>It is taking a lot of time to run.</li>
  <li>I want to make it run faster. Can someone please guide me .</li>
  <li>I am an amateur coder.</li>
  </ol>