I am trying to put conditional formula in spreadsheet using google apps script but i am not getting the way out to resolve this.
The VBA equivalent to the required GAS is:
Sub ageing()
Rem Ageing Slabs for ADVANCES
With Range("ap2:ap4000")
.Formula = "=IF(u2="""","""",if(u2<0,""Delivery Reported after
RC"",IF(AND(u2>=0,u2<31),""Ageing
0-30"",IF(AND(u2>=31,u2<=60),""Ageing
31-60"",IF(AND(u2>=61,u2<=90),""Ageing
61-90"",IF(AND(u2>=91,u2<=120),""Ageing
91-120"",IF(and(u2>120,u2<=180),""Ageing 121-180"",IF(u2>180,""More
than 6 months"",""""))))))))"
.Value = .Value
End With
End Sub
Kindly suggest the VBA equivalent to this in GAS.
Thanks
The same question posted on https://productforums.google.com/forum/#!category-topic/docs/spreadsheets/how-do-i/desktop/H2dXGR7arvU
You can use Range.setFormula() to set a formula in a cell.
SpreadsheetApp.getActiveRange().setFormula('=SUM(A1:A100)');
I think you can make it less complicated by not checking twice for each condition
In JS you can put single quotes and double quotes inside it, or " and \" inside.
You can use ArrayFormula for this type of tasks, and you can even eliminate Google Apps Script or VBA (it's just one formula in the end)
SpreadsheetApp.getRange("AP2").setFormula('IF(U2:U4000="","",IF(U2:4000<0,"Delivery Reported after RC",IF(U2:U4000<=30,"Ageing 0-30",IF(U2:U4000<=60,"Ageing 31-60",IF(U2:U4000<=90,"Ageing 61-90",IF(U2:U4000<=120,"Ageing 91-120",IF(U2:U4000<=180,"Ageing 121-180","More than 6 months","")))))))');
Related
I have these 2 columns in my sheet: A and B.
A contains dates in format day.month.year. B contains hours in format hh:mm. I usually concatenate the values of these 2 columns by using this formula:
=concatenate((text(A3;"mm.dd.yyyy")&" "&text(B3;"hh:mm")))
I use the same formula to concatenate the values of 2 other cells: C (filled with dates!) and D (filled with hours!). The exact formula in this case is:
=concatenate((text(C3;"mm.dd.yyyy")&" "&text(D3;"hh:mm")))
The whole purpose of this little exercise is to get the dates and hours in the format that is required to create events in my Google Calendar. I am currently using it in my calendar event creation script with the method setFormulaR1C1(formula), but I am not sure if that´s a really good idea. So, please tell me:
How would an apps script equivalent of my formula look like?
And is there a way to write that apps script equivalent of my formula as a callback function?
Thank you so much in advance!
How would an apps script equivalent of my formula look like?
Given that the cells already display the date and time in the format you need, you can simply use this:
const sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
const datetimeString =
sheet.getRange('A3').getDisplayValue()
+ ' '
+ sheet.getRange('B3').getDisplayValue();
If the cells do not display the date and time in the required format, you will need to use Utilities.formatDate() and Spreadsheet.getSpreadsheetTimeZone(). There are some complications when interpreting spreadsheet time values in Apps Script. Refer to the recipe at Google Script when taking time from sheets adds +1 minute to avoid surprises.
To get the same with a spreadsheet formula more easily, use this:
=trim(A3) & " " & trim(B3)
I have following formula
=UNIQUE(QUERY('Id match'!A2:B,"SELECT A,B WHERE B matches '"& TEXT(TEXTJOIN("|",1,FILTER(W3:W30,W3:W30<>"",W3:W30<>0)),0)&"' ",0))
That is Querying results if the item is contained in a list. So far so good. This function needs to constantly evaluate the input from Cell W3:W30. However as soon as I close the sheet, this continuation seems to stop & the Formula returns to the result "#N/A". Repasting the formula does the trick but I dont want to do this over and over.
It is worth mentioning that the query input is based on another query!
I read that SpreadsheetApp.flush() in an Apps Script might help, but for me it does not. Also I am a bit confused how I tell the script which spreadsheet to update.
Has anyone an idea to approach this issue and keep the formula working at all times
Excel Example - conditional formatting
No, but a crude alternative is mentioned here: Gsheets-databars
Use the formula function REPT to create a bar graph consisting of the pipe character "|", repeated a number of times based on the value in the adjoining cell.
=REPT("|", B1)
I searched and could not found any way to do it currently it not possible.
I have a large amount of data that reference cells. I want to lock these references without going into each cell to change the equation. I know there is a way to do a script in excel. I don't know if there is a way to create the same type of script in excel.
Those interested in the excel script:
Sub test()
Dim c As Range
For Each c In Selection
c.Formula = Application.ConvertFormula(c.Formula, xlA1, , xlAbsolute)
Next
End Sub
Thanks Guys!!
There is no Spreadsheet Service method that is equivalent to VBA's Application.ConvertFormula().
You could enter a feature request on the Google Apps Script Issue Tracker. If you do, add a comment here to let us know the issue number.
I'm a VBA newbie, but have successfully created a handful of useful Excel Functions. The one I'm working on right now, which seems like it should be simple, is eluding me. I think I'm misunderstanding the syntax, and need some guidance.
Consider the following screen capture; I am attempting to create the function in Column E, which is simply the VALUE from D$n.
So far, this is as far as I've gotten:
Function PASTVALUE(q As String)
q.PasteSpecial Paste:=xlPasteValues
End Function
which, if I understand properly, is reading the input value (in my case, the contents of cell D$n) as a String, then pasting it using PasteValues.
Do I need to somehow copy it before I paste it? I thought that the q As String parameter was what brought it into the function.
But then if I'm not copying, is it trying to paste from an empty clipboard...in which case I have no idea what I should be using to accomplish this.
Help!
You can just ''transfer'' the value(displayed) over like this
Function PASTEVALUE(rng As Range)
PASTEVALUE = rng.Text
End Function
or use the Evaluate() function to evaluate the formula in that range
Function PASTEVALUE(rng As Range)
PASTEVALUE = [rng]
End Function
There are some features of Excel's object model that you cannot access during a calculation; i.e. during the evaluation of a function. Broadly speaking they are the ones that manipulate the value in cells in some way. If the converse were true, then the calculation process in Excel would break.
Pasting clipboard data into the worksheet falls therefore into the forbidden category.
One solution would be to put your code in a macro; accessible by a button on the worksheet. That is outside the calculation cycle and therefore permissible.