Google Sheets - Equivalent of a FOR loop - google-sheets

A1 = 8
A2 = 14
I don't know if an equivalent for this exists in Google Sheets but what I am trying to do is that for every multiple of 5 in A1 I want to subtract 5 off of A2 and make it one string in A3. So in this example A3 = 14/9 And if A1 = 12; A3 = 14/9/4 This should even go into the negatives in the case of A1 = 16; A3 = 14/9/4/-1
Ultimately I would like to avoid using the API/Script system if possible. Right now the only way I can think of doing it is a ton of IF() handlers.
This is the formula I am currently using, but understandably it's a mess and stops working when the IF()'s run out.
=A2&
IF(A1>5,
"/"&A2-5&
IF(A1>10,
"/"&A2-10&
IF(A1>15,
"/"&A2-15&
IF(A1>20,
"/"&A2-20
, "")
, "")
, "")
, "")

=ARRAYFORMULA(QUERY(IF(ROW(INDIRECT("A1:A"&QUOTIENT(A1, 5)+1)),
MMULT(TRANSPOSE(( ROW(INDIRECT("A2:A"&QUOTIENT(A1, 5)+2))<=
TRANSPOSE( ROW(INDIRECT("A2:A"&QUOTIENT(A1, 5)+2))))*
{A2; TRANSPOSE(SPLIT(REPT(5*-1&"♦", QUOTIENT(A1, 5)), "♦"))}),
SIGN({A2; TRANSPOSE(SPLIT(REPT(5*-1&"♦", QUOTIENT(A1, 5)), "♦"))})^2),
IFERROR(1/0)), "limit "&QUOTIENT(A1, 5)&" offset 1"))

try it like this:
=A2-QUOTIENT(A1, 5)*5

=IFERROR(ARRAYFORMULA(TO_TEXT(JOIN("/",
IF(ROW(INDIRECT("A1:A"&QUOTIENT(A1, 5)+1)),
MMULT(TRANSPOSE(( ROW(INDIRECT("A2:A"&QUOTIENT(A1, 5)+2))<=
TRANSPOSE( ROW(INDIRECT("A2:A"&QUOTIENT(A1, 5)+2))))*
{A2; TRANSPOSE(SPLIT(REPT(5*-1&"♣", QUOTIENT(A1, 5)), "♣"))}),
SIGN({A2; TRANSPOSE(SPLIT(REPT(5*-1&"♣", QUOTIENT(A1, 5)), "♣"))})^2),
IFERROR(1/0))))))

Related

How to ArrayFormula in Google Sheets with multiple IF AND OR conditions?

Can someone please help me?
I can't get seems to work it.
What else should I do?
Here is my code
=ARRAYFORMULA(IF(AND(Y2:Y="VUL",L2:L="ANNUAL"),V2:V*0.03,IF(AND(Y2:Y="VUL",L2:L="QUARTERLY"),V2:V>0,IF(AND(Y2:Y="VUL",L2:L="SEMI ANNUAL"),V2:V*AB2:AB,"0"))))
The results must be from 2nd row to last but only works only for 2nd row.
Use ifs(), like this:
=arrayformula(
ifs(
Y2:Y <> "vul", iferror(1/0),
L2:L = "annual", V2:V * 0.03,
L2:L = "quarterly", V2:V > 0,
L2:L = "semi annual", V2:V * AB2:AB,
true, iferror(1/0)
)
)
use:
=ARRAYFORMULA(IF((Y2:Y="VUL")*(L2:L="ANNUAL"), V2:V*0.03,
IF((Y2:Y="VUL")*(L2:L="QUARTERLY"), V2:V>0,
IF((Y2:Y="VUL")*(L2:L="SEMI ANNUAL"), V2:V*AB2:AB, "0"))))
AND is *
OR is +
Got the rights answer
thank you so much guys
=ARRAYFORMULA(IF((Y2:Y="VUL")*(L2:L="ANNUAL"),V2:V*0.03,
IF((Y2:Y="VUL")*(L2:L="QUARTERLY")*(V2:V>0), V2:V*AB2:AB,
IF((Y2:Y="VUL")*(L2:L="SEMI ANNUAL")*(V2:V>0), V2:V*AB2:AB, "0"))))

Adding blank rows in a set of data in Google Sheets

I have a set of data. What i am looking forwards is to add 2 blank rows after each set of 3 values like this
Hope to get help in getting this solved.
you can find the sample google sheet here : https://docs.google.com/spreadsheets/d/11nMvUWn3xcTfxlk4v30KruPr03HSheMk1jrxZPpJ_p4/edit?usp=sharing
Thanks
Shijilal
Solution:
IF it's the third row, Add 3 bunnies separated by a space, else keep the values as it is
JOIN them all and SPLIT by a bunny and TRANSPOSE
Sample:
=ARRAYFORMULA(TRANSPOSE(SPLIT(TEXTJOIN("🐇",1,IF(MOD(ROW(A2:A16),3)=1,A2:A16&REPT("🐇 ",3),A2:A16)),"🐇")))
Some time ago, I created this custom function that may help you. I changed it slightly to meet your requirement and added it to the script editor.
function rowsBetween(range, s, rowsWithData, text) {
var n = [],
a = [],
i = 0;
while (i < s) {
a.push(text
)
i++;
}
range.forEach(function(r, i) {
n.push(r);
if((i + 2) % rowsWithData == 1) {
a.forEach(function(x) {
n.push(x);
});
}
});
return n;
}
This script will allow you to enter in the spreadsheet this (custom) formula (see also cell E2)
=rowsBetween(A2:A16, 2, 12,)
See if that works for you?

Can't get a certain formula into sheet using script editor google sheets

Hi all I am haveing a problem geting a formula into google sheet using script editor. I can get one in that doesn´t work but not one that does.
This one will put the formula in but the formula doesn´t work
function x1() {
var ss3 = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
ss3.getRange("AF2").setFormula("=IF((AND(OR(I2='YOUR TRIP DWL',I2='MEGA PACK DWL (YT + AA + BONUS)'),L2<=0,AD2<>'')),'Send Email', 'Wait')")
var lr3 = ss3. getLastRow();
var filldownrange3 = ss3.getRange(2, 32, lr3-1);
ss3. getRange("AF2").copyTo(filldownrange3);
}
This one shows an Error in script editor but the furmula work in the cells i manually placed in.
function x1() {
var ss3 = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
ss3.getRange("AF2").setFormula("=IF(((AND(OR(I2="YOUR TRIP DWL",I2="MEGA PACK DWL (YT + AA + BONUS)"),M2<=0,AA2<>"",AE2<>""))), "Send Email", "Wait")")
var lr3 = ss3. getLastRow();
var filldownrange3 = ss3.getRange(2, 32, lr3-1);
ss3. getRange("AF2").copyTo(filldownrange3);
The problem is Line 3 with the formula itself as other fumulas are ok, can anyone shed some light on this, Thanks in advance,
I suggest you try your second version with escaping of the internal double quotes.
IF this hels anyone else with the same problem, I have finally solved this problem by changing the double quotes surounding the formula with single quptes and leaving the double quotes inside the formula, so line 4 reads:
ss3.getRange("AF2").setFormula('=IF(((AND(OR(I2="YOUR TRIP DWL",I2="MEGA PACK DWL (YT + AA + BONUS)"),M2<=0,AA2<>"",AE2<>""))), "Send Email", "Wait")');

Find all occurences of exact string in range and list it

I want to create list of all occurences of "x" string in range. This is my sheet:
And I want to search all occurences and list them and give proper names:
For example for G2, I want "Beret Grey" string as result. I think that I need to use array formula or something like that.
Let me first preface this that vba would be much more robust, but this formula will get you there. It may be slow as it is an array type formula and is doing a lot of calculations. These calculations only expound exponentially as the number of cells with them in it increases:
=IFERROR(INDEX(A:A,AGGREGATE(15,6,ROW($B$2:$G$7)/($B$2:$G$7="x"),ROW(1:1))) & " " & INDEX($1:$1,AGGREGATE(15,6,COLUMN(INDEX(A:G,AGGREGATE(15,6,ROW($B$2:$G$7)/($B$2:$G$7="x"),ROW(1:1)),0))/(INDEX(A:G,AGGREGATE(15,6,ROW($B$2:$G$7)/($B$2:$G$7="x"),ROW(1:1)),0)="x"),ROW(1:1)-COUNTIF($B$1:INDEX(G:G,AGGREGATE(15,6,ROW($B$2:$G$7)/($B$2:$G$7="x"),ROW(1:1)) -1),"x"))),"")
You will need to expand the range to what you need. Change all the $B$2:$G$7 to $B$2:$N$29. Do not use full column references outside those that I have used. It will kill Excel.
Also note what is and what is not relative references, they need to remain the same or you will get errors as the formula is dragged/copied down.
As simple UDF to do what you want:
Function findMatch(rng As Range, crit As String, inst As Long) As String
Dim rngArr() As Variant
rngArr = rng.Value
Dim i&, j&, k&
k = 0
If k > Application.WorksheetFunction.CountIf(rng, crit) Then
findMatch = ""
Exit Function
End If
For i = LBound(rngArr, 1) + 1 To UBound(rngArr, 1)
For j = LBound(rngArr, 2) + 1 To UBound(rngArr, 2)
If rngArr(i, j) = crit Then
k = k + 1
If k = inst Then
findMatch = rngArr(i, 1) & " " & rngArr(1, j)
Exit Function
End If
End If
Next j
Next i
then you would call it like this:
=findMatch($A$1:$G$7,"x",ROW(1:1))
And drag/copy down.

Is XLConnect in R is vectorized for writeNamedRegion()? What is the syntax for it?

I want to write to NamedRegions in multiple sheets in an excel workbook using XLConnect from R using vectorization, if possible.
I have a list:
trya
$grp1
var1 var2 var3
1455100 459 21 459
$grp2
var1 var2 var3
936710 463 20 463
I have a workbook template (wb1) containing a worksheet (Sheet1) that contains namedRegions (each a single cell). The namedRegions in Sheet1 are named : "varn1","varn2","varn3"
I can clone Sheet1 to create separate sheets to contain the data for each grp in trya.
cloneSheet(wb1, sheet="Sheet1", name = names(trya))
But..how do I write to the NamedRegions in these sheets? I am happy to use 3 separate passes, one for each NamedRegion to write to the multiple worksheets.
Here is my start at the code for NamedRegion "varn1: using code
example is writeNamedRegion(object,data,name of region,header,rownames)
writeNamedRegion(wb1, trya??? ,name = "varn1",header = FALSE)
I don't know
1. how to generically pick off the elements of trya like: trya[[1]]$var1?
2. how to loop over the cloned sheets, named.... names(trya)?
Any help will be appreciated.
Jan
Many of XLConnect's functions/methods are vectorized. This also holds for writeNamedRegion. The following example illustrates how to write a list of data.frame's to appropriate named regions using a single writeNamedRegion call:
require(XLConnect)
wb = loadWorkbook("test.xlsx", create = TRUE)
mySheets = paste0("MySheet", 1:3)
myNames = paste0("MyName", 1:3)
myData = list(mtcars, CO2, USArrests)
createSheet(wb, name = mySheets)
createName(wb, name = myNames, formula = paste(mySheets, "$A$1", sep = "!"))
writeNamedRegion(wb, data = myData, name = myNames)
saveWorkbook(wb)

Resources