I am using Google Apps Script to update a Google spreadsheet.
The Apps script is taking the follower count for Twitter pages.
I am using below formula for the same
I have used this URL for reference
cell.setValue('=IMPORTXML(\"'+url+'\","//a[#data-nav=\'followers\']/span[3]")');
Basically we are doing a kind of scraping here to extract the count values. The above code is looking for anchor tag with value for data-nav as followers and taking the text inside the 3rd span tag . When I inspected the page in browser , the value shown in the third span tag was 100 (for example).
But the value picked by appscript is higher than that (like 101 or 102) .
I thought it was due to caching , and we even used random numbers to avoid that , but even that didn't work.The details for that was found here
Can somebody have a look into this ?
Related
I am a legal ops specialist working in a law firm with an interest of coding but have no practice lately. (I had python course during my studies but that was years ago)
Therefore I may be asking a very easy question, because I used an existing template and modified it for our process, however because of my lack of information on the topic I can't find the cause of the problem we are having.
So this is the template I used and the guide about it.
The problem is, when i filled the form any send a mail to the approvals it doesn't fill the answer template(docs) so the e-mail I receive comes as following;
table in the email template which is supposed to be fed from the google sheet
Which is basically the names of the google sheets columns, not the value in the cell. I believe the problem might be about the following piece of code;
// We start with the doc template HTML body, and then we replace
// each '{{fieldName}}' with the row's respective value.
let emailBody = headers.reduce(
(result, fieldName) => result.replace(`{{${fieldName}}}`, row[fieldName]),
docToHtml(templates.get(emailTemplate || action))
);
OR, because I couldn't manage to link the table in the google docs template with the answer sheet and I don't know how to do it.
Any kind of help is appreciated, I'm very cold and lost out here.
Edit1: Screenshots of the answer sheet and related part of the code.
answer sheet - columnsconst part of the code
Edit2: I added a screenshot of the template provided by Google. You can see that there is a link between the template and the answer sheet. I couldn't manage to restore such link when I changed the google docs template. Google docs template
After a lot of testing, as strange as it sounds, i have discovered that the main issue is the font type.
Also, it appears not possible to use a placeholder more than 1 time on the same email.
The evidence i have is that, by using a text editor to create the placeholder, i set ALL of the values in the doc row to {{Name}}, and in the Form Responses i set the name value to 1000.
The first 1 worked, then i typed over the second value with a different placeholder, and it began to work too.
So, you should recreate all the placeholders in a text editor, then copy and paste them onto the doc.
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.
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.
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".
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.