Question

I'm trying to write a simple script for a Google Docs Spreadsheet. The two links below are a screenshot of the sheets I'm writing the script for.

http://i.stack.imgur.com/uGik7.png

http://i.stack.imgur.com/AbKnQ.png

On Sheet Purchase orders, If column e == "stock" then I need it to find the part number and adjust the quantity in sheet "Inventory". Bellow is a beginning to what I am trying to do. Can anyone give me a hand?

function updateInventoryFromPurchaseOrders() {
  var purchaseOrders = {}; // A mapping of partNumber => quantity.
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Purchase Orders');
  if (sheet) {
    // For each row, if column "E" == "stock" then set partNumber, quantity.
    purchaseOrders[partNumber] = quantity;
  }
  // Now purchaseOrders should look like {'SL249':5, 'ML50':1, 'MWF':1}

  sheet = ss.getSheetByName('Inventory');
  if (sheet) {
    // For each row, set quantity, partNumber.
    var purchased = purchaseOrders[partNumber];
    // Set column "A" value = quantity + purchased
  }
}

Thanks for the help.

Was it helpful?

Solution

javascript does not feature foreach - there are possible way to get around this, including implementing the mechanism yourself.

If it is a onetime Fix and you will migrate to some other language soon - i would suggest the following:

for( var CurrentRow=0; CurrentRow < DetermineMaxRows(); CurrentRow++ ) {
    if(isColumnEEqualStock()){
        //Things you want to do
    }
}

Something simmilar for iterating through Rows and Columns...

If you want to implement your own foreach take a look at this site.

If you plan to expand your Javascript efforts consider link text.

Otherwise think about making appletts in Java and implementing them instead of a script.

OTHER TIPS

If your flag for stock, and the quantity are in the same sheet, you could get the entire content in the same array (used .getDataRange(), to just grab everything), then when you find your in stock content, you can just grab the next index in that row.

var partNumbers = sheet.getDataRange().getValues(); 

for(num in partNumbers){
  if(partNumbers[num][4] == 'stock'){ //
    // Assuming quantity is in column F
    purchaseOrders[partNumber] = partNumbers[num][5];
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top