4

Below is a script I am using for a Google Docs Spreadsheet.

These links show what I am doing:

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

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

How can I set up a "flag" so that when I run this script a second time, it doesn't add the perviously added stock items?

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet1 = ss.getSheetByName("Purchase Orders");
  var sheet2 = ss.getSheetByName("Inventory");
  var data = sheet1.getDataRange();
  var i_data = sheet2.getDataRange();
  var lastRow = data.getLastRow();
  var iLastRow = i_data.getLastRow();

  for (i=1;i<=lastRow;i++) {
    if (data.getCell(i, 5).getValue() == "stock"){
      for (n=1;n<=iLastRow;n++){
        if (data.getCell(i,3).getValue() == i_data.getCell(n,3).getValue()) {
        i_data.getCell(n, 1).setValue(i_data.getCell(n,1).getValue() + data.getCell(i,2).getValue());
       }
      }
    }

  }
}​

I guess I'm trying to do this: Once the item has been added to inventory, the script adds an x to column i of that line. Then when the script is run again, it skips over the lines with an x in column i

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
David
  • 285
  • 1
  • 4
  • 18

2 Answers2

3

Designate a cell to hold the flag value, and have the script check that particular cell for the flag value.

  • So how would I set it up to do this. Once the item has been added to inventory, the script adds an x to column i of that line. Then when the script is run again, it skips over the lines with an x in column i – David Dec 14 '10 at 02:45
  • You've probably already solved this, but anywho... The X in column I will solve your problem perfectly. You want to store the flag that indicates that each row has been processed in the spreadsheet itself, so that you don't lose that information when you close the sheet. In addition, you (or some other human) might want to know whether a particular row has been processed. – Lang Martin Jan 03 '11 at 17:32
2

In JavaScript, functions are objects. Objects have properties. So, decorate your function:

function myFunction() {
    if (!myFunction.alreadyDoneRunIt) {
        alert('bapt uia');
        myFunction.alreadyDoneRunIt = true;
    }
}

for (var i = 0; i < 10; i++) {
    myFunction(); // alerts once
}