IF statement with variable in google Apps Script - if-statement

I am trying to make a google spreadsheet document with some buttons in it for a few colleagues. The colleagues can indicate which silo they're working with by pressing a certain button. In this case, it is either 1, 2, 3 or 4. This value is then stored in a cell. Afterward, they can keep track of their production by pressing a plus or minus button every time they have produced a pallet of material. The amount of pallets produced is stored in a cell (in this case 'M4:M5'). Now I want to keep track of the number of pallets produced from each silo. So when they have selected silo 1, I want the number of pallets produced stored in a cell next to silo 1. When they select a different silo, I want to keep track of the pallets produced from that silo. I hope you kinda get what I am talking about.
I want to do this by using an If statement. So if silo 1 is selected, and they press the plus button because a pallet has been produced, I want a pallet to be added to a certain cell I made for silo 1. I tried to do this by storing the selected silo number as "temp" and then verifying it with an "if" statement. If it matches a certain silo number, the right cell will have a +1 added to it. The problem is that Apps Script is telling me that "temp" is unused in my code.
I am totally new to this coding language. Can someone please help me?
function PlusHVBB() {
var spreadsheet = SpreadsheetApp.getActive();
var tempHVBB = spreadsheet.getRange('M4:M5').getValue();
spreadsheet.getRange('M4:M5').setValue(tempHVBB+1);
var temp = spreadsheet.getRange('P4:P5').getValue();
if (temp = 1){
spreadsheet.getRange('C29').setValue(tempHVBB+1);
}
};

The problem is that you use getValue() on a range that contains more than one cell and that your comparison operator is incorrect
The problem with your if condition is that temp = 1 does not compare the value of temp against 1, but rather assigns it the value 1. To compare it against 1 you need to use the == or === operator: if (temp == 1)
Furthermore:
'M4:M5' is a range that contains two cells
You need to either get (and set!) the value of each single cell indvidually:
var tempHVBB1 = spreadsheet.getRange('M4').getValue();
var tempHVBB2 = spreadsheet.getRange('M4').getValue();
Or you can use the methods getValues() and setValues() respectively to retrieve and set values from a range of cells. This will return you a nested array or values in the format [[row1/column1, row1/column2],[row2/column1, row2/column2]] or in your case: [['M4', 'M5']]

Related

In Libreoffice Calc, which formula will check if a a keyword or part of it is contained in a cell in a row and copy the entire content of that cell?

I am learning how to use formulas in spreadsheets, I do use libre office.
I need to sort out data in a quite huge messy spreadsheet.
Each column contains mixed data, the sheet is huge, dozens of columns and thousands of rows, if the spreadsheet does not contain errors each cell in a row either contains a different keyword or is empty, there should not be two cells in the same row containing the same keyword.
The problem to solve is to sort out all the data so to reach to have a new spreadsheet in which each cell marked with a given specific keyword is kept in the same position but placed in one column dedicated to that same keyword.
the kind of spreadsheet with mixed up cells to be sorted out
the data in the spreadsheet has to be fixed so to appear in this way
A formula that can be used to extract sorted out data from a cell is the following:
=IF(SEARCH("Text1";B2;1);B2;0)
The formula can be dragged to each cell below to hit the proper cell next to it. The result is correct.
The results are correct, but I do not know why the expected 0 is not printed, there is #VALUE! instead
The logic is very simple, if the cell contains the keyword or any other text that contains that keyword the result is the full content of that cell, otherwise the result is 0.
Here comes the first question, why do I get #VALUE! as a result for those cells that do not contain the keyword? I expected to get 0 instead, just as indicated in the formula,
I tried to leave this filed empty and also to put the 0 result in quotes, the actual result is always the same, #VALUE!...
However, of course this formula extracts only the information contained in one column, so for each other column the process must be repeated.
In order to avoid to create a column with the formula for each column in the spreadsheet or anyway to process each column one by one and more importantly to have then to merge all the results to form one columns containing only cells with a given keyword I thought to use the same formula extending the parsing to each next cell in the row as follows:
=IF(SEARCH("text";B2;1);B2;IF(SEARCH("text";C2;1);C2;IF(SEARCH("text";D2;1);D2;0)))
The logic is very simple and should output in one go a column containing all the cells containing the keyword that are found in the row, check if the first cell in the row contains a word using the search function, if does then the result is the content of that cell, otherwise perform the next test, the next test is the same, check if the next cell contains a certain word using the search function, if does then the result is the content of that cell, otherwise proceed to the next test…. and so on until last test, if no test gave a true result then print 0 (but we get #VALUE!, OK I could live with that...).
In theory should work for a any number of cells, but in the practice does not at all, in fact does work only for the first IF test and cell indicated in the formula.
WHY?
The result using the extended version of the formula to parse N cells in sequence is the same obtained with the simple formula to parse only one cell
Finally, how do I resolve this problem using IF and Search?
Is there any other better approach and way to solve this kind of problems and sort out data in huge spreadsheets of this kind?
Thank you for any hint and help.

Is there a way to check the value of the cell to the left and add two to the count, unless there is a value entered in another cell in the same row?

I am trying to write a piece of code in Google Sheets that automatically creates a follow-up date based on the date in the immediate left cell. It gets that date, and adds two (days) to it. Then that date gets pushed into Appsheet, where it is marked on a virtual calendar.
I also want the code to recognize when something is filled into the "Status" section (which is located in the 4th cell to the right) and fill in the current cell with something like "Done".
Currently, everything I have works except for that last part, when something is entered into the Status column I get an error that reads;
Function IF parameter 1 expects boolean values. But 'Declined' is a
text and cannot be coerced to a boolean.
I believe that means I need to convert the data in the status column into a boolean value, but I have no idea how to proceed in doing that. See below code;
=IF(INDIRECT("RC[4]",0), "Done", INDIRECT("RC[-1]",0) +2)
Let me know what you guys think, and thank you in advance for your help 👍
try:
=IF(INDIRECT("RC[4]",0)="Declined", "Done", INDIRECT("RC[-1]",0) +2)
or:
=IF(INDIRECT("RC[4]",0)<>"Declined", "Done", INDIRECT("RC[-1]",0) +2)

sumproduct if a cell contains a certain value, give all results otherwise just the specified result

My issue is that I need to reference a cell (A1) which will either be the name of a state that can be found in column L, or it can be "All States" which I then want to include all results of column L. I can't work out how to include this.
=SUMPRODUCT(--(IF(A1="All States",Data!$L:$L,Data!$L:$L=A1)),Data!Q:Q)
I want to add a bunch more criteria based on the above so I don’t want to go down the route of imbedding the sumproduct in an if function because the formula will quickly become too unweildy.
You have a lot of choices. Using your initial formula I would tweak it to
(A) =SUMPRODUCT((IF($A$1="All States",1,($L$2:$L$11=$A$1)))*($Q$2:$Q$11))
But this would need to be entered as an array formula so instead of just confirming with ENTER, you need CONTROL+SHIFT+ENTER. You will know you have done it right when { } show up around your formula. Note that they cannot be added manually.
A non array type formula which would be faster I believe would be to look at your two options. You are either dealing with a single state or all states. Set up an IF check to determine if you need to sum all of column Q, or if you need to find a single value from column Q. I used the following formula:
(B) =IF(A1="all states",SUM($Q$2:$Q$11),INDEX($Q$2:$Q$11,MATCH($A$1,$L$2:$L$11,0)))
A bit of a cheat but but simplifies things, is to add a final state to the bottom of your list in L and call is "All States". In the corresponding row in Q place =sum(First Cell:Last Cell). If you do that then you can use the following formula:
(C) =SUMPRODUCT(($L$2:$L$12=$A$1)*($Q$2:$Q$12))
That are other options out there as well, just thought I would show some options.

Calculate ever expanding number of columns with data to the right

Currently have a spreadsheet that tracks attendance. First column is name, second column is attendance % and contains the formula I need to revise, subsequent columns simply have an X or O in them and denote whether someone attended or not (headers for these columns are dates).
Currently using a COUNTIF() I can check how many X's there are and then the formula is SUM(100/no_of_columns*COUNTIF(A3:A12))
Ideally I want to firstly replace no_of_columns with the actual number of columns with data to the right.
I've thought about replacing this with a SUM(COUNTIF('X')+COUNTIF('O')) but it seems pretty messy?
Secondly I want to replace the A12 with whatever the last column value is.
I could just make the last column a very high column value, but again feels messy and would like to know if there is a better way...
Example: https://docs.google.com/spreadsheets/d/1rjnUQP7V-U1EZTp3Z8yO7HybBCuQjf2y4LJ4Dv4ctF8/edit?usp=sharing
Presume you only have the attendance dates in Row 1 without other information such as headers for Column A and B,
Put the following formula in Cell B2 and drag it down,
=COUNTIF(INDEX(OFFSET($C2,,,,COUNTA($1:$1)),),"x")/COUNTA($1:$1)*100
The logic is to use INDEX + OFFSET function to dynamically return the range of columns on the right, and use COUNTA to find out how many dates are there, and you should understand the use of COUNTIF, the calculation is self-explanatory.
EDIT #2
After looking into your worksheet, I guess you are adding the new dates by inserting columns between B and C so you probably want to use the following formula in Cell B2 instead to avoid the system shifting the starting cell reference automatically:
=COUNTIF(INDEX(OFFSET($B2,,1,,COUNTA($1:$1)),),"x")/COUNTA($1:$1)*100
The logic is the same as the previous one but just a little change to the OFFSET references so it starts looking for the range from Column B instead of C.
I have tested the above in both Excel and Google-sheets working just fine. Let me know if you have any questions. Cheers :)
paste in B2:
=ARRAYFORMULA(IFERROR(IF(LEN(A2:A),
MMULT(IF(INDIRECT("C2:"&ADDRESS(ROWS(A2:A), MAX(IF(1:1<>"", COLUMN(1:1), ))))="x", 1, 0),
TRANSPOSE(COLUMN(INDIRECT("C2:"&ADDRESS(ROWS(A2:A), MAX(IF(1:1<>"", COLUMN(1:1), )))))^0))/
MMULT(IF(INDIRECT("C2:"&ADDRESS(ROWS(A2:A), MAX(IF(1:1<>"", COLUMN(1:1), ))))<>"", 1, 0),
TRANSPOSE(COLUMN(INDIRECT("C2:"&ADDRESS(ROWS(A2:A), MAX(IF(1:1<>"", COLUMN(1:1), )))))^0))*100, ), 0))
spreadsheet demo

Applying a formula to all cells in a column, not just one at a time?

In OpenOffice.org Calc, I would like to apply a formula to a column that references a cell from the same row but in a different column. I.e., =C1*48 in cell D1, but I want all cells in column D to do this without having to copy the formula to each one manually. Or another way of saying it, for each cell in C that has a number, I want to fill in the corresponding row-cell in D with a formula value based on that number. So far, all I can see from the documentation is that I'd have to copy/adjust the formula for every new row in the spreadsheet. And since I have 4 such columns that need calculation, this will get to be tiresome. I have little experience with spreadsheets at all, so I'm hoping that my problem is actually very simple, and that I just am looking at the wrong parts of the documentation.
I don't have OpenOffice in front of me, but it tries really hard to be Excel like in many ways, so usually assumptions about Calc based on Excel are fairly close to reality.
In Excel, a formula in cell D1, that points to a cell in C1, is treated as a relative reference - that is, one column back from where I am now. So when that formula is filled into other cells (either by Fill Down, or dragging the little handle in the corner of the cell outline, or by copy-pasting the formula into a range of selected cells) the new formulas are similarly treated, by default, as referring to the cell that is one column back from them, in the same row.
To force a formula to use an absolute reference, one specifies the cell address with a dollar sign - $C$1 will always point to (use) the contents of cell C1, regardless of where the formula ends up. ($C1 and C$1 are alternates that allow one parameter of the address to change in the usual relative sense while fixing the other half in place... this probably isn't important to you yet).
In other words, I'd expect that you can type the formulas in the first row of your OpenOffice Calc sheet and copy them down to the rest of the row, and things will just work.
If you want to extend your range down you can do this by calculating the first 2 cells with your formula and then highlighting them. You now grab the little square on the bottom right of the highlighted area and drag that down, across or both.
If you have a specific cell (e.g. D2) which you wish to have remain in all the cells you extend your range to, then in your 2 initial cell calculations use the following:
Extending Down
=G2*D$2
=G3*D$2
Extending Across
=G2*$D2
=H2*$D2
Extending Down and Across
Use $D$2
From your question it seems that you are trying to apply the same formula on whole row (or column) of cells and show the result on another row (or column).
The best way to do this is to use something called array formulas or array functions (in libre office). This link has very good explanation - https://help.libreoffice.org/Calc/Array_Functions
The way you would implement this in your case is
Type this formula in cell D1
=C1:C30 *48
and press ctrl + shift + enter
The formula now looks like this with the flower braces {..}
={C1:C30 *48}
and the formula gets applied all the way from D1 to D30.
You can even extrapolate the same concept to a whole matrix (for example: A1:C10)
(The way to tell excel/open office/ libre office that you wrote an array formula is to press ctrl + shift + enter. Never press enter as that will break the array function and convert it to a regular function)