google spreadsheet importxml, chop off part of query - html

I have a simple ImportXml function in Google spreadsheet:
=ImportXml( "http://www.google.com/finance?q=0322.hk", "//span[#class='chr']" )
but it is giving me 2 cells of data, the cell direction underneath it is a =Continue(blah blah)... automatically placed by Google.
Here is the HTML code associated with this page. Looks like it has 2 items. I've replaced "<" & ">" with "(" & ")"
(span class="ch bld")(span class="chr" id="ref_673324_c")-0.45(/span)
(span class="chr" id="ref_673324_cp")(-2.02%)(/span)
How do I change my ImportXml function so that I'm only importing the "-0.45" without the "(-2.02%)?

I've also found that the INDEX command works... useful too if you want the 2nd (-2.02%) item in the results. One important note, though, is that you've got to first transpose the output of ImportXML or it won't work.
=INDEX( TRANSPOSE (ImportXML (...)), {num}) where num is the desired result.

I found a work around... I put =NOEXPAND function infront of it and it works. but if anybody has another solution let me know.

Related

using formulas and hyperlink in the same cell

So the main idea is I want to put a Hyperlink on a cell that contains a formula, But google sheet don't like that.
I had 2 ideas.
1- Put the formula in cell A1 and the output of that formula is dumped into cell A2 which is gonna be Hyperlinked.
(I tried to use FILTER to pull data into the Hyperlinked cell but it would give me a #REF! error:Array result was not expanded because it would overwrite data in A2.)
2- Use some sort of apps-script of which I have very little understanding of, so I didn't find any scripts relating to my question.
I didn't understand the reason why you can't put a Hyperlink AND a formula in the same cell, But as far as I understood:
Hyperlink is a formula it self → =HYPERLINK(url, [link_label])
So instead of Hyperlinking our cell using the UI, we can use the formula form to Hyperlink the cell and use our formula in the place of [link_label] which is gonna be the name of our cell:
=HYPERLINK( url , [link_label])
=HYPERLINK("google.com", SUM(B2:B5 ))

How to use cell value instead of number

Im sorry I dont even know how to correctly phrase this question.
The line of code I need to edit is this;
sheet.getRange(targetcell).setValue('=INDEX(QUERY(IMPORTHTML("http://www.futbin.com/consumables/Position%20Change","table",2),"select Col2"),1)');
However instead of row 1, I want to use the value from cell G1. So, something like this;
sheet.getRange(targetcell).setValue('=INDEX(QUERY(IMPORTHTML("http://www.futbin.com/consumables/Position%20Change","table",2),"select Col2"),G1)');
However, this throws an error.
Could anyone advise how I can use the value in G1 instead of it being fixed?
Thank you.
Try .setFormula instead of .setValue ...
sheet.getRange(targetcell).setFormula('=INDEX(QUERY(IMPORTHTML("http://www.futbin.com/consumables/Position%20Change","table",2),"select Col2"),G1)');
If there were a column G from the table that results from that IMPORTHTML function then you would use this:
sheet.getRange(targetcell).setValue('=INDEX(QUERY(IMPORTHTML("http://www.futbin.com/consumables/Position%20Change","table",2),"select Col7"),1)');
Because G is column 7
However, there is only 3 columns from that IMPORTHTML. You can check this by entering the formular into a Google Sheet:
=IMPORTHTML("http://www.futbin.com/consumables/Position%20Change","table",2)
I suggest playing around with your formula in SHeets and once you've got what you want, go back into AppsScript.

Google sheets IMPORTXML Query get attribute with local-name

This is a follow up question to this previous question: Google sheets IMPORTXML Return no Data N/A
Given the following XML (has namespace) I want to query the CODE number inside CPV_CODE.
<CPV_CODE CODE="90524400"/>
With XPATH I would simply use: /CPV_CODE/#CODE but as I have to use local-name (due to namespace) I have no idea how to get it.
I tried
=importxml(url,"//*[local-name() ='CPV_CODE']")
but it does not return the #CODE number.
In your situation, how about the following xpath?
Sample xpath:
//*[local-name()='CPV_MAIN']/*[local-name()='CPV_CODE']/#CODE
Or
//*[local-name()='CPV_MAIN']//#CODE
Modified formula:
=IMPORTXML(A1,"//*[local-name()='CPV_MAIN']/*[local-name()='CPV_CODE']/#CODE")
Or
=IMPORTXML(A1,"//*[local-name()='CPV_MAIN']//#CODE")
In this case, the URL is put in the cell "A1".
Result:
Note:
In the URL, it seems that there are 2 CPV_CODE tags. If you want to retrieve <CPV_CODE CODE="90524200"/>, how about the following formula?
=IMPORTXML(A1,"//*[local-name()='CPV_ADDITIONAL']/*[local-name()='CPV_CODE']/#CODE")
Or
=IMPORTXML(A1,"//*[local-name()='CPV_ADDITIONAL']//#CODE")
Reference:
IMPORTXML

Google sheets query with text

This is probably a very stupid question but I can't seem to find an answer for it anywhere. I'm trying to use a simple COUNTA query but I also want text to appear so something like this
"Amount = "=COUNTA(B16:B25)
=(your text) & (your formula output)
="Amount: " & COUNTA(A:A)
should work.

How to use a conditional array formula in Google Sheets?

I want my spreadsheet user to be able to choose between two different options for an array formula. I have tried to put it in an IF statement but I can't get it to work. I do not want to cut and paste it down the column because I want it to be applies when a new row is added. I don't really know what I'm doing but I've been fiddling with it for a couple of hours now. Here is the code I currently have:
=IF($B$6="alternating days",ARRAYFORMULA(IF(ISBLANK(indirect("OVERVIEW!$A" & row())),IF($C2:C="l","l","d"),IF($C2:C="l","d","l"))),IF($B$6="weekdays/weekends",ARRAYFORMULA(IF(ISBLANK(indirect("OVERVIEW!$A" & row())),IF($C2:C="l","l","d"),IF(OR(WEEKDAY
(indirect("OVERVIEW!$A" & row()))=1,WEEKDAY(indirect("OVERVIEW!$A" & row()))=7),"l","d"))),"none"))
It's a long formula so please scroll along.
I attempted to have the ARRAYFORMULA at the beginning but it wouldn't let me reference just $B$6.
Thanks for any help you can provide.
I recommend something like this:
=ARRAYFORMULA(IF($B$6="alternating days",if(isodd(row(indirect("C4:C"&counta(OVERVIEW!A3:A)+3))),"l","d"),IF($B$6="weekdays/weekends",ARRAYFORMULA(IF(WEEKDAY(OVERVIEW!A3:A)=1,"d",if(WEEKDAY(OVERVIEW!A3:A)=7,"d","l"))))))
If the days are alternating, use odd rows to alternate the letter, otherwise just combine 2 IF statements to determine if it is a weekday or not.