开发者

How do I loop through a column until I find a certain number?

开发者_如何学JAVAI'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.


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.


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];
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜