I have made a GET request in VBA and I'm able to parse the first iteration of returned data into an excel sheet. However it will not start the second iteration.
A little more info: I'm using Shopify API to get all Orders. In Postman I can see multiple records for different orders (600+) in total. I can parse all the information I need from the first returned order however I can't loop to the second.
I am use VBA-JSON library available here: https://github.com/VBA-tools/VBA-JSON
I have posted my code below...
I can see that x is the total number of orders however increment V doesn't help me get onto the second order information.
Sub ShopifyOrderAPI_GET()
Dim http As Object, str As Variant
Set http = CreateObject("MSXML2.XMLHTTP")
With http
.Open "GET", "https://SHOPNAME.myshopify.com/admin/orders.json?status=any", False
.Send
str = Split(.responseText, "{""orders"": [ {")
End With
x = UBound(str)
Dim JSON As Object
Set JSON = JsonConverter.ParseJson(http.responseText)
On Error Resume Next
For Each V In x
V = 1
Cells(V, 1) = JSON("orders")(1)("id")
Cells(V, 2) = JSON("orders")(1)("email")
Cells(V, 3) = JSON("orders")(1)("closed_at")
Cells(V, 4) = JSON("orders")(1)("created_at")
Cells(V, 5) = JSON("orders")(1)("updated_at")
Cells(V, 6) = JSON("orders")(1)("number")
Cells(V, 7) = JSON("orders")(1)("note")
Cells(V, 8) = JSON("orders")(1)("token")
Cells(V, 9) = JSON("orders")(1)("gateway")
Cells(V, 10) = JSON("orders")(1)("total_price")
Cells(V, 11) = JSON("orders")(1)("subtotal_price")
Cells(V, 12) = JSON("orders")(1)("total_tax")
Cells(V, 13) = JSON("orders")(1)("currency")
Cells(V, 14) = JSON("orders")(1)("financial_status")
Cells(V, 15) = JSON("orders")(1)("total_discounts")
Cells(V, 16) = JSON("orders")(1)("total_line_items_price")
Cells(V, 17) = JSON("orders")(1)("buyer_accepts_marketing")
Cells(V, 18) = JSON("orders")(1)("name")
Cells(V, 19) = JSON("orders")(1)("processed_at")
Cells(V, 20) = JSON("orders")(1)("tags")
Cells(V, 21) = JSON("orders")(1)("contact_email")
Cells(V, 22) = JSON("orders")(1)("order_status_url")
Cells(V, 23) = JSON("orders")(1)("discount_codes")(1)("code")
Cells(V, 24) = JSON("orders")(1)("discount_codes")(1)("amount")
Cells(V, 25) = JSON("orders")(1)("fulfillment_status")
Cells(V, 26) = JSON("orders")(1)("tax_lines")(1)("title")
Cells(V, 27) = JSON("orders")(1)("tax_lines")(1)("price")
Cells(V, 28) = JSON("orders")(1)("tax_lines")(1)("rate")
Cells(V, 29) = JSON("orders")(1)("tags")
Cells(V, 30) = JSON("orders")(1)("line_items")(1)("title")
Cells(V, 31) = JSON("orders")(1)("line_items")(1)("sku")
Cells(V, 32) = JSON("orders")(1)("line_items")(1)("quantity")
Cells(V, 33) = JSON("orders")(1)("line_items")(1)("price")
Cells(V, 34) = JSON("orders")(1)("line_items")(1)("variant_title")
Cells(V, 35) = JSON("orders")(1)("line_items")(1)("properties")(1)("name")
Cells(V, 36) = JSON("orders")(1)("line_items")(1)("properties")(1)("value")
' Cells(V, 37) = JSON("orders")(1)("billing_address")(1)("name")
' Cells(V, 38) = JSON("orders")(1)("billing_address")(1)("address1")
' Cells(V, 39) = JSON("orders")(1)("billing_address")(1)("address2")
' Cells(V, 40) = JSON("orders")(1)("billing_address")(1)("city")
' Cells(V, 41) = JSON("orders")(1)("billing_address")(1)("zip")
' Cells(V, 42) = JSON("orders")(1)("billing_address")(1)("country")
' Cells(V, 43) = JSON("orders")(1)("billing_address")(1)("country_code")
' Cells(V, 44) = JSON("orders")(1)("billing_address")(1)("latitude")
' Cells(V, 45) = JSON("orders")(1)("billing_address")(1)("longitude")
' Cells(V, 46) = JSON("orders")(1)("shipping_address")(1)("name")
' Cells(V, 47) = JSON("orders")(1)("shipping_address")(1)("address1")
' Cells(V, 48) = JSON("orders")(1)("shipping_address")(1)("address2")
' Cells(V, 49) = JSON("orders")(1)("shipping_address")(1)("city")
' Cells(V, 50) = JSON("orders")(1)("shipping_address")(1)("zip")
' Cells(V, 51) = JSON("orders")(1)("shipping_address")(1)("country")
' Cells(V, 52) = JSON("orders")(1)("shipping_address")(1)("country_code")
' Cells(V, 53) = JSON("orders")(1)("shipping_address")(1)("latitude")
' Cells(V, 54) = JSON("orders")(1)("shipping_address")(1)("longitude")
V = V + 1
Next V
End Sub
Any ideas where I'm going wrong? Any assistance/guidance is much appreciated.
This is my first time working with APIs in VBA.
Related
When I was learning diceloss, I didn't understand how to write this code:
tp = torch.sum(temp_target[...,:-1] * temp_inputs, axis=[0,1])
What does temp_target[...,:-1] mean?
The code for the entire diceloss calculation is as follows:
def Dice_loss(inputs, target, beta=1, smooth = 1e-5):
n, c, h, w = inputs.size()
nt, ht, wt, ct = target.size()
temp_inputs = torch.softmax(inputs.transpose(1, 2).transpose(2, 3).contiguous().view(n, -1, c),-1)
temp_target = target.view(n, -1, ct)
tp = torch.sum(temp_target[...,:-1] * temp_inputs, axis=[0,1])
fp = torch.sum(temp_inputs , axis=[0,1]) - tp
fn = torch.sum(temp_target[...,:-1] , axis=[0,1]) - tp
score = ((1 + beta ** 2) * tp + smooth) / ((1 + beta ** 2) * tp + beta ** 2 * fn + fp + smooth)
dice_loss = 1 - torch.mean(score)
return dice_loss
The function checks the wavetrend cross and is displayed if the condition is met. I want the function to run for every bar within the last 5 bars. How can I do it with Bar Index
func_wave() =>
n1 = input(10, 'Channel Length')
n2 = input(21, 'Average Length')
obLevel1 = input(60, 'Over Bought Level 1')
obLevel2 = input(53, 'Over Bought Level 2')
osLevel1 = input(-60, 'Over Sold Level 1')
osLevel2 = input(-53, 'Over Sold Level 2')
ap = hlc3
esa = ta.ema(ap, n1)
d = ta.ema(math.abs(ap - esa), n1)
ci = (ap - esa) / (0.015 * d)
tci = ta.ema(ci, n2)
wt1 = tci
wt2 = ta.sma(wt1, 4)
con = ta.crossover(wt1, wt2)
con
s1 = request.security('BTCUSDT', timeframe.period, func_wave())
s2 = request.security('ETHUSDT', timeframe.period, func_wave())
s3 = request.security('BNBUSDT', timeframe.period, func_wave())
scr_label = 'WT\n'
scr_label := s1 ? scr_label + 'BTCUSDT\n' : scr_label
scr_label := s2 ? scr_label + 'ETHUSDT\n' : scr_label
scr_label := s3 ? scr_label + 'BNBUSDT\n' : scr_label
I = label.new(bar_index -165, 0, scr_label, color=color.green, textcolor=color.white,
style=label.style_label_up, yloc=yloc.price)
label.delete(I[1])
plot(0, color=color.new(color.white, 0))
You could use the ta.barssince function
https://www.tradingview.com/pine-script-reference/v5/#fun_ta{dot}barssince
I would like to find all numbers and formulas which contain subscripts and superscripts in excel cells and replace it with html tags for subscripts and superscripts.
Eg. cell containing a2 + (b3 - c) would be replaced as:
a<sup>2</sup> + (b<sup>3</sup> - c)
Thanks a lot.
Try this one:
Sub test()
Dim ColNo, RowNo As Long
Dim NewStr As String
Set ws1 = Worksheets("q")
Set ws2 = Worksheets("q-a")
ws1.Activate
With ws1
RowNo = .UsedRange.SpecialCells(xlCellTypeLastCell).Row
ColNo = .UsedRange.SpecialCells(xlCellTypeLastCell).Column
For i = 1 To ColNo
For j = 1 To RowNo
l = 1
NewStr = .Cells(j, i).Value2
For k = 1 To Len(.Cells(j, i).Value2) - 1
If .Cells(j, i).Characters(k + 1, 1).Font.Superscript = True Then
NewStr = Mid(NewStr, 1, k - 1 + l) & "<sup>" & Mid(.Cells(j, i) _
.Value2, k + 1, 1) & "</sup>" & Mid(.Cells(j, i).Value2, _
k + 2, Len(.Cells(j, i).Value2) - (k + 1))
l = l + 11
ElseIf .Cells(j, i).Characters(k + 1, 1).Font.Subscript = True Then
NewStr = Mid(NewStr, 1, k - 1 + l) & "<sub>" & Mid(.Cells(j, i)._
Value2, k + 1, 1) & "</sub>" & Mid(.Cells(j, i).Value2, _
k + 2, Len(.Cells(j, i).Value2) - (k + 1))
l = l + 11
End If
Next
l = 1
For k = 1 To Len(NewStr) - 1
If InStr(k, NewStr, "</sup><sup>", vbBinaryCompare) = k Then
NewStr = Mid(NewStr, 1, k - 1) & Mid(NewStr, k + 11, Len(NewStr) - (k + 10))
ElseIf InStr(1, NewStr, "</sub><sub>", vbBinaryCompare) <> 0 Then
NewStr = Mid(NewStr, 1, k - 1) & Mid(NewStr, k + 11, Len(NewStr) - (k + 10))
End If
Next
ws2.Cells(j, i).Value2 = NewStr
NewStr = ""
Next
Next
End With
End Sub
According with the new information, I just make it simpler.
Hope it helps
As far as I try, that one works always but fail on the first character. So if the firs character is Subscript or Superscript, it will fail.
The code it is for the superscript case. For the subscript case, just change .Font.Subscript by .Font.Superscript and whatever code on html (<sup> on the superscript).
Sub test()
Dim ColNo, RowNo As Long
Dim Pos(500) As Integer
Dim Str(500) As String
Dim sType(500) as String
Dim NewStr As String
Set ws1 = Worksheets("Hoja1")
Set ws2 = Worksheets("Hoja2")
ws1.Activate
With ws1
RowNo = .UsedRange.SpecialCells(xlCellTypeLastCell).Row
ColNo = .UsedRange.SpecialCells(xlCellTypeLastCell).Column
l = 2
For i = 1 To ColNo
For j = 1 To RowNo
Pos(1) = 1
For k = 1 To Len(.Cells(j, i).Value2) - 1
If .Cells(j, i).Characters(k + 1, 1).Font.Superscript = True Then
Pos(l) = k + 1
sType(l) = "Sup"
l = l + 1
ElseIf .Cells(j, i).Characters(k + 1, 1).Font.Subscript = True Then
Pos(l) = k + 1
sType(l) = "Sub"
l = l + 1
End If
Next
For k = 1 To l - 1
If Pos(k + 1) > Pos(k) Then
If sType(l + 1) = "Sup" Then
Str(2 * k) = Mid(.Cells(j, i).Value2, Pos(k), Pos(k + 1) - Pos(k))
Str(2 * k - 1) = Mid(.Cells(j, i).Value2, Pos(k + 1), 1)
Str(2 * k - 1) = "<sup>" & Str(2 * k - 1) & "</sup>"
NewStr = NewStr & Str(2 * k) & Str(2 * k - 1)
ElseIf sType(l + 1) = "Sub" Then
Str(2 * k) = Mid(.Cells(j, i).Value2, Pos(k), Pos(k + 1) - Pos(k))
Str(2 * k - 1) = Mid(.Cells(j, i).Value2, Pos(k + 1), 1)
Str(2 * k - 1) = "<sub>" & Str(2 * k - 1) & "</sub>"
NewStr = NewStr & Str(2 * k) & Str(2 * k - 1)
End If
End If
Next
If NewStr <> "" Then
NewStr = NewStr + Mid(.Cells(j, i).Value2, _
Pos(l - 1), Len(.Cells(j, i).Value2) - Pos(l - 1))
Else
NewStr = .Cells(j, i).Value2
End If
ws2.Cells(j, i).Value2 = NewStr
NewStr = ""
For k = 1 To l - 1
Pos(k) = 0
sType(k) = ""
Str(2 * k) = ""
Str(2 * k - 1) = ""
Next
l = 2
Next
Next
End With
End Sub
Hope it helps
I am implementing a search filter in my web app. using sub-queries like this:
tool = Tool.select('*, (select ROUND(AVG(ratings.rating)) from ratings where tool_id = tools.id AND rating_type = 2) as ratings,
3956 * 2 * ASIN(SQRT(POWER(SIN(('+"#{params[:latitude]}"+' - abs(tools.latitude)) * pi()/180 / 2), 2) + COS('+"#{params[:latitude]}"+' * pi()/180 ) * COS(abs(tools.latitude) * pi()/180) * POWER(SIN(('+"#{params[:longtude]}"+' - abs(tools.longitude)) * pi()/180 / 2), 2) )) as distance').where('user_id != ? AND pause_status =?', user_id, 0).order('distance asc')
# =>delivery type only if delivery type is 1
if params[:search].present? && !params[:search].nil?
tool = tool.where('title LIKE ? OR description LIKE ?', "%#{params[:search]}%","%#{params[:search]}%")
end
# =>Category search
if params[:category_id].present? && !params[:category_id].nil?
tool = tool.where('category_id =?', params[:category_id])
end
# =>price range
if params[:max_price].present? && params[:min_price].present? && !params[:max_price].nil? && !params[:min_price].nil?
tool = tool.where('price >= ? AND price <= ?', params[:min_price].to_f, params[:max_price].to_f)
end
# => filter availability
if params[:availability].present? && !params[:availability].nil?
if params[:availability].to_i == 2
tool = tool.where('available_type =?', 2) #=> weekend
elsif params[:availability].to_i == 1
tool = tool.where('available_type =?', 1) # => weekdays
end
end
if params[:rating].present? && !params[:rating].nil?
tool = tool.having('ratings > 5')
end
if params[:delivery_type].present? && !params[:delivery_type].nil?
if params[:delivery_type].to_i == 0
tool = tool.where('delivery_type = ?', 0)
end
end
if tool.empty?
return []
else
tool_array = []
tool.each do |t|
tool_hash = {}
tool_hash['id'] = t.id
tool_hash['title'] = t.title
tool_hash['latitude'] = t.latitude
tool_hash['longitude'] = t.longitude
tool_hash['attachment'] = Image.get_single_attachment(t.id)
tool_array.push(tool_hash)
end
return tool_array
end
when I pass rating parameter it print the query like this:
SELECT COUNT(*) FROM `tools` WHERE (user_id != 3 AND pause_status =0) HAVING (ratings > 5)"
and without rating parameter:
SELECT *, (select ROUND(AVG(ratings.rating)) from ratings where tool_id = tools.id AND rating_type = 2) as ratings, 3956 * 2 * ASIN(SQRT(POWER(SIN((30.657797735213 - abs(tools.latitude)) * pi()/180 / 2), 2) + COS(30.657797735213 * pi()/180 ) * COS(abs(tools.latitude) * pi()/180) * POWER(SIN((76.7327738833397 - abs(tools.longitude)) * pi()/180 / 2), 2) )) as distance FROM `tools` WHERE (user_id != 3 AND pause_status =0) ORDER BY distance asc"
and I a error like this in my having clause:
"error": "Mysql2::Error: Unknown column 'ratings' in 'having clause': SELECT COUNT(*) FROM `tools` WHERE (user_id != 3 AND pause_status =0) HAVING (ratings > 5)",
"code": 301
and if I comment the each loop it works.
Please tell where I am doing wrong.
having should work with group by
You can't use alias name as ratings in the following line,
select ROUND(AVG(ratings.rating)) from ratings where tool_id = tools.id AND rating_type = 2) as ratings
Give a different name as MYSQL confusing ratings as column.
MySQL Case statement error:
When I use the following, as separate cases, the query works, however when combined they do not.
I am using MySQL Workbench
Error:
Only the Manufacturer Information returns, not the ProdID based query.
The ProdID is correct, I use it to pull the ID prior to this and it returns the correct ID#.
I require them in 1 column to run calculations in the next step.
Not working:
CASE
When(T4.manufacturers_id = 1) then ''
When(T4.manufacturers_id = 2) then '.10'
When(T4.manufacturers_id = 3) then '.10'
When(T4.manufacturers_id = 4) then '0'
When(T3.products_id = 11) then '.10'
When(T3.products_id = 34) then '.10'
When(T3.products_id = 35) then '.10'
When(T3.products_id = 36) then '.10'
When(T3.products_id = 37) then '.10'
When(T3.products_id = 38) then '.10'
When(T3.products_id = 39) then '.10'
end As Comms,
working:
CASE
When(T4.manufacturers_id = 1) then ''
When(T4.manufacturers_id = 2) then '.10'
When(T4.manufacturers_id = 3) then '.10'
When(T4.manufacturers_id = 4) then '0'
end As MIDComms,
case
When(T3.products_id = 11) then '.10'
When(T3.products_id = 34) then '.10'
When(T3.products_id = 35) then '.10'
When(T3.products_id = 36) then '.10'
When(T3.products_id = 37) then '.10'
When(T3.products_id = 38) then '.10'
When(T3.products_id = 39) then '.10'
end As PIDComms,
Ideally I'd like it to be:
CASE
When(T4.manufacturers_id = 1) then ''
When(T4.manufacturers_id = 2) then '.10'
When(T4.manufacturers_id = 3) then '.10'
When(T4.manufacturers_id = 4) then '0'
When(T3.products_id = 11) then '.10'
When(T3.products_id) between 34 and 39 then '.10'
end As Comms,
Thanks in advance
A case statement only returns one value -- the first one encountered. Perhaps you want some sort of concatenation:
CONCAT_WS(':',
(CASE When(T4.manufacturers_id = 1) then ''
When(T4.manufacturers_id = 2) then '.10'
When(T4.manufacturers_id = 3) then '.10'
When(T4.manufacturers_id = 4) then '0'
END),
(CASE When(T3.products_id = 11) then '.10'
When(T3.products_id = 34) then '.10'
When(T3.products_id = 35) then '.10'
When(T3.products_id = 36) then '.10'
When(T3.products_id = 37) then '.10'
When(T3.products_id = 38) then '.10'
When(T3.products_id = 39) then '.10'
end) ) As Comms,