How to get all TextRun elements - google-apps-script

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.

Related

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".

Insert the entire contents of one Google Doc into the comment of another

The problem
I'm interested to know if it is possible to copy the entire contents of one Google Doc and paste into a comment in another Google Docs using keyboard commands.
Background-
I do a lot of grading of student papers and have a range of standard comments I use which are individually stored in separate documents. I use Macros and keyboard shortcuts in MS Word to grab the contents of the comment file I want and put it into a comment in the paper I'm grading. I edit the macro files using the VB editor when necessary. It works quite efficiently.
I found some related material in my research, however this don't quite match what I am trying to do.
I think the code from here Insert comment into Google doc does something like what I want, but kind of the opposite.
Google Apps Scripts is new to me. I'm not really looking to become a programmer, I just need to know if developing such a script is possible or not and how hard it would be. I would appreciate being pointed in the right direction. Thanks.
A sample to point you into the right direction:
function myFuction()
{
var fileOriginId='PASTE HERE THE ID OF THE FILE WITH THE COMMENT TEXT';
var fileDestinationId ='PASTE HERE THE ID OF THE FILE WHERE THE COMMENTS SHALL BE INSERTED';
var text=DocumentApp.openById(fileOriginId).getBody().getText()
var comment={ 'content': text};
Drive.Comments.insert(comment, fileDestinationId);
}
This code snippet passes the whole contents of a Google Docs document to the variable text. Subsequently, the contents of text is inserted as a comment into a second Google Docs document. It uses the Advanced Drive Service that needs to be enabled beforehand.
Should you desire to pass only a part of the text in the original file into the comment, e.g. a paragraph, you would need to chose the specific paragraph instead of the whole document body.
Should you desire to append the comment to a certain text within the destination document, you need to use the optional property context.value, as done in the reference you provided.
Please find here references to useful documentation that will help you
udderstand Apps Script and adjust the code snippet provided to your
requirements:
DocumentApp to access Google Docs documents
Body of a document
Retrieve children of a document
Advanced Drive Service
Enable Advanced Services
Insert comments with Advanced Drive Service

Appending or inserting footnotes programmatically into google doc using google app script

I am using Google App Script to generate a new google doc. The contents of the document come from an array that is looped and paragraph elements are added, or text elements are inserted/appended to pre-existing paragraph elements that were earlier in the loop.
The issue is: some of the items in the array need to be appended or inserted as footnote elements.
The documentation says that footnote elements are contained within listItem or paragraph elements. However there does not seem to be a method to insert footnotes programmatically in the same way that other elements can be inserted into paragraphs - such as appendText() and appendHorizontalRule() etc.
I have not posted any code because I do not know where to begin on this issue. Any guidance is appreciated.
According to Apps Script documentation,
...there are rules about which types of elements can contain other
types. Furthermore, the Document Service in Apps Script can only
insert certain types of elements. The tree below shows which elements
can be contained by a certain type of element.
More details here https://developers.google.com/apps-script/guides/docs
Footnote and FootnoteSection elements are not highlighted, which means they can't be inserted programmatically.

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)