Search code examples
google-sheetsweb-scrapinggoogle-sheets-formula

importxml converting string to int


After many hours I finally figured out how to import data from <span>... Yet right now I've hit next wall and I can't figure out what can help me in this situation. After using:

=IMPORTXML("https://www.bodypak.pl/pl/aminokwasy/5890-6pak-nutrition-bcaa-pak-400g.html";"//span[@id='our_price_display']")

As result, we have 79,00 PLN, yet that is String and I can't figure out how to convert it to INT For making everything easier cell where I'm importing data I will mark as #XML =Value('#XML') don't work, same implemented formatting.
I also tried to export the result to another cell and format another cell but that didn't work either.

=LEFT(#XML,LEN(#XML)-3) leave me with 79,00 but still I can't convert it to Int.
And I'm quite stuck right now.


Solution

  • You can use the function split like this: split(str, separator, 0) and take the first item of the resulting array with

    =index(split(arr,","),0,1)
    

    All together:

    =index(split(split(IMPORTXML("https://www.bodypak.pl/pl/aminokwasy/5890-6pak-nutrition-bcaa-pak-400g.html","//span[@id='our_price_display']")," "),","),0,1) 
    

    The formula above truncates the string at the ",". If you want to actually convert to integer, use int() after replacing the comma with a ".":

    =int(value(substitute(split(IMPORTXML("https://www.bodypak.pl/pl/aminokwasy/5890-6pak-nutrition-bcaa-pak-400g.html","//span[@id='our_price_display']")," "),",",".")))