Can we create databars in google spreasheet using Google Apps Script? - google-apps-script

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.

Related

google sheet apps script truncate column value

I have the following table:
I am simply trying to write a Google apps script to insert into column E everything in column B prior to '.Upload'. So in the table column E = 20ba4a5c.
I think I should be able to use the split() function to do that but I'm having some difficulty.
ss.getRange('E'+lastRow).setFormula('SPLIT(B'+lastRow+'.Upload')[0]');
You should use REGEXEXTRACT to build a simple regex in order to
achieve your goal.
It is also a better practice to use template literals when dealing with multiple concatenations.
Solution:
Replace:
setFormula('SPLIT(B'+lastRow+'.Upload')[0]');
with:
setFormula( `REGEXEXTRACT(B${lastRow},"^(.*?).Upload")`)
Output:

"Currentregion" concept in Google Sheets script language

From inside Google Sheets Script, I am trying to name a variable-sized matrix on the worksheet. In EXCEL VBA, I would go to the top left most cell and select the whole matrix using activecell.currentregion.select.
This would select the whole matrix (e.g. D5:L50) on the worksheet, which I could then name.
Is there the same ability in Google Sheets script language. If not, can anyone figure out how to do this?
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('D5:L50').activate();
you can use macro under:
Tools > Macros > Record macro > and then make your selection > save macro > edit macro
SpreadsheetApp.Range
getDataRegion():
Returns a copy of the range expanded in the four cardinal Directions to cover all
adjacent cells with data in them. If the range is surrounded by empty cells not including those
along the diagonals, the range itself is returned. This is similar to selecting the range and
typing Ctrl+A in the editor.
The question is: how do you refer to a range which contains values and most importantly, the range may change as user add or remove contents.
In Excel VBA, this is dealt with a built-in function called "currentregion". I'm also curious if there is a equivalent in GS.

Google Sheets formula that includes values from another cell

In Google Sheets, I have a formula in a cell that connects to an API with a script and spits out the JSON results. For example, I can connect to the Youtube API with a formula that looks like this,
=ImportJSON("https://www.googleapis.com/youtube/v3/videos?id=mv-cj6mBkPk&key=API KEY&fields=items(id,snippet(channelId,title,categoryId),statistics)&part=snippet,statistics")
I'd like to make part of that formula get it's value from a separate cell. For example, the video ID above (mv-cj6mBkPk), rather than have it typed in the above formula, I'd like to pull in the value from a cell (i.e. B1). Then I can create multiple versions of this formula with other video IDs (i.e B2, B3, etc.)
I've tried combining various parts of the formula through CONCATENATE-ing a few cells, but that doesn't seem to 'RUN' the formula, it just shows it.
My skills aren't so advanced in this area, so any help would be great. Thanks!!
It is not obvious what your problem is exactly, or where you got the code for the ImportJSON() function (as this is not a built-in function),
but a variation of the below SHOULD work - if it does not, then post what error you are getting (exactly).
Also, I presume you have substituted "API KEY" for the actual key... I have used API_KEY, so that the formula does not get broken across multiple lines here.
A1 : mv-cj6mBkPk
A2 : =ImportJSON("https://www.googleapis.com/youtube/v3/videos?id="&A1&"&key=API_KEY&fields=items(id,snippet(channelId,title,categoryId),statistics)&part=snippet,statistics")

How to use a formula written as a string in another cell [evaluate for Google Spreadsheet] [duplicate]

This question already has answers here:
Is there a way to evaluate a formula that is stored in a cell?
(13 answers)
Closed last month.
I read several old posts about Google Spreadsheet missing the evaluate function.
There is any solution in 2016?
The easiest example.
'A1' contains the following string: UNIQUE(C1:C5)
'B1' I want to evaluate in it the unique formula written in 'A1'.
I've tried concatenating in this way: 'B1' containing ="="&A1 but the outcome is the string =UNIQUE(C1:C5).
I've also tried the indirect formula.
Any suggestion to break last hopes, please?
Additional note
The aim is to write formulas in a spreadsheet and use these formulas by several other spreadsheets. Therefore, any change has to be done in one place.
Short answer
Use a script that includes something like var formula = origin.getValue() to get the string and something like destination.setFormula(formula) to return the formula.
Explanation
As was already mentioned by the OP, Google Sheets doesn't have a EVALUATE() built-in function. A custom function can't be used because custom functions can only return one or multiple values but can't modify other cell properties.
A script triggered by a custom menu, events or from the Google Apps Script editor could be used to update the formulas of the specified cells.
Since the formulas will be kept as strings, it could be more easy to keep them in the script rather than in the spreadsheet itself.
Example
The following is a very simple script that adds the specified formula to the active range.
function addFormula() {
var formula = '=UNIQUE(C1:C5)';
var range = SpreadsheetApp.getActiveRange();
range.setFormula(formula);
}
I have a solution for my own use case. My investment broker exports data to its users in (badly-formatted) Excel. I do my own analysis in Google Sheets. I have found copy/pasting entire sheets of data to be accident-prone.
I have partially automated updating each tab of the records. In the sheet where I maintain all the records, the First tab is named "Summary"
Save the broker's .xlsx data to Google Sheets (File | Save as Google Sheets);
In the tab named Summary, enter into a cell, say "Summary!A1" the URL of this Google Sheet;
In cell A2 enter: =Char(34)&","&CHAR(34)&"Balances!A1:L5"&Char(34)&")"
In the next tab, enter in cell A1: ="IMPORTRANGE("&Char(34)&Summary!A1&Summary!A2
The leading double quote ensures that the entry is saved as a text string.
Select and copy this text string
in cell A3, type an initial "=" + Paste Special.
This will produce an importrange of the desired text, starting at cell A3

If or conditional formula formula in Google apps script

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","")))))))');