importJSON and concatenate function not working together - json

I am trying to use concatentate to create the input parameters with a variable for the importJSON function. The concatenated string looks and works fine if I copy it into the importJSON function but if I reference the string as part of the function or use concatenate in the function I get an invalid argument error.
Here is the concatenate string. Cell I7 is the date. For example 2017-11-27.
=concatenate("""https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo""",",","""","/Time Series (Daily)/",I7,"/4. close""", ", ","""noHeaders""")
Here is the output string with the date variable:
"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo","/Time Series (Daily)/2017-11-27/4. close", "noHeaders"
This works:
=importJSON("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo","/Time Series (Daily)/2017-11-27/4. close", "noHeaders")
These dont work:
=importJSON(concatenate("""https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo""",",","""","/Time Series (Daily)/",I7,"/4. close""", ", ","""noHeaders"""))
=importJSON(K9) >> K9 is cell where the concatenate function is

The issue was with the extra set of quotes. When referencing a cell or using a formula within the importJSON function, quotes are not needed.

Related

Index was outside the bounds of the array in SSRS

I have two parameters , let's say P1 and P2. The sample expression I used for P2 is
IIF(P1.Label="string", "null" ,Split(P1.Label," ").GetValue(0))
When the condition is false, the split expression is working fine. But if the condition is true, I'm getting 'Index was outside the bounds of the array' error. If the condition is true, I need to pass the value "null" as varchar type.
Can someone please advice on this?
The problem with the IIF function is that it is a function not a language construct. This means that it evaluates both parameters before passing the parameters to the function. Consequently, if you are trying to do a Split on a parameter that can't be split, you will still get the 'Index was outside the bounds of the array' error, even when it looks like that code shouldn't be executed due to boolean condition of the IIF statement.
The best way to solve this is to create a safe string splitter function in custom code where you can use real language constructs. Also, check that the string is splittable by checking it contains a space instead of checking for a special string:
Public Function SafeSplit(ByVal SplitThis As String) As String
If InStr(SplitThis, " ") Then
Return Split(SplitThis, " ")(0)
End If
Return "null"
End Function
and then use this in your report for the Value expression instead of IIF:
=Code.SafeSplit(Parameters!P1.Label)

How to pass a cell value or a function to the data of QUERY command in google spreadsheets

How can you pass a specific cell value or output of custom function (that returns string) as the "data" to a QUERY command?
For example instead of writing the A2:E6 in the following command:
QUERY(A2:E6,"select avg(A) pivot B")
I want to do something like this:
QUERY(A1,"select avg(A) pivot B")
Where A1 is reference to cell A1 which holds A2:E6 as the value.
For some reason QUERY command does not like the A1 there. But when you replace the A1 with the actual string value everything works just fine.
You can use the INDIRECT function
=QUERY(INDIRECT(A1), "select avg(A) pivot B")
Which is exactly for this kind of use case.
There is even one for going the other way, ADDRESS, from cell address to string.
Something like this:
function loadFormula() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('Sheet1');//you may need to change sheet name
const rg=sh.getRange('A1');
const s = rg.getDisplayValue();//I used this method because it returns a strong
const fs= '=QUERY(' + s + ',\"select avg(A) pivot B\")';
sh.getRange(?).setFormula(fs);
}

Fetching data in range to process client-side (Google Sheets)

Using Google Scripts I'm trying to return a string and I get an array.
The code is adapted from another answer: Find Cell Matching Value And Return Rownumber
function updateValues(sheet)
{ // loads named range "CalendarSettings" (on Reserved tab) into array dataSearch for later searching
dataRangeSearch = sheet.getRange("CalendarSettings");
dataSearch = dataRangeSearch.getValues().reduce(function (a, b) {return a.concat(b);});;
}
I was expecting that dataSearch would be a string, since I thought that a.concat(b) would result in a string.
"CalendarSettings" is a named range. Here is a screenshot of what the named range contains.
And here's a screenshot of a msgBox showing the contents of the variable dataSearch.
This looks like an array to me, and it behaves like an array in terms of how it is accessed. But what confuses me is that a.concat(b) I thought would be a string.
Strings and Arrays in JavaScript both have .concat() methods - using .concat() on two arrays will return an array, and on two strings will return a string.
You can turn an array into a string with .toString():
dataSearch = dataRangeSearch.getValues().reduce(function (a, b) {return a.concat(b).toString();});;

Use Split, Join, and another function in SSRS

I have a field in SQL Server that contains an comma separated list. Here are 2 examples:
select 'ex1,ex2,ex3' as str union all
select 'ax1,ax2'
In my report, I have to transform all of these values (5 in this case) using a function. In this question I will use Trim, but in actuality we are using another custom made function with the same scope.
I know how I can split every value from the string and recombine them:
=Join(Split(Fields!str.Value,","),", ")
This works great. However, I need to execute a function before I recombine the values. I thought that this would work:
=Join( Trim(Split(Fields!VRN.Value,",")) ,", ")
However, this just gives me an error:
Value of type '1-dimensional array of String' cannot be converted to 'String'. (rsCompilerErrorInExpression)
I can't personally change the function that we use.
How do I use an extra function when dealing with both an split and a join?
You can use custom code to include all the logic (Split->Custom Code->Join).
Make adjustments inside the loop to call your custom function instead of trim
Public Function fixString (ByVal s As String) As String
Dim mystring() As String
mystring = s.Split(",")
For index As Integer = 0 To mystring.Length-1
mystring(index) = Trim(mystring(index))
Next
Return Join(mystring, ",")
End Function
To call the custom code use the following expression
Code.fixString( Fields!VRN.Value )

Regarding Extract Function

i have query related to extract function, will Extract Function accept ASCII Value?
The $EXTRACT function when given a string of ASCII characters, will return a substring of that string.
for example $EXTRACT("Hello World",1,6) is "Hello"
If you want to extract the ASCII value of a character in a string, use the function $ASCII.
If you want to create a string given the ASCII values, use the function $CHAR.
For example $EXTRACT("ABC",2) is "B" and $ASCII("ABC",2) is the number 66
and the function $CHAR(67,65,66) is the string "CAB".