【问题标题】:Find Product with SKU number and ADD or SUBTRACT from Inventory Stock查找具有 SKU 编号的产品并从库存中添加或减去
【发布时间】:2021-04-29 13:48:50
【问题描述】:

我希望这里有人可以提供帮助。我们一直在尝试创建一个包含所有产品的动态库存列表,每个产品都有一个 SKU 和单位尺寸。大多数按钮现在都可以使用,但我们仍然缺少 INStock 和 OUTStock 表中 UPDATE 按钮的脚本。

An example of a clear OUTStock sheet, how it should look after clicking update and having all the products sent/added to inventory list

Example of products put into INStock sheet

Inventory sheet, where products are to be added/subtracted from

基本上,一旦单击 INStock 中的更新,所有列出的商品都将通过库存表中各自的 SKU 代码找到并添加到当前库存中。 INStock E 列上的盒子将被添加到库存表 C 列中的各自产品中,并且各个瓶子将从 INStock 表的 F 列转移到库存表 D 列中的各自产品中。

另一方面,OUTStock 是从地窖中取出的任何库存,因此是相同的概念,但不是添加到前面所说的列中,而是从中减去。

在此过程之后,来自 INStock/OUTStock 的 B2:B30 和 E2:F30 列需要清除其内容。 我遇到过很多问题,人们对按钮有类似的概念,但没有人查找特定产品来添加数量。如果有人能提供正确的功能,那就太棒了。

非常感谢您的宝贵时间!

更新: 感谢@Irvin 的帮助。据我了解,我已经添加了脚本并修改了 outStock 函数,但是我在减法方面遇到了一些问题。请参阅所附照片以获取更多信息。我还包含了该函数的日志执行:

Initial Stock count of Las Rocas (14 bottles)

outStock update removing 2 bottles from inventory

Too many bottles removed and put into negative

function outStock(outstock, inventory){ //Function for OUTStock sheet data
  var skus = [];
  var boxes = [];
  var bottles = [];

  for(var x = 2; x <= outstock.getDataRange().getLastRow();x++){ //Get all SKUs,Boxes and Bottles from OUTStock sheet
    skus.push(outstock.getRange(x,1).getValue());
    boxes.push(outstock.getRange(x,5).getValue());
    bottles.push(outstock.getRange(x,6).getValue());
  }

  for (var y = 3; y <= inventory.getDataRange().getLastRow(); y++){ //Check Inventory sheet by SKU code from OUTStock
      skus.forEach(res => {
        if(res == inventory.getRange(y,1).getValue()){
          var boxUpdate = boxes[skus.indexOf(res)] - inventory.getRange(y,3).getValue(); //ADD the sum of the box value from OUTStock and BOX cell value on Inventory to array boxUpdate
          var bottleUpdate = bottles[skus.indexOf(res)] - inventory.getRange(y,4).getValue(); //ADD the sum of the bottle value from OUTStock and INDIVIDUAL cell value on Inventory to array bottleUpdate
          inventory.getRange(y,3).setValue(boxUpdate); //Update BOX column cell value on Inventory sheet 
          inventory.getRange(y,4).setValue(bottleUpdate); //Update INDIVIDUAL column cell value on Inventory sheet
          outstock.getRange("B2:B"+outstock.getDataRange().getLastRow()).clearContent(); //Clears all contents on B2:B(the last row your sheet has)
          outstock.getRange("E2:F"+outstock.getDataRange().getLastRow()).clearContent(); //Clears all contents on E2:F(the last row your sheet has)
          Logger.log("Found Match for OUTStock SKU code "+res+" on Inventory sheet at row #" + y+"\n==OUTSTOCK DATA==\nSKU: "+res+"\nBox: "+boxes[skus.indexOf(res)]+"\nBottle: "+bottles[skus.indexOf(res)]+"\n==UPDATED INVENTORY==\nCell C"+y+" was updated to "+boxUpdate+"\nCell D"+y+" was updated to "+bottleUpdate);
        }
      });
  }
  Logger.log(" Done Updating SKU codes: " + skus + " on Inventory sheet");
}

除此之外,它似乎工作得很好。请参阅下面给出的照片示例的执行日志:

As can be seen, update to -12 instead of 12

我希望我清楚问题所在。再次感谢!

更新 2: 至于脚本,我将其全部放在一个文件中,保持相同的主要功能不变(更新),并为进货和出库按钮分配相同的更新功能(见图)这是完整的脚本:

Example of function assigned to OUTStock Update button

function update() { //Main function
  var sheet = SpreadsheetApp.getActive();
  var instock = sheet.getSheetByName('INStock');
  var outstock = sheet.getSheetByName('OUTStock');
  var inventory = sheet.getSheetByName('Inventory');

  if(sheet.getSheetName() == "INStock"){ 
    Logger.log("Run update for INStock sheet"); //Logs the result for review
    inStock(instock, inventory); //Runs the function for INStock
  }else if(sheet.getSheetName() == "OUTStock"){
    Logger.log("Run update for OUTStock sheet"); //Logs the result for review
    outStock(outstock, inventory); //Runs the function for OUTStock
  }else{ //IF statement if you run test your code from the script editor
    Logger.log("Please select either INStock or OUTStock sheet on your spreadsheet"); //Logs the result for review
  }
}

function inStock(instock, inventory){ //Function for INStock sheet data
  var skus = [];
  var boxes = [];
  var bottles = [];

  for(var x = 2; x <= instock.getDataRange().getLastRow();x++){ //Get all SKUs,Boxes and Bottles from INStock sheet
    skus.push(instock.getRange(x,1).getValue());
    boxes.push(instock.getRange(x,5).getValue());
    bottles.push(instock.getRange(x,6).getValue());
  }

  for (var y = 3; y <= inventory.getDataRange().getLastRow(); y++){ //Check Inventory sheet by SKU code from INStock
      skus.forEach(res => {
        if(res == inventory.getRange(y,1).getValue()){
          var boxUpdate = boxes[skus.indexOf(res)] + inventory.getRange(y,3).getValue(); //ADD the sum of the box value from INStock and BOX cell value on Inventory to array boxUpdate
          var bottleUpdate = bottles[skus.indexOf(res)] + inventory.getRange(y,4).getValue(); //ADD the sum of the bottle value from INStock and INDIVIDUAL cell value on Inventory to array bottleUpdate
          inventory.getRange(y,3).setValue(boxUpdate); //Update BOX column cell value on Inventory sheet 
          inventory.getRange(y,4).setValue(bottleUpdate); //Update INDIVIDUAL column cell value on Inventory sheet
          instock.getRange("B2:B"+instock.getDataRange().getLastRow()).clearContent(); //Clears all contents on B2:B(the last row your sheet has)
          instock.getRange("E2:F"+instock.getDataRange().getLastRow()).clearContent(); //Clears all contents on E2:F(the last row your sheet has)
          Logger.log("Found Match for INStock SKU code "+res+" on Inventory sheet at row #" + y+"\n==INSTOCK DATA==\nSKU: "+res+"\nBox: "+boxes[skus.indexOf(res)]+"\nBottle: "+bottles[skus.indexOf(res)]+"\n==UPDATED INVENTORY==\nCell C"+y+" was updated to "+boxUpdate+"\nCell D"+y+" was updated to "+bottleUpdate);
        }
      });
  }
  Logger.log(" Done Updating SKU codes: " + skus + " on Inventory sheet");
}

function outStock(outstock, inventory){ //Function for OUTStock sheet data
  var skus = [];
  var boxes = [];
  var bottles = [];

  for(var x = 2; x <= outstock.getDataRange().getLastRow();x++){ //Get all SKUs,Boxes and Bottles from OUTStock sheet
    skus.push(outstock.getRange(x,1).getValue());
    boxes.push(outstock.getRange(x,5).getValue());
    bottles.push(outstock.getRange(x,6).getValue());
  }

  for (var y = 3; y <= inventory.getDataRange().getLastRow(); y++){ //Check Inventory sheet by SKU code from OUTStock
      skus.forEach(res => {
        if(res == inventory.getRange(y,1).getValue()){
          var boxUpdate = boxes[skus.indexOf(res)] - inventory.getRange(y,3).getValue(); //ADD the sum of the box value from OUTStock and BOX cell value on Inventory to array boxUpdate
          var bottleUpdate = bottles[skus.indexOf(res)] - inventory.getRange(y,4).getValue(); //ADD the sum of the bottle value from OUTStock and INDIVIDUAL cell value on Inventory to array bottleUpdate
          inventory.getRange(y,3).setValue(boxUpdate); //Update BOX column cell value on Inventory sheet 
          inventory.getRange(y,4).setValue(bottleUpdate); //Update INDIVIDUAL column cell value on Inventory sheet
          outstock.getRange("B2:B"+outstock.getDataRange().getLastRow()).clearContent(); //Clears all contents on B2:B(the last row your sheet has)
          outstock.getRange("E2:F"+outstock.getDataRange().getLastRow()).clearContent(); //Clears all contents on E2:F(the last row your sheet has)
          Logger.log("Found Match for OUTStock SKU code "+res+" on Inventory sheet at row #" + y+"\n==OUTSTOCK DATA==\nSKU: "+res+"\nBox: "+boxes[skus.indexOf(res)]+"\nBottle: "+bottles[skus.indexOf(res)]+"\n==UPDATED INVENTORY==\nCell C"+y+" was updated to "+boxUpdate+"\nCell D"+y+" was updated to "+bottleUpdate);
        }
      });
  }
  Logger.log(" Done Updating SKU codes: " + skus + " on Inventory sheet");
}

【问题讨论】:

    标签: google-apps-script google-sheets sum product add


    【解决方案1】:

    推荐的解决方案:

    根据您的陈述,您希望您的更新过程是这样的:

    1. 从 INStock/OUTstock 表中获取所有 SKU 代码

    2. 更新 Inventory 工作表的 BOX 列:

      INStock Box 值 +/- Inventory Box 与来自 INStock/OUTstock

      的匹配 SKU 代码的行的当前值
    3. 更新Inventory表的INDIVIDUAL列:

      INStock Bottle 值 +/- Inventory INDIVIDUAL 当前值,在与来自 INStock/OUTstock

      的匹配 SKU 代码的行上
    4. 清除 INStock/OUTstock 的 B:B 和 F:E 列的内容

    这样,我根据您附加的示例(经过测试的 INStock 表)创建了示例表,并在下面创建了一个脚本:

    INStock 工作表:

    库存表:

    脚本,您可以用作参考:

    /**
     * Custom Update funtion for INStock and OUTStock sheets
     */
    
    function update() { //Main function
      var sheet = SpreadsheetApp.getActive();
      var instock = sheet.getSheetByName('INStock');
      var outstock = sheet.getSheetByName('OUTStock');
      var inventory = sheet.getSheetByName('Inventory');
    
      if(sheet.getSheetName() == "INStock"){ 
        Logger.log("Run update for INStock sheet"); //Logs the result for review
        inStock(instock, inventory); //Runs the function for INStock
      }else if(sheet.getSheetName() == "OUTStock"){
        Logger.log("Run update for OUTStock sheet"); //Logs the result for review
        outStock(outstock, inventory); //Runs the function for OUTStock
      }else{ //IF statement if you run test your code from the script editor
        Logger.log("Please select either INStock or OUTStock sheet on your spreadsheet"); //Logs the result for review
      }
    }
    
    function inStock(instock, inventory){ //Function for INStock sheet data
      var skus = [];
      var boxes = [];
      var bottles = [];
    
      for(var x = 2; x <= instock.getDataRange().getLastRow();x++){ //Get all SKUs,Boxes and Bottles from INStock sheet
        skus.push(instock.getRange(x,1).getValue());
        boxes.push(instock.getRange(x,5).getValue());
        bottles.push(instock.getRange(x,6).getValue());
      }
    
      for (var y = 3; y <= inventory.getDataRange().getLastRow(); y++){ //Check Inventory sheet by SKU code from INStock
          skus.forEach(res => {
            if(res == inventory.getRange(y,1).getValue()){
              var boxUpdate = boxes[skus.indexOf(res)] + inventory.getRange(y,3).getValue(); //ADD the sum of the box value from INStock and BOX cell value on Inventory to array boxUpdate
              var bottleUpdate = bottles[skus.indexOf(res)] + inventory.getRange(y,4).getValue(); //ADD the sum of the bottle value from INStock and INDIVIDUAL cell value on Inventory to array bottleUpdate
              inventory.getRange(y,3).setValue(boxUpdate); //Update BOX column cell value on Inventory sheet 
              inventory.getRange(y,4).setValue(bottleUpdate); //Update INDIVIDUAL column cell value on Inventory sheet
              instock.getRange("B2:B"+instock.getDataRange().getLastRow()).clearContent(); //Clears all contents on B2:B(the last row your sheet has)
              instock.getRange("E2:F"+instock.getDataRange().getLastRow()).clearContent(); //Clears all contents on E2:F(the last row your sheet has)
              Logger.log("Found Match for INStock SKU code "+res+" on Inventory sheet at row #" + y+"\n==INSTOCK DATA==\nSKU: "+res+"\nBox: "+boxes[skus.indexOf(res)]+"\nBottle: "+bottles[skus.indexOf(res)]+"\n==UPDATED INVENTORY==\nCell C"+y+" was updated to "+boxUpdate+"\nCell D"+y+" was updated to "+bottleUpdate);
            }
          });
      }
      Logger.log(" Done Updating SKU codes: " + skus + " on Inventory sheet");
    }
    
    function outStock(outstock, inventory){ //Function for OUTStock sheet data
    var skus = [];
    var boxes = [];
    var bottles = [];
    
    for(var x = 2; x <= outstock.getDataRange().getLastRow();x++){ //Get all SKUs,Boxes and Bottles from OUTStock sheet
      skus.push(outstock.getRange(x,1).getValue());
      boxes.push(outstock.getRange(x,5).getValue());
      bottles.push(outstock.getRange(x,6).getValue());
    }
    
    for (var y = 3; y <= inventory.getDataRange().getLastRow(); y++){ //Check Inventory sheet by SKU code from OUTStock
        skus.forEach(res => {
          if(res == inventory.getRange(y,1).getValue()){
            var boxUpdate =  inventory.getRange(y,3).getValue() - boxes[skus.indexOf(res)]; //ADD the sum of the box value from OUTStock and BOX cell value on Inventory to array boxUpdate
            var bottleUpdate = inventory.getRange(y,4).getValue() - bottles[skus.indexOf(res)]; //ADD the sum of the bottle value from OUTStock and INDIVIDUAL cell value on Inventory to array bottleUpdate
            inventory.getRange(y,3).setValue(boxUpdate); //Update BOX column cell value on Inventory sheet 
            inventory.getRange(y,4).setValue(bottleUpdate); //Update INDIVIDUAL column cell value on Inventory sheet
            outstock.getRange("B2:B"+outstock.getDataRange().getLastRow()).clearContent(); //Clears all contents on B2:B(the last row your sheet has)
            outstock.getRange("E2:F"+outstock.getDataRange().getLastRow()).clearContent(); //Clears all contents on E2:F(the last row your sheet has)
            Logger.log("Found Match for OUTStock SKU code "+res+" on Inventory sheet at row #" + y+"\n==OUTSTOCK DATA==\nSKU: "+res+"\nBox: "+boxes[skus.indexOf(res)]+"\nBottle: "+bottles[skus.indexOf(res)]+"\n==UPDATED INVENTORY==\nCell C"+y+" was updated to "+boxUpdate+"\nCell D"+y+" was updated to "+bottleUpdate);
          }
        });
    }
    Logger.log(" Done Updating SKU codes: " + skus + " on Inventory sheet");
    }
    

    结果:

    update 自定义函数放在INStock 工作表上的更新按钮上以运行脚本后,结果如下:

    库存表上的结果:

    日志执行结果,您可以在其中查看脚本发生的情况:

    【讨论】:

    • 感谢@Irvin,请查看我遇到的唯一错误的更新。非常感谢您的帮助!
    • 惊人的@Irvin Jay G.,修改后的脚本完美运行!抱歉我的错误,整个 Google Script 场景的新手。再次感谢!
    • @Patxi Fernandez Coster,不客气。结果是 -12 的原因是框 (2) 已减去库存表 (2-14 = -12) 上的单个单元格数据 (14)。我已经根据我的答案更新了我的 OUTStock 函数,特别是在 var boxUpdate 和 var bottleUpdate 上,其中库存 INDIVIDUAL 数据从 OUTStock 的 Box 数据中减去。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多