Grab table from URL in Google App Script - google-apps-script

In this link answer in line 6, how to identify how many getElement("div") and getElements("div")[number] should be captured from a URL.
This is the URL from where I am looking to capture the table.

You may want to look into using import.io, which is a tool specifically for extracting tabular data from web pages, and which has an Apps Script integration.

another example could be to pull the table into Sheets and the access the data from within the Sheets. For example:
=importhtml("http://www.tradingeconomics.com/zambia/rating","table",1)
This pulls the first table on the particular webpage into the sheet.

Related

Web scraping with Google Script, asynchronous website?

I want to scrape this specific information "NM_000014.5" from the RefSeq transcripts table (to be more specific I'd like to have the RefSeq Select one) of this website https://www.ncbi.nlm.nih.gov/gene/?term=%22A2M%22+human
I want to scrape this information using Google script in order to add it to a Google sheet table. I have already been able to scrape other info but this specific information is too hard to get for me. I tried to use UrlFetchApp.fetch and parsing with XmlService but it seems that this info is linked to another website...or something like that...is there a "simple" way to get that?
You could try using the IMPORT functions that come with Google Sheets -- specifically, either ImportXML or ImportHTML. These are great options that I have used in the past for simple site scrapes.
Here is a great article covering basic web scraping in Google Sheets with ImportXML: https://www.benlcollins.com/spreadsheets/google-sheet-web-scraper/
Just right-click Inspect the site for the correct HTML tags. Hope this helps a bit

Can I dynamically create a google form to e-mail based on a template

I'm trying to write a form for trainers to report attendance on particular classes to send a standardized report back to HR. I want to use a spreadsheet to populate the question. Would I be able to take an existing form and modify the controls on it, or do I need to create the forms entirely in code?
There is an add-on for Forms, formRanger, which will allow you to change the responses available to a question. Using that changes the form for all those viewing/responding at that time. So you could not concurrently have different responses. You can have multiple Forms submit to the same file, and then merge the responses if you wished. Or even record them into different files and merge the contents into a common file.
The only other solution would be to create code and deploy the form as a web app. From that you could have the web app read a spreadsheet file or a sheet and create the responses based on that. You may want to start here: Extending Google Sheets and then look at HTML Service: Create and Serve HTML as a starting point.
Once you have specific questions, create a new post and be sure to include your code.

IMPORTxml on google sheets

Trying to make a spreadsheet on google sheets that scrapes data from a site.
I'm trying to get the sell price of items from rsbuddy exchange, eg:
https://rsbuddy.com/exchange/?id=1745
I have the code on google sheets as:
=IMPORTxml("https://rsbuddy.com/exchange/?id=1745","//*[#id='sell-price']")
But instead of showing me 1734gp it comes up as --- on the sheet.
I've tried adding /text() at the end of the query for the importxml but it doesn't change anything.
I'm guessing the solution is something similar?
I don't believe you can do it with xpath because it's populated dynamically. If you view the full source, you're getting what is there. It's literally ---
You can see the source data here which is in JSON and looks like it's tied to the page url id. Google sheets doesn't natively support json but this good person wrote a script and it seems to work well in my example sheet.
You'll need to do it via JSON, though you can't importJSON natively through Google Sheets. If you add this library as code to your sheet (via the script editor) then you can you can use =IMPORTJSON (with different parameters) to get the data back that you need.
http://blog.fastfedora.com/projects/import-json

Creating a new document on google drive through the API

Is it possible to create a new document (either using a template or completely dynamic) using the Google Drive APIs? I am working with a client that requires generation of word documents. I tired looking up but I couldn't find sufficient documentation on how to "create and format" documents using the APIs.
For example, can I include the client's company logo on the top of the doc programmatically? I'd like whatever I am doing for this client to be generic (i.e including logo at the top etc) to be dynamic, so I can re-purpose this for other clients as well.
I also am having difficulty finding documentation on any kind of formatting we could do on the documents (bold, italic, new paragraph) etc
The Google Drive API only deals with whole file operations. It has no understanding of the content of the files, including formatting.
You have two options:-
Use Apps Script (ie. not the Drive API) which has document manipulation features. See https://developers.google.com/apps-script/reference/document/
Create your templates in HTML (or Word, OpenOffice), then upload them to a new file, setting the option to 'convert to google docs'
It is possible to create new documents as well as make a copy of already existing documents (templates)
Some time ago I've created simple example how to work with templates:
On your Google drive create folder Templates
Inside this folder create formated document with images, texts etc... Replace dynamic values with {A},{B},{C} representing spreadsheet column names in this document
Than create spreadsheet and fill some data in it. Inside this spreadsheet navigate to Tools->Script gallery and search for fast template generator -> Install it
Close document and re-open it. Script will automatically execute onOpen function which will add Template generator to spreadsheet menu. Now just choose row from which you would like to populate values in template and launch Template generator-> Generate from template.
A new document with filled values will be generated from chosen template.
You can find source code and customize it in according your need in Tools->Script editor in this spreadsheet, so result should be in PDF or sent via email etc...
EDIT: Seems this approach does not work, as Google Drive API only allows copying files across current user's Drive.
I found for me suitable approach was to create a document in my own Google Drive. Style it accordingly, add base text etc. and give it read-only permission to everyone. Then from the Google Drive API use copy file functionality and copy it into the current users Drive.

Use a Google Docs Spreadsheet as a datasource for a dynamic Google Sites webpage

I have a Google Form that feeds a Google Docs Spreadsheet. I'd like to--in turn--have that Google Docs Spreadsheet feed a webpage.
In plainer English, babysitters fill out the form to sign up to be in our community's Babysitter Directory. The spreadsheet houses all of the data. I'd like to code a webpage to pull selected bits of the data for the online directory.
I've tried doing a separate sheet in the spreadsheet, using a QUERY to select the columns that I want to include (and the order in which I want to include them), publishing that sheet to the web, then embedding that sheet into the webpage in an iFrame. And that works.
But even with the QUERY, there are SO many columns that users need to scroll WAY over to the right to see all the data for each babysitter. It's unwieldy.
What would be way better would be if I could break the data for each entry over multiple lines and do some nice formatting for a directory, rather than just a linear spreadsheet. So that, essentially, each babysitter's "entry" in the directory is more than 1 line long. Does that make sense?
If I was working in Office, I would know exactly what to do: use the Excel spreadsheet as the datasource for a Word Mail Merge and I would put move the fields around on the page to make it all look good.
And, to be sure, if I can do this in a Google Doc, then embed the Doc into the webpage, that's fine, too. But I would think there's some way I can do it directly in the Google Site?
Can I?
If anybody has even just a reference page for me to take a look at, I'd appreciate it.
Thanks!
Are you trying to do this in Google Sites? If so, you can embed the entire spreadsheet on the page, but if you only want certain columns, you can try inserting an Apps Script widget on the page.
You need to know how to write a Google Apps Script that will run JS functions and render HTML, here is a tutorial
To create the Script that can run on your page, go to:
More > Manage site > Apps Script > Add new script
Here's also a link to how to interact with Spreadsheet data.
What you want to do is more or less a database interface that uses a spreadsheet as 'data holder', depending on your programming skills it can be quite easy or very hard...
Here is an example of such a webapp, its has probably too many fields and features but the general idea is the same (a quick search tool and a window to show results).
It that what you had in mind ?
If so I can share the code to help you to get started but if you are not familiar with javascript it will probably need too much effort to get through.