Google Script, Import HTML data - google-apps-script

I'm trying to create a personal notifier whenever a certain website contains the word 'Indian' the script should email me.
For some reason I cannot find a script function that would import HTML data,
Is there such a function?

Yes, you can perform a URL fetch:
https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String)

Just like Harold said, you can perform a URL fetch:
var output = UrlFetchApp.fetch("https://raw.githubusercontent.com/####");
var form = HtmlService.createHtmlOutput(output.getContentText());
SpreadsheetApp.getUI().showModalDialog(form, "Fetched HTML");
You can otherwise fetch a website using the same tactic, for example:
var output = UrlFetchApp.fetch("https://www.google.com");
var form = HtmlService.createHtmlOutput(output.getContentText());
SpreadsheetApp.getUI().showModalDialog(form, "Fetched HTML");
Upon launch, the dialog should return Google's homepage.

Related

How to get the correct XPath for Google Sheets formula ImportXML

I tried to set up importXML in my Google Sheets, I used the method to copy full Xpath.
It seems not working at all.
After reading Xpath still not sure how to get the right path just for the token price.
Hope can get some idea or document to read to get the value I need.
Thanks a lot for reading this.
Wish you to have a nice day.
=IMPORTXML("https://info.osmosis.zone/token/DSM","/html/body/div[1]/div/div[3]/div/div/p")
Create a custom function by:
Opening Script Editor (Tools > Script Editor or Extensions > Apps Scripts)
And then enter the following within the script:
/**
* #return Specific value out of the value of different fields
* #customfunction
*/
function PARSEVALUE(Url,itemKey) {
var res = UrlFetchApp.fetch(Url);
var content = res.getContentText();
var jsonObject = JSON.parse(content);
return jsonObject[0][itemKey];
}
In your spreadsheet, use the function like:
=PARSEVALUE("https://api-osmosis.imperator.co/tokens/v1/DSM","price")
There are different values for different keys, as in price,symbol,name,liquidity,volume_24h e.t.c. you can grab using this function.
The page contents
You need to enable JavaScript to run this app.
JavaScript content is not supported by any IMPORT formulae. the best course of action would be to find a different source for your scrapping.
You need to use a specific url (api) which contains the json.
edit :
According to the url provided by #QHarr and if you want to retrieve all informations from url, try
function parseValues(url) {
const jsn = JSON.parse(UrlFetchApp.fetch(url).getContentText())
const headers = Object.keys(jsn[0]);
return ([headers, ...jsn.map(obj => headers.map(header => Array.isArray(obj[header]) ? obj[header].join(",") : obj[header]))]);
}
and in your sheet
=parseValues("https://api-osmosis.imperator.co/tokens/v1/DSM")

How to skip a UrlFetchApp when the url is empty and still run the script

Sometimes there will be no url available to fetch, basically sometimes I will add an image and other times I will not, but I still would like the rest of the script to run. At the moment the lack of the url is stopping the entire thing.
I am getting this error when there is no available url to fetch in A1Image cell:
Error Exception: Attribute provided with no value: url
Are there different methods to make the script run and pretty much ignore/skip the Url Fetch App fetch (A1Image) only when A1Image cell is empty?
Here is the script part to show A1Image url as an image in the PDF:
// Insert image1
var element = body.findText("Image1").getElement();
var blob = UrlFetchApp.fetch(A1Image).getBlob();
var image = element.getParent().asParagraph().insertInlineImage(0, blob);
image.setHeight(400);
image.setWidth(600);
if(A1Image) {
var blob = UrlFetchApp.fetch(A1Image).getBlob();

Why is this Importxml formula not working?

The following formula does work for some, but not for others:
=IFNA(VALUE(IMPORTXML("https://finance.yahoo.com/quote/C2PU.SI", "//*[#class=""D(ib) Mend(20px)""]/span[1]")))
If used without IFNA, it says 'Resource at url not found'.
Here's the value I'm trying to pull in:
I appreciate if you could point me to the right direction.
Thank you!
It does not return any values even for simple importxml.
It seems the site is generated by javascript or protected so it can't be scraped by importxml.
Don't use the "inspect" tool as it will show the DOM as it's being rendered by the web browser including modifications to the source code by client-side JavaScript, instead look at the source code.
Resources
How to know if Google Sheets IMPORTDATA, IMPORTFEED, IMPORTHTML or IMPORTXML functions are able to get data from a resource hosted on a website?
The structure of the DOM is generated by javascript. Nevertheless, all informations you need are contained by a json string called here root.App.main. You can get all the data by these way
function extract(url){
var source = UrlFetchApp.fetch(url).getContentText()
return source.match(/(?<=root.App.main = ).*(?=}}}})/g) + '}}}}'
}
and then retrieve the data by conventionnal json parsing. This will give you the value
[![function marketPrice() {
var code = 'C2PU.SI'
var url='https://finance.yahoo.com/quote/' + code
var source = UrlFetchApp.fetch(url).getContentText()
var jsonString = source.match(/(?<=root.App.main = ).*(?=}}}})/g) + '}}}}'
var data = JSON.parse(jsonString)
var regularMarketPrice = data.context.dispatcher.stores.StreamDataStore.quoteData.item(code).regularMarketPrice.raw
Logger.log(regularMarketPrice)
}
Object.prototype.item=function(i){return this\[i\]};][1]][1]

How to take a text field and set it as the description

I am working in google Apps Scripts, specifically with Google Drive. I am trying to make an application that when the user types in text into the text field, the program takes the text and sets its description using setDescription(). Everything is working fine, i just cannot figure out how to take whatever text the user enters and set it as the description.
Thanks in advance
First get the value of input
Example code from this SO answer:
var textbox = app.createTextBox().setName("textboxName");
var buttonHandler = app.createServerHandler("updateLabelText").addCallbackElement(textbox);
var button = app.createButton("Click me").addClickHandler(buttonHandler);
var label = app.createLabel().setId("label");
Then in your function:
function updateLabelText(e) {
var app = UiApp.getActiveApplication();
app.getElementById("label").setText(e.parameter.textboxName);
return app;
}
Assign a Simple Trigger
doGet(e) runs when a user visits a web app or a program sends an HTTP GET request to a web app.
doPost(e) runs when a program sends an HTTP POST request to a web app.
Set Text as Description
// Trash every untitled spreadsheet that hasn't been updated in a week.
var files = DriveApp.getFilesByName('Untitled spreadsheet');
files.setDescription(description)

How to (from a Google Spreadsheet) get the ID of a linked form

I have a Google Spreadsheet that a form is linked to and all form responses are stored in. What I am trying to find is the ID of the FORM itself. I tried this but this does not work..
(I'm running the following code FROM The script editor IN the spreadsheet that the form is linked to.)
function getID()
{
var form = FormApp.getActiveForm();
var formID = form.getId();
Logger.log(formID);
}
That returns NULL since the script is container-bound to the spreadsheet itself. Is there any other way to get the ID of the linked form or even the URL of the linked form?
I can MANUALLY get it by doing the following from the Spreadsheet.
Form > Edit form
This will show me the URL.
IF I knew the NAME of the form I could get it by name using the DriveApp.getFilesByName(), iterate through it and then use the File.getId() but I don't necessarily know the name.
Any ideas?
To avoid parsing the url, the safest way would be:
Logger.log( (FormApp.openByUrl(SpreadsheetApp.getActiveSpreadsheet().getFormUrl())).getId() );
or long hand version:
function logFormId_LongHand(){
var formURL = SpreadsheetApp.getActiveSpreadsheet().getFormUrl();
var form = FormApp.openByUrl(formURL);
var formId = form.getId();
Logger.log( formId );
}
To get the form object in a spreadsheet app script I use the following function:
function getLinkedForm(){
return FormApp.openByUrl( SpreadsheetApp.getActiveSpreadsheet().getFormUrl() );
}
To anyone else who stumbles upon this: The new spreadsheets allow this. I've tried it already and it works for me. Here is how I get the ID:
var formUrl = SpreadsheetApp.getActiveSpreadsheet().getFormUrl();
var formID = formUrl.match(/[-\w]{25,}/);
I got the regex from this question: Easiest way to get file ID from URL on Google Apps Script
Hope this helps.
If you only want the Form ID for the attached Form, and don't need to keep the Form URL for any reason, this one-liner should suffice:
var formID = SpreadsheetApp.getActiveSpreadsheet().getFormUrl().match(/\/d\/(.{25,})\//)[1];
Yes.
It would be a script like this that would be ran from within the spreadsheet:
function getFormUrl()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formUrl = ss.getFormUrl();
Logger.log(formUrl);
}
However, if you try and run this in the new spreadsheets you receive this error message:
The api method 'getFormUrl' is not available yet in the new version of Google Sheets.
So, until they add support it will only work in older versions.
You can also find out / connect to the original form from the "Help" menu. Type form and then click "Edit Form." It will locate the original form. This was helpful when a search in drive was unsuccessful through the name of the form (???). So if you need the original editable form for whatever reason, can't find it, and have the linked Sheets doc, then you can now backtrack and go from there. :)