How to write a script in Google Apps script?

Lads, Hey!
Help to write a script.
I'm not a programmer, not once, but smoke Google, I was able to do that
function myFunction() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var code1 = ss.getActiveSheet().getRange("A3").getValue();
 var code2 = ss.getActiveSheet().getRange("M4").getValue();
 var value1 = ss.getActiveSheet().getRange("E3").getValue();
 var value2 = ss.getActiveSheet().getRange("N4").getValue();

 if (code1==code2){
 var sum = value1 + value2;
 var rr = ss.getActiveSheet().getRange("E3").setValue(sum);
}


}


All that is written here, made for one cell.
It checks the cell M4, if this cell coincides with A3 that performs the summation of E3 and N4
A3 - this is the product code in table
M4 is a product code which I enter by hand
E3 - the number of items in stock
N4 - number of product that it is and I enter it by hand.

Actually, as I said above, it all works with a single cell.
But I need it to work for each cell separately. For example I enter the code "2345" it was this cell, for example it will be A45 and found her, but E45 has made the amount of E45 and N4.
April 3rd 20 at 18:36
1 answer
April 3rd 20 at 18:38
Solution
To the data cell by cell is very long. It is better to take all the data at once in the desired range, for example:
var data = ss.getActiveSheet().getRange("A3:A1000").getValues();

And then you can access that data by row number and column number, not forgetting that the count goes from zero:
data[row][column]
Record, too, need massively. The end result is something like this:
function onOpen() { 
 //Executed when the opening
SpreadsheetApp
.getUi()
.createMenu('Menu')
.addItem('Run','doIt')
.addToUi();
};

function doIt() {
try{
 var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); //Current sheet

 var data1 = ss.getRange("A3:E" + ss.getMaxRows()).getValues();
 var data2 = ss
 .getRange("m4:n" + ss.getMaxRows())
.getValues()
 .filter(function(row) // Remove empty strings
{
 return row[0]!="";
}
);

 for (var row2=0;row2<data2.length;row2++){
 for (var row1=0;row1<data1.length;row1++){
 if (data1[row1][0]===data2[row2][0]){
data1[row1][4]+=data2[row2][1];
 break; //If the value is found, no further searching
};
};
};

 ss.getRange("A3:E" + ss.getMaxRows()).setValues(data1); //Output data
SpreadsheetApp.getActive().toast("Done!");
 }catch(e){ //If the error
 Logger.log("Error! "+ e);
};
}

Demo table
Hello!
Thank YOU so much! The desired functions are performed, but there is one thing.
I can't put the script on the button, because the tables say that the function was not found.
When I enter the function doIt, the script is running, but no matter what I typed in the product code, even if it's not existing code, the script runs and says "Finished". Although I understand it needs to write error.
And the menu is also not created, as I understand it, their first function is to create some kind of menu. Can I something not so do? Please help. - valentine.Brekke64 commented on April 3rd 20 at 18:41
onOpen() you can try to run first time from the code editor - Google will ask for permission.

If the article does not exist, just this pair of cycles will be wasted and nothing will change, in theory.

You can add a condition, if there is such a need:
function doIt() {
 var isModifed = false;
try{
 var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); //Current sheet

 var data1 = ss.getRange("A3:E" + ss.getMaxRows()).getValues();
 var data2 = ss
 .getRange("m4:n" + ss.getMaxRows())
.getValues()
 .filter(function(row) // Remove empty strings
{
 return row[0]!="";
}
);

 for (var row2=0;row2<data2.length;row2++){
 for (var row1=0;row1<data1.length;row1++){
 if (data1[row1][0]===data2[row2][0]){
data1[row1][4]+=data2[row2][1];
 isModifed = true;
 break; //If the value is found, no further searching
};
};
};
 if (isModifed ) {
 ss.getRange("A3:E" + ss.getMaxRows()).setValues(data1); //Output data
SpreadsheetApp.getActive().toast("Done!");
}else{
 SpreadsheetApp.getActive().toast("These articles not found!");
};
 }catch(e){ //If the error
 Logger.log("Error! "+ e);
};
}
- amira_Runolfsson commented on April 3rd 20 at 18:44
@amira_Runolfsson, Super! Thank you so much! Big, big, big thank you, everything works as it should! :) - valentine.Brekke64 commented on April 3rd 20 at 18:47

Find more questions by tags Google Apps ScriptGoogle Sheets