Pregunta

A continuación se muestra un script que estoy usando una hoja de cálculo de Google Docs.

Estos enlaces muestran lo que estoy haciendo:

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

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

¿Cómo puedo configurar una "bandera" de modo que cuando corro este script por segunda vez, no se le añade los artículos disponibles añadido perviously?

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());
       }
      }
    }

  }
}​

Creo que estoy tratando de hacer esto: Una vez que el artículo ha sido añadido al inventario, el script agrega una X en la columna i de esa línea. Luego, cuando la secuencia de comandos se ejecuta de nuevo, se salta sobre las líneas con una x en la columna i

¿Fue útil?

Solución

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
}

Otros consejos

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

Add the values to an array first. Each time you add an item check the array to see if it's already in it. Then loop your array an post to the UI.

Here's a post to help with checking if the array already contains the value. stackoverflow.com/questions/237104/

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top