Googlesheet importxml find value using xpath - google-sheets

I need to get the integer value "maximum drawdown" from this website link =https://www.mql5.com/en/signals/1414510,
from google chrome i tried copy the xpath
//*[#id="radarChart"]/svg/text[8]/tspan[2]
then in google sheet, i write:
=importxml(https://www.mql5.com/en/signals/1414510, "//*[#id='radarChart']/svg/text[8]/tspan[2]")
It cant retrieve the value,
How to write the correct xpath to the value?
thanks

by xpath
Try for https://www.mql5.com/en/signals/1409351
=regexextract(importxml(A1,"//div[#class='s-data-columns hoverable']"),"\((.*)\)")
i highly recommand to test first this formula to have an overview
=importxml(A1,"//div[#class='s-data-columns hoverable']")
by json
the second way could be to parse the json contained in the source (var radarChart)
function radarChart(url){
var source = UrlFetchApp.fetch(url, {muteHttpExceptions: true}).getContentText().replace(/(\r\n|\n|\r|\t| )/gm,"")
var data = source.split(`new svgRadarChart($('radarChart'),[`)[1].split(']')[0]
var values = (data.match(/value : ([^,]+)/g))
var names = (data.match(/name : ([^,]+)/g))
var val
for (var i=0;i<names.length;i++){if (names[i]==`name : 'Maximum drawdown'`){val=values[i]}}
return (val)
}
or
function radarChart(url){
var source = UrlFetchApp.fetch(url, {muteHttpExceptions: true}).getContentText().replace(/(\r\n|\n|\r|\t| )/gm,"")
var data = source.split(`new svgRadarChart($('radarChart'),[`)[1].split(']')[0]
var values = (data.match(/value : ([^,]+)/g))
var names = (data.match(/name : ([^,]+)/g))
var result = []
for (var i=0;i<names.length;i++){
result.push([names[i].replace('name : ','').replace(/'/g,''),values[i].replace('value : ','')])
}
return (result)
}

Related

Skip empty value in Google Sheets

I have running script in google sheets for submitting data, but I have no idea how to skip empty values
Here is my script :
function SubmitData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formS = ss.getSheetByName("FORM");
var dataS= ss.getSheetByName("DATA");
var values = formS.getRange(8,1,20,5).getValues();
dataS.getRange(dataS.getLastRow()+1,3,20,5).setValues(values);
}
Thank You
var values = formS.getRange(8,1,20,5).getValues();
values = values.filter(e=>e.join('') !== '');
dataS.getRange(dataS.getLastRow()+1,3,values.length,5).setValues(values);

How to share filtered data with same filter view in google sheets?

I am using google spreadsheet and this is my code to import data and save my filter on it.
function importCSVFromWeb() {
// Provide the full URL of the CSV file.
var csvUrl = "https://covid19.who.int/WHO-COVID-19-global-table-data.csv";
var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
var csvData = Utilities.parseCsv(csvContent);
var sheet = SpreadsheetApp.getActiveSheet();
// --- I added below script.
var filter = sheet.getFilter();
var range = filter.getRange();
var criteria = [];
var start = range.getColumn();
var end = start + range.getNumColumns() - 1;
for (var i = start; i <= end; i++) {
var criterion = filter.getColumnFilterCriteria(i);
if (criterion) criteria.push({col: i, c: criterion.copy()});
}
filter.remove();
// ---
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
// And, I added below script.
criteria.forEach(({col, c}) => range.createFilter().setColumnFilterCriteria(col, c));
}
My issue is: How can i share the spreadsheet with my filter applied on it to use it ?
In my case, I want to use another visualization tool on only filtered data

CopyTo Google sheets

I'm trying to get this code to work but keep getting a "TypeError: offset.copyTo is not a function". The script is supposed to take one value from sheet "Budget" and copy it into the next available row in column F on sheet "Projected".
I have tried playing around with syntax and other ways to copy but to now avail, any help would be greatly appreciated
function OffsetRecord() {
var sheet = SpreadsheetApp.openById("xx").getSheetByName("Budget");
var offset = sheet.getRange('I5').getValue();
var destSheet = SpreadsheetApp.openById("xx").getSheetByName("Projected");
var colF = destSheet.getRange('F:F').getValues();
var fcolF = colF.filter(function (e) {return e[0];});
var firstEmptyRowIncolF = fcolF.indexOf('')+1;
offset.copyTo(firstEmptyRowIncolF, {contentsOnly: true});
copyTo expects ranges as parameters, not values or column numbers, see here
Modify your code as following:
function OffsetRecord() {
var spreadsheet = SpreadsheetApp.openById("XX");
var sheet = spreadsheet.getSheetByName("Budget");
var offset = sheet.getRange('I5');//without ".getValue();"!
var destSheet = spreadsheet.getSheetByName("Projected");
...
firstEmptyRowIncolF = ...;
var destinationColumn = 1;
var range = destSheet.getRange(firstEmptyRowIncolF, destinationColumn);
offset.copyTo(range, {contentsOnly: true});
}

Google Spreadsheet Script: setValue

This is my code (it is fully working as intended).
function concatenate() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Responses");
var datarange = sheet.getDataRange();
var lastrow = datarange.getLastRow();
for (i=lastrow;i>=2;i--) {
var datad = sheet.getRange(i, 4).getValue();
var datae = sheet.getRange(i, 5).getValue();
var concatde = sheet.getRange(i, 4).setValue(sheet.getRange(i, 1).getValue());
}
}
However my question is regarding the line var concatde = sheet.getRange(i, 4).setValue(sheet.getRange(i, 1).getValue()); in my code.I wish to add a string at the end of the setValue as well shown in this line.
var concatde = sheet.getRange(i, 4).setValue(sheet.getRange(i, 1).getValue(), "A");
However, when I run it with the string "A", I get this error message:
Cannot find method setValue(object,string). (line 12, file "Code")
Any suggestions for me to be able to have a string at the end of it as well?
From what I can see setValue() only takes in one parameter.
Google Apps Script is just javascript so you can do something like below:
var concatde = sheet.getRange(i, 4).setValue(sheet.getRange(i, 1).getValue() + "A");
Note the '+' after getValue().

Using Google Sheets and Google Scripts - How do I insert an entry from a cell into a variable?

This is my first time playing with a JSON API and my java is super rusty.
I'm trying to do the following:
Pull a string from a Google Sheets cell into function getSymbol (name). In this case, the string should be "Ethereum"
Insert the name variable into a url string, which is where the JSON I want to pull lives. In this case, the API output looks like this:
[
{
"id": "ethereum",
"name": "Ethereum",
"symbol": "ETH",
"rank": "2",
"price_usd": "95.3675",
"price_btc": "0.0605977",
"24h_volume_usd": "152223000.0",
"market_cap_usd": "8713986432.0",
"available_supply": "91372705.0",
"total_supply": "91372705.0",
"percent_change_1h": "0.38",
"percent_change_24h": "1.38",
"percent_change_7d": "37.07",
"last_updated": "1494105266"
}
]
Next I want to pull the "symbol" item from the JSON and return that back to the spreadsheet. In this case, the function would take in "Ethereum" and return "ETH".
Below is my code. Whenever I run it I get an error saying my name variable is undefined, resulting in a URL that looks like this (says "undefined" instead of "ethereum"):
https://api.coinmarketcap.com/v1/ticker/undefined/?convert=USD
What am I doing wrong?
function getSymbol (name) {
var url = "https://api.coinmarketcap.com/v1/ticker/"+name+"/?convert=USD";
var response = UrlFetchApp.fetch(url);
var text = response.getContentText();
var json = JSON.parse(text);
var sym = json["symbol"];
return sym;
}
The return type is an array of objects i.e. json = [{object1},{object2}]
Even though there is just one element, you still need to access it like so
var sym = json[0]["symbol"]
//or
var sym = json[0].symbol
Your final code will look like this:
function getSymbol (name) {
var url = "https://api.coinmarketcap.com/v1/ticker/"+name+"/?convert=USD";
var response = UrlFetchApp.fetch(url);
var text = response.getContentText();
var json = JSON.parse(text);
var sym = json[0]["symbol"];
return sym;
}

Resources