Extract a single row from a table - html

I’m trying to extract a single row from a table.
I'm using google sheet to create the links and in cell D3 it contains this url.
https://www.wsj.com/market-data/quotes/AAPL/options
I have several links in cell D3 to go through.
The word "Last Trade" appears several times in different tables but I'M ONLY INTERESTED IN THE VERY FIRST TABLE FROM THE TOP.
with this word and once this word is found i'm looking to extract the ROW just above it.
Below is the IMPORTXML, and its needs modification and it should be able to pull that last row.
=IMPORTXML(D3,"//tr[td1/#class='acenter inthemoney'][last()]")
Any help would be greatly appreciated.
Thanks.

For that row you will need:
(//tr[#class='last_trade_row'])[1]/preceding-sibling::tr[1]
And then pick the wright td...it's unclear which td you want. So if you wanted the third td the XPath would be:
(//tr[#class='last_trade_row'])[1]/preceding-sibling::tr[1]/td[3]
Its always the first table that ends with the word LAST TRADE and the row above it that i'm looking to extract, so in this case this is the row that i'm looking to extract, below is the picture.
https://www.wsj.com/market-data/quotes/AAPL/options
In the above case where you want the first td the XPath will then be
(//tr[#class='last_trade_row'])[1]/preceding-sibling::tr[1]/td[1]

Related

How can I make a formula in Google Spreadsheet to return a value if a cell contains a text in a list

I'm currently interested in building a custom function/formula in Google Spreadsheet where the function will intake a cell and return a value that is related to a specific text in a list.
As you can see in the screenshot, which is a demo that I drafted to give a better idea of what I'm trying to accomplish, column A contains a list of items that has random texts. In the texts, there are some keywords that I would like to identify into different categories.
Column B is just a demo of how I envision to use my custom function that I want to create. Column C is the expected result that I would get by using the custom function.
Column E is the list of keywords that I would like the custom function to search for in cells and Column F is the category that I would like the custom function to return once it finds the relevant keyword in a text.
Thank you!
I tried searching for this topic, but I only came across how to set up an array formula to achieve what I want to do. If there was any post about this, I would really appreciate if you could just link it in the comment.
Given your scenario, you can try with this formula:
=FILTER(F:F,INDEX(REGEXMATCH(A2,"(?i)"&E:E)),E:E<>"")
If you wish to have it for the whole row:
=BYROW(A2:A,LAMBDA(each,IF(each="","",FILTER(F:F,INDEX(REGEXMATCH(each,"(?i)"&E:E)),E:E<>""))))
If you could have more than one result, you can add JOIN:
=BYROW(A2:A,LAMBDA(each,IF(each="","",JOIN(", ",FILTER(F:F,INDEX(REGEXMATCH(each,"(?i)"&E:E)),E:E<>"")))))

Do I need a script or a formula?

I am trying to have the formula look in Column A for not empty cells. (That part of the formula works fine.) Then return the name from Column B that's in the same row as the not empty cell.
Column A Column B
text/date Kelly
So if B has anything in it, tell me the name Kelly. I 've tried combining formulas, but I'm either not doing it correctly or maybe I need a script?
Here's the part that's working: =IF(A24="","don't meet","meet")
Of course, I want it to search the whole column, but I know for sure 24 has the text in the cell with a name so I was just playing around that line.
Answer is based on the description and document you provided and should give you something to work with.
This formula simple checks the entire Column A and returns values from the corresponding row from Col B if the Cell from the Col A row is empty/blank.
=ARRAYFORMULA((IF(A1:A<>"",B1:B,"")))
Here are example screenshots of your example data + end result
Though I'm not entirely sure what you'd like to happen if A has values on them. This formula retains A if it has content but you could always change the (A2:A="",B2:B,A2:A) part of the formula if you want something else to happen if A is not empty.
Give this a try, I put it in your sheet in the green area.
=ARRAYFORMULA(IF(A1:A15 = "", "", B1:B15))
Arrayformula applies the IF function to the entire range. So substitute A1:A15 with what ever the whole range is, A1:A600 or A1:A if you want to do the whole column. Make srue the second column has the same values but with B.
You can use filter() to filter your Column B if both column A and column B is not empty.
Formula:
=filter(B1:B,A1:A<>"",B1:B<>"")
Output:

Is there a way to access the first element in a column on a website using VBA?

Here is a screenshot of a column in a website page.
It is located in that way in the website page :
As you can see, all the rows have a 'Completed' button you can pres and followed by a number of lines. These rows refer to exports. So the columnis not static and is constantly changing.
However, everytime i run the macro i want to access the first row of the column.
Here is a sample code of he HTML code of the first 'Completed' button in the screenshot above:
I have many that have the same class name. Look at the highlighted rows as an example in the picture below:
I really have no idea how to write a VBA code to always access the first 'Completed' bytton in this column.
PS: In the HTML code, in the tag "a", the onclick="....." is constantly changing. So i cannot use this as an argument to access the desired field and click on the desired button.
Please if anyone could help me figure out how to do this, i would really be happy.
Thank you :)
If you want to click the 'Completed' button in the first column, you can use the code below:
Set doc = objIE.Document
doc.getElementsByTagName("tr")(0).getElementsByTagName("td")(0).getElementsByTagName("a")(0).Click
The code get the first <tr> then get the first <td> then get <a> in it.
<tr> tags are rows, <td> tags are cells inside those rows. You did not provide enough code to show the entire table, but generally speaking to access the first row of a table, you would need to refer to the collection object and use the index number you want.
.getElementsByTagName("tr")(0)
This will refer to the first row of a table. Same with getting the first column in the first row of your table:
.getElementsByTagName("tr")(0).getElementsByTagName("td")(0)
Once you tracked down the particular cell, now you are wanting to click the link. You can use the same method as above.
.getElementsByTagName("tr")(0).getElementsByTagName("td")(0).getElementsByTagName("a")(0).Click
And a final note, the first row of a table could be a header, so you may actually want the 2nd row (1) instead.
Thanks for updating with more HTML code. I am going to slightly switch gears and use querySelector() to grab the main table.
doc.querySelector("#divPage > table.advancedSearch_table > tbody"). _
getElementsByTagName("tr")(3).getElementsByTagName("td")(3).Children(0).Click
See if this works for you.

xpath scraping data from the second page

I am trying to scrape data from this webpage: http://webfund6.financialexpress.net/clients/zurichcp/PortfolioPriceTable.aspx?SchemeID=33, and I specifically need data for fund number 26.
Have no problem getting data from the first page with this address (funds number 1-25), but for the hell of me can't scrape anything from the second page. Can someone help?
Thanks!
Here is the code I use: in Google Sheets:
=IMPORTXML("http://webfund6.financialexpress.net/clients/zurichcp/PortfolioPriceTable.aspx?SchemeID=33","/html/body/form[#id='MainForm']/table/tr/td/div[#id='main']/div[#id='tabResult']/div[#id='Prices']/table/thead/tr[26]/td[#class='Center'][1]")
You can do 2 things - one is to append the PgIndex=2 onto the end of your URL, and then you can also significantly simplify your xpath to this:
//*[#id='Prices']//tr[2]/td[2]
This specifically grabs the second row on the table (tr which means table-row), in order to bypass the header row, then grabs the second field which is the table-data cell.
=IMPORTXML("http://webfund6.financialexpress.net/clients/zurichcp/PortfolioPriceTable.aspx?SchemeID=33&PgIndex=2","//*[#id='Prices']//tr[2]/td[2]")
To get the second page, add &PgIndex=2 to your url. Then adjust the /table/thead/tr[26] to /table/thead/tr[2]. The result is:
=IMPORTXML("http://webfund6.financialexpress.net/clients/zurichcp/PortfolioPriceTable.aspx?SchemeID=33&PgIndex=2","/html/body/form[#id='MainForm']/table/tr/td/div[#id='main']/div[#id='tabResult']/div[#id='Prices']/table/thead/tr[2]/td[#class='Center'][1]")

(SSRS) if cell contains specific text then divide another cell by a number say 2

I'm having trouble trying to divide a cell by (A NUMBER) only if another cell contains specific text and if the cell contains another text i want it to be divide by a different number in SSRS
This is what i have but it is not working.
=IIF(Fields!PARTNO_LOT.Value=("A1001"),Fields!LOCSTOCK.Value/200)
I have added a example but in excel just to show my intent in SSRS.
If the above answer doesn't work, try this:
=Switch(
Fields!PARTNO_LOT.Value="A1001",Fields!LOCSTOCK.Value/200,
Fields!PARTNO_LOT.Value="A1002",Fields!LOCSTOCK.Value/285,
Fields!PARTNO_LOT.Value="A1003",Fields!LOCSTOCK.Value/89
)
Let me know if this helps.
If I understand correctly, you are trying to execute multiple conditions. You can use nested IIF statements:
=IIF(Fields!PARTNO_LOT.Value=("A1001"),Fields!LOCSTOCK.Value/200,
IIF(Fields!PARTNO_LOT.Value=("A1002"),Fields!LOCSTOCK.Value/285, Fields!LOCSTOCK.Value/89))
If your values are only few, this would work. If they are more than 2-3, I would look into creating a function. Reference: https://msdn.microsoft.com/en-us/library/ms156028.aspx