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.

I'm still learning how to write JavaScript but this is what I'm trying to do. I hope this makes sense. Can anyone help me with this?

get.sheetbyname "Purchase Orders"
    If column E  == "stock"
    var qty == three cells left of cell e
    var partNum == two cells left of cell e

get.sheetbyname "Parts inventory"
    loop through column C until you find partNum
    var oldQty == number(two cells left of partNum)
    add qty to oldQty to get new qty

Was it helpful?

Solution

You'll definitely need to get familiar with JavaScript quickly. It's a powerful language and seems easy to pick up but has many differences from other popular, seemingly related languages (e.g. Java, C, etc). Once you're comfortable with JavaScript, you'll want to familiarize yourself with the Google Apps Script Spreadsheet Services APIs. These APIs allow you to script Google Docs spreadsheets like you would other spreadsheet applications, such as Microsoft Excel. Start by opening your workbook and browsing to "Tools > Scripts > Script Editor...", then creating a new named script.

Here's a start at what you're trying to do specifically. It looks like you're trying to find out how many purchase orders exist for some item and update the quantity in the inventory sheet.

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
  }
}

Reading and writing the values from individual cells will require you to use Ranges, the docs linked above should give you enough examples to get you the rest of the way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top