Using google script to manipulate data conditional formating and data validation - javascript

I was looking for already 3 days but nowhere found how to manipulate those two tools by google script.
I am interested in maiking script runned dynamic creation of worksheets and fast edditing of worksheets.
I need to alter those: data validation and conditional formating which are available normaly on tool bar.
Please help, give me the name of class that consists of those or the functions in google script with little information I will find rest.
Thanks in adavance
Tom

Data validation can be set using Range.setDataValidation(). Unfortunately Apps Script doesn't support setting conditional formatting, but you can star this feature request to get notified if/when it's added.

The tools you are asking about appear to be in the Range class of the Google Apps Script Spreadsheet API. Note that the API calls deal with a range of cells at a time, although you can easily define a range that contains only one cell.
The question in its current form has nothing to do with Google App Engine, so consider taking that off the question tags.

Related

I can't find Xpath on this website or maybe wrong syntax

I'm trying to scrape data from this url https://drive.getbigger.io/#/stores, however I didn't find the Xpath of the text I want to export, which are the producer's offer.
Firstly I try the importxml function in Google sheet:
=IMPORTXML(A1;"/html/body/flt-ruler-host/div[23]/p")
and it gave me N/A error "the imported content is empty"
so I try to scrape this website with add-ons and Parsehub, and it gave me every time a .csv file where i can't find the data I want to export.
Also I can't find the right Xpath for the data I would like to scrape, when I use the inspection tool, the data isn't in the <body> part.
However the Xpath I use in my importXML function is some code I find in the <body> part and which is close of the text I'd like to extract (the producer's offer).
It seems that the Xpath I am looking for is linked in the <head> part with some JavaScript code, also when I hover the page with the selection tool in order to scrape the data it select the whole page, maybe because there is a "scroll <div>".
So I wonder if the website use some kind of protection against scraping or other.
Please guys tell me if :
I could find the right Xpath in order to scrape with the importXML function?
Should I extract the data with a python script?
if the website block my attempts, how could I do this?
You won't be able to scrape anything with IMPORTXML formula since the website uses dynamic rendering (javascript).
So yes, Python+Selenium (or other combinations) could do the job. The website won't block you if you follow some rules (switch user-agent, add pauses between requests).
You would probably need these XPath :
Product description :
//p[1][string-length(text())>5][parent::flt-dom-canvas]
Product price :
//p[3][contains(text(),"€") and not (contains(text(),","))][parent::flt-dom-canvas]
However, I think the most elegant way to get the data is probably to use the API the website relies upon. With GoogleSheets and a custom ImportJSON script, you can obtain something like this (result for "fromage" as query) :
It won't work out of the box, you'll have to modify some parts of the script since it won't load a JSON (called with POST) which needs headers in the request. In a nutshell, you need to construct the payload part, add headers to the request ("Bearer XXXXX"), and add a parameter to a function to retrieve the results.
All this depends on your objective and your expected output.
EDIT : For references (constructing the payload, adding parameters) you can read :
https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetchurl,-params
Look also the networktab of your browser developper tools in order to find : the url of the API and the correct parameters to send.

Google sheets API: How to retrieve the A1 notation of an active range with javascript

I'm actually working on a web project based on Google Spreadsheet, and following the Browser quickstart method (https://developers.google.com/sheets/api/quickstart/js).
I would like to select a range on my sheet, and click on a button.
I would have a function that detect the active range's A1 notation, then stores it in a variable and display it in an input range.
Many people that are using the script editor are talkig about a "getActiveRange" function. But this function is only accessible when using the script editor, and not from my js file.
Is there another way to get the A1 notation of my selected range?
Using Sheets API, what you're asking isn't straight up available. Like what you've mentioned, this is somewhat doable using App Script's getActiveRange() but for Sheets APIv4, there's no ad-hoc to do that. The only supported methods in Sheets API v4 is found in the official reference.
You should be able to select a Range with the spreadsheets.values.get Method, take a look here:
https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get
Does that help you?

"make this a quiz" and create answer key using FormApp in google spreadsheet script

I wrote a google spreadsheet script that parses a spreadsheet and creates a form with the data.
Once the form is created, I can manually go to the form settings -> Quizzes and then "make this a quiz". Once I make it a quiz, I can manually assign points and a correct answer to each problem.
Does anyone know a way to script this when creating the form? Specifically, in my google spreadsheet, my script uses FormApp to create the form and add questions. When the script creates the form, I want to add the setting "make this a quiz", and when the script creates each questions, I want to set the answer key and assign point values. I am not sure this ability exist as I could not find it in the FormApp class.
Thank you
On April 27, 2017, Google added many new Google Script methods for handling Forms. The announcement is on the G Suite Developers Blog: Create Quizzes in Google Forms with Apps Scripts Among the additions is the isQuiz() function, which allows you to set the form as a quiz. The documentation (although very brief) is at the Google Developers site. The Developers Blog has a detailed explanation (with sample code) of how to create a quiz using Apps Script.

Use Site Property in eSpace Javascript

I included Google Analytics (javascript) in my Outsystems website via de eSpace Javascript. Now I want to place the Analytics Key in my Site Properties so I can update it easily for every environment.
How can I use a Site Property in my Javascript?
You can create a site property to store the Tracking ID.
site Property screenshot
Second, you need to create a webblock with an unescaped expression, and add your javascript this way:
weblock expression screenshot
Finally, you just need to drag you weblock to each webpage you want to track.
cheers,
Vera
As far as I know, you cannot use Site Properties in the eSpace JavaScript window. For that, you have to use an escaped expression on a web screen or web block to add your JavaScript code along with the use of Site Properties.
Since you want the same script on all the web screens, I suggest that you add this expression in the Footer web block, so that it will be automatically added to all the web screens you create.
I can understand your use case. If I read it correctly, you're trying to use some JavaScript in one espace, that would be run in every page load, something like an
onLoad(function(){
// your Google Analytics code, but using the value from the site property
})
And in this way, you would be able to update the site property without the need to republish all consumers. Seems like a nice approach :)
On way to be able to achieve this, would be to have your JavaScript to request the key on the fly to the server side, and maybe cache it.
This can be easier or harder depending on the Platform version you're running... But here's a simple way to achieve it.
Add the site property to the espace. Build a page that has no layout, and in the preparation, add a download widget that only downloads the value of your site property. In the same espace, in the espace JavaScript, add an AJAX request to the page I was referring to before, and when you get the response back, start your Google Analytics code.
To be able to use this in every other espace, and in every page, you still need to reference something from the Google Analytics espace though, so that espace JavaScript is run in every page
Hope it helps :)

How to check whether the html has changed?

Apologies if that's the wrong place, but have no clue where to ask.
We are building Firefox addon that works on selected websites. Now, because those websites tend to change once in a while, I want to run once a day a javascript script that will check whether the specific part of code has been changed, and if it did, we would get some return message (ideally directly to my email).
What would be the easiest, simplest way to do that? I've read about webcrawlers, webscrapers, but they seem to be doing far more than we need. We basically want to run from our PCs connected to internet a small script that will do that simple check and send us email as soon as the change to the specific part of html body has been detected (or to be more specific - selected elements have not been found).
Any help greatly appreciated
Basically you might set up Google Spreadsheet to scrape pages' parts thru IMPORTXML function (here with an example) using xpath. Then you set up notifications in a spreadsheet: Tools -> Notification Rules
Now each time the scraping function (IMPORTXML) gets content that is different to previous one, spreadsheet should trigger a mail to you.
As an off-the-shelf alternative I also might recommend some tools that track pages' and/or their parts'(!) changes including notifying by email.

Categories