ImportXML with XPath in Google Spreadsheet using <span> element - html

I am trying to get the value of an element attribute from this site via importXML in Google Spreadsheet using XPath.
The attribute value i seek is content found in the span class="item-chart_server-price__1r2rn".
outputs is 2,427 Z
Tried using:
//*[#id='app']/div[2]/div[2]/span[2] i get #N/A;
//div[#id='app']//div[#class='item-chart_item-chart__3YMlA']//div[#class='item-chart_server__37cgg']/span[#class='item-chart_server-price__1r2rn'] i get #N/A;
//span[#class='item-chart_server-price__1r2rn'] i get #N/A;

looks like this won't be possible. all you can get is:
=IMPORTXML("https://www.romexchange.com/?q=steel","//*")

Indeed, I was not able to fetch the data using ImportXML, but I ended up being able to fetch from romexchange.com using a custom javascript function.
You can have a look at it in
https://github.com/diogovk/rom_exchange_sheets_macro/blob/master/rom_exchange_sheets_macro.js
To enable it on your sheets, go to Tools->Script editor.
After you saved a new function ROMX_latest will be available.

Related

How to get all TextRun elements

Using Google App Script, is there a way to get all the TextRuns (a block of text with the same format) in Google Docs?
I'm trying to create a function that batch replaces one specific text format (from black text to white text).
I've looked through the GAS reference, but can't find anything that does this. findElement() doesn't allow the ElementType TextRun. And overall TextRun doesn't seem to be a functional/manipulable element, at least in the current ref docs -- even though the Document Structure doc does mention it as a sub-element of Paragraph.
So, is there a way to get all the TextRuns? Or is there an equivalent alternative?
Text runs can be retrieved using Document.get. The runs are located at Body> Structured Element (Paragraph)>Paragraph Element>TextRun in the document json. Advanced Google services may be used to access the api from apps script.

How get content from html element in Google spreadsheet?

I can use following code in Google Spreadsheet to retrieve a specific <span> element on that html-document.
=IMPORTXML("https://finance.yahoo.com/quote/NBIX201218C00125000?p=NBIX201218C00125000";"//td[#data-test='PREV_CLOSE-value']/span")
But this returns the error message that the returned content is empty.
So how do I retrieve the content (in this example the text '17.00') of this HTML-element in the spreadsheet?
Use IMPORTFROMWEB addon (number of requests are limited in the free plan) to get this.
XPath used :
//td[#data-test='PREV_CLOSE-value']/span
Formula in C5 :
=IMPORTFROMWEB(C1;C2)
Maybe this is because of the consent manager on Yahoo, that is shown before you get to the actual page.
When I downloaded the page, after checked the consent, put in on my server and use your code in Google Spreadsheets it is working.

Get the ranges of the elements in a google doc using Google Apps Script

Following on from this question - I am now unsure how I might be able to target certain paragraphs or elements within the document when using Google Apps Scripts in order to use the batch update method outlined one would need to find the range of the element that required styling.
If there were 10 empty paragraphs (with content of "\n",) in a document - how would it be possible to target the 8th paragraph and get the range values {"startIndex": xx, "endIndex": xx} relative to the document so they could be used in the batch update?
I have a feeling I am missing something very obvious here.
Using Google Docs API and the document ID you can make a documents.get request, which will return you a document resource with a body field which has a content field with all the elements in the document, where you can see the "startIndex" and "endIndex".

How do I create a google form template using google script?

I have created a google form which I would like to convert to a template. The objective is to to distribute this template to other people so they can amend the form as they see fit. I also need these amended forms to sit inside a directory in the google drive.
How can I construct a google script to convert the original form into a template?
Thus far I have
var form = FormApp.openById('asdfasdfasdfasdfasdfasdfFORMIDGOESHEREfasdfasdfa');
I am stuck on how to take this form and now convert it to a template.
If you want to get the whole form and then transform it into a template to modify it at will, it is not feasible. But instead, if you want to get certain elements and modify them or create a new form using Apps Script you can check the Class Form, which gives you several methods that will allow you to insert or removes elements.

How to get Google search result snippets (descriptions) by using importxml function?

I want use Google spreadsheet =importxml to get Google search results snippets but I don't know what XPath I should use. Could any tell me what is XPath for it?
I tried using //h3[#class='result-desc'] but it keeps telling me:
imported content is empty
=IMPORTXML("https://www.google.com/search?q=Bmw&safe=off&tbs=qdr:d", "//h3[#class='result-desc']")
What is correct XPath for it?
Remember that Google Sheets ImportXML function will often receive a different HTML than what you see in a browser. (For example it doesn't load JavaScript, etc), so you cannot rely on inspecting HTML to get your XPATHs.
In your case some examples you can try:
=IMPORTXML("https://www.google.com/search?q=Bmw&safe=off", "//*[#class='g']")
or
=IMPORTHTML("https://www.google.com/search?q=Bmw&safe=off","list",9)