Answer the question
In order to leave comments, you need to log in
How to subtract 1 unit of value when a cell matches in different sheets of the same Google spreadsheet?
I make a system for accounting for goods and sales. I made a Google form when selling a product, where the manager selects a product from the list. All data from google form gets into "Sheet1". Also in this table there is "Sheet2" taking into account the goods.
In "Sheet1" there are columns "time", "manager", "product", "Synchronization". In "Sheet2" there are columns "Product", "Quantity".
I need to put an onEdit trigger so that the following actions are performed:
1. When editing the "product" line, it was checked whether the "Synchronization" line was filled. If something is written there, then do nothing. This means that the function has already been executed.
2. If it is empty, then take the value from the "Product" column (of this format "
If you find such a value, then from the column "Quantity of goods" in the same line you need to subtract 1 unit of goods and write it to "Sheet1" in the column "Synchronization" - Synchronized so that the function is not repeated again for this line.
I am ready to thank Qiwi for help.
Answer the question
In order to leave comments, you need to log in
It would be helpful to see an example table.
upd:
function myFunction() {
var s = SpreadsheetApp.getActiveSpreadsheet();
var s1 = s.getSheetByName('Лист1');
var s2 = s.getSheetByName('Лист2');
var vals1 = s1.getRange(1,1,s1.getLastRow(),s1.getLastColumn()).getValues();
var vals2 = s2.getRange(1,1,s2.getLastRow(),s2.getLastColumn()).getValues();
for(var i=0;i<vals1.length;i++){
if(vals1[i][2] != '' && vals1[i][3] == ''){
for(var j=0;j<vals2.length;j++){
if(vals2[j][0] == vals1[i][2]){
var new_count = vals2[j][1] - 1;
s2.getRange(j+1,2).setValue(new_count);
}
}
s1.getRange(i+1,4).setValue('Синхронизировано');
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question