【发布时间】:2021-04-29 13:48:50
【问题描述】:
我希望这里有人可以提供帮助。我们一直在尝试创建一个包含所有产品的动态库存列表,每个产品都有一个 SKU 和单位尺寸。大多数按钮现在都可以使用,但我们仍然缺少 INStock 和 OUTStock 表中 UPDATE 按钮的脚本。
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