trying to get 'Growth Rate' to google sheet - google-sheets

for the following example i try to get Growth Rate for ticket in index C1:
=IMPORTXML(concatenate("https://www.gurufocus.com/term/dividend_growth_5y/",index(C1),"/5-Year-Dividend-Growth-Rate/"),"//*[#id="target_def_description"]/p[2]/strong[9]")
the data is in the following page:
https://www.gurufocus.com/term/dividend_growth_5y/T/5-Year-Dividend-Growth-Rate/ATT-Inc#:~:text=AT%26T's%20Dividends%20per%20Share%20for,Rate%20was%201.80%25%20per%20year.
please advice
10x
Y.

When I tested your formula using your URL, an error of Could not fetch url occurs. So in this answer, as a workaround, I would like to propose to use Google Apps Script for achieving your goal.
Sample script:
Please copy and paste the following script to the script editor of Spreadsheet, and put a custom formula of =SAMPLE(url) in a cell. By this, the script is run.
function SAMPLE(url) {
const res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
if (res.getResponseCode() != 200) throw new Error(res.getContentText());
const meta = res.getContentText().match(/<meta name\="description"[\s\S\w]+?>/);
if (meta) {
const gr = meta[0].match(/ ([0-9.]+%)/);
if (gr && gr.length == 2) {
return gr[1];
}
}
return "Value cannot be retrieved.";
}
Result:
When above script is run for the URL of https://www.gurufocus.com/term/dividend_growth_5y/T/5-Year-Dividend-Growth-Rate/ATT-Inc#:%7E:text=AT%26T%27s%20Dividends%20per%20Share%20for,Rate%20was%201.80%25%20per%20year and https://www.gurufocus.com/term/dividend_growth_5y/T/5-Year-Dividend-Growth-Rate, the following result is obtained.
Note:
This sample script can be used for your URLs of https://www.gurufocus.com/term/dividend_growth_5y/T/5-Year-Dividend-Growth-Rate/ATT-Inc#:%7E:text=AT%26T%27s%20Dividends%20per%20Share%20for,Rate%20was%201.80%25%20per%20year and https://www.gurufocus.com/term/dividend_growth_5y/T/5-Year-Dividend-Growth-Rate. So when you change the URL, the script might not be able to be used. So please be careful this.
References:
Custom Functions in Google Sheets
Class UrlFetchApp

Related

How do I use the split function in Google Sheets correctly?

I want to get data from the text found in 2.. In 1. Is the data I have available and in 3. You will find the wanted result.
I have the following information available
Categories
Kleur
Soorthek
Bevestigingswijze
I am getting this text from scraping: BevestigingswijzeKlembevestigingKleurWitSoorthekSpijlenhek
I want this as a result by using a function in Google Sheets.
Wanted Result
KleurWit
SoorthekSpijlenhek
BevestigingswijzeKlembevestiging
Thank you in advance!
You could accomplish this with an Apps Script custom function. To achieve this, follow these steps:
In your spreadsheet, select Tools > Script editor to open a script bound to your file.
Copy this function in the script editor, and save the project:
function splitScrape(categories, scrape) {
const indexes = categories.map(c => scrape.indexOf(c[0])).sort();
const split = indexes.map((index, j) => scrape.slice(index, indexes[j+1]));
return split;
}
The sample above won't detect multiple occurrences of the same category in the scrape string, and it will only handle the first one (if a second parameter is not provided, indexOf only detects the first occurrence). In order to detect multiple occurrences, I'd suggest replacing the function with this one:
function splitScrape(categories, scrape) {
const indexes = [];
categories.flat().filter(String).forEach(c => {
let index = scrape.indexOf(c);
while (index > -1) {
indexes.push(index);
index = scrape.indexOf(c, index + 1);
}
});
indexes.sort((a, b) => a-b);
const split = indexes.map((index, j) => scrape.slice(index, indexes[j+1])).filter(String);
return split;
}
Now, if you go back to your spreadsheet, you can use this function like any in-built one. You just have to provide the appropriate ranges where the Categories and the scrape string are located, as you can see here:
Reference:
Custom Functions in Google Sheets

=importxml, Website to Google Sheets - getting #N/A every time

Website Link
https://redacted
xml options I have tried so far
<span aria-labelledby="amount">722</span>
//*[#id="amount"]/h3/span[2]
/html/body/div[3]/main/div/span/div/div/div[2]/div/div/div[2]/div/div[2]/div[3]/div/div/div/div[2]/div[1]/h3/span[2]
None working
Trying to =importxml from here # a value of "722" this is value on 5/5/22 anyway.
Unfortunately, it seems that your expected value cannot be directly retrieved using the XPath. Because the value is put to the HTML using Javascript and IMPORTXML cannot analyze the result of Javascript. But, fortunately, it seems that your expected value is included in the HTML as the JSON data. So, in this answer, I would like to retrieve the value from the JSON data.
Pattern 1:
In this pattern, IMPORTXML and REGEXEXTRACT are used.
=ARRAYFORMULA(REGEXEXTRACT(IMPORTXML(A1,"//script[#data-component-name='GetOfferWrapper']"),"defaultEstimatedValue"":(.+?)}"))
The URL https://www.gazelle.com/iphone/iphone-13-pro-max/other/iphone-13-pro-max-1tb-other/498082-gpid is put in the cell "A1".
When this formula is used, the following result is obtained.
Pattern 2:
In this pattern, a custom function created by Google Apps Script is used. When the value is retrieved from JSON data, Google Apps Script is useful. When you use this script, please copy and paste the following script to the script editor of Spreadsheet and save the script. And, please put a custom function of =SAMPLE("https://www.gazelle.com/iphone/iphone-13-pro-max/other/iphone-13-pro-max-1tb-other/498082-gpid") to a cell.
function SAMPLE(url) {
const res = UrlFetchApp.fetch(url).getContentText();
const data = res.match(/<script.+data-component-name="GetOfferWrapper".+?>([\w\s\S]+?)<\/script>/);
if (!data || data.length == 0) return "No data";
const obj = JSON.parse(data[1]);
return obj.initState.defaultEstimatedValue;
}
The URL https://www.gazelle.com/iphone/iphone-13-pro-max/other/iphone-13-pro-max-1tb-other/498082-gpid is put in the cell "A1".
When this formula is used, the value of 722 is retrieved.
Note:
The formula and custom function can be used for the current HTML. So, when the specification of HTML is changed, those might not be able to be used. Please be careful about this.
References:
IMPORTXML
REGEXEXTRACT
Custom Functions in Google Sheets
fetch(url)
JSON.parse()
you will need to find another site with intel you attempting to scrape. the #N/A error is the result of google sheets not supporting the import of JavaScript elements. you can always check for compatibility by disabling JS in site settings and only what's left can be usually scrapped. in this case its nothing:

Importxml() returned "empty cells" or "formula parse error"

I tried Importhtml ("https://nepsealpha.com/investment-calandar/dividend","table",) and then Importxml("https://nepsealpha.com/investment-calandar/dividend",xpath). I found out xpath from "selectorgadget" extension of googlechrome, but still couldn't import it. It shows either "empty content" or formula parse error".
You can retrieve quite all the informations this way
=importxml(url,"//div/#data-page")
and then parse the json.
By script : =getData("https://nepsealpha.com/investment-calandar/dividend")
function getData(url) {
var from='data-page="'
var to='"></div></body>'
var jsonString = UrlFetchApp.fetch(url).getContentText().split(from)[1].split(to)[0].replace(/"/g,'"')
var json = JSON.parse(jsonString).props.today_prices_summary.top_volume
var headers = Object.keys(json[0]);
return ([headers, ...json.map(obj => headers.map(header => obj[header]))]);
}
edit
to update periodically, add this script
function update(){
var chk = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange('A1')
chk.setValue(!chk.getValue())
}
put a trigger as you wish on the update function and change as follows
=getData("https://nepsealpha.com/investment-calandar/dividend",$A$1)
I know that's not the answer you want to see.
It's impossible to get any content from this website using IMPORTXML or other tools included in Google Sheets.
It's generated using Javascript. Once Javascript is disabled no content is displayed:
It's done on purpose. Financial companies pay for live stock data and they don't want to share it with us for free.
So the site is protected against tools like importxml.

IMPORTJSON to google sheets with an underscore (_) in the column's name

I'm trying to use IMPORTJSON to import some data from:
https://templeosrs.com/api/player_stats.php?player=Mikael&date=1639587712
to my google spreadsheet. For some columns, like "Ehp", I can get the value just fine with
=VALUE(QUERY(TRANSPOSE(IMPORTJSON(C5)), "select Col2 where Col1 = 'Ehp'"))
where C5 is just a cell with the link above in it. But this doesn't seem to work for any column that has an underscore in it, like "Ehp_rank".
I should say I understand nothing about how any of this works, I'm doing everything by looking at examples I found, then just trial and error. Can someone help me import columns with an underscore?
Also, I'd like to import the "Username", but it's inside that info block and I can't seem to get it right. I've tried "info.Username", changing Col2/1 to Col3/2, but no sucess.
In that case, how about directly preparing a script as follows?
Sample script:
Please copy and paste the following script to the script editor. And, please put a custom formula of =SAMPLE("https://templeosrs.com/api/player_stats.php?player=Mikael&date=1639587712"). By this, in this case, 90 is returned.
function SAMPLE(url) {
const res = UrlFetchApp.fetch(url).getContentText();
const obj = JSON.parse(res);
return obj.data && obj.data.Ehp_rank ? obj.data.Ehp_rank : "";
}
Reference:
fetch(url)

Google spreadsheet direct download link for only ONE sheet as excel

I was wondering if its possible to download say only sheet 1 of a google spreadsheet as excel? I have seen few SO posts that show the method to export the WHOLE sheet as excel, but I need to just export one sheet. Is it at all possible? and if yes, how?
You can download a specific sheet using the 'GID'.
Each sheet has a GID, you can find GID of specific sheet in the URL of
spreadsheet. Then you can use this link to download specific sheet -
https://docs.google.com/spreadsheets/d/<KEY>/export?format=xlsx&gid=<GID>
ex:
https://docs.google.com/spreadsheets/d/1D5vzPaOJOx402RAEF41235qQTOs28_M51ee5glzPzj0/export?format=xlsx&gid=1990092150
KEY is the unique ID of the spreadsheet.
source: https://www.quora.com/How-do-I-download-just-one-sheet-from-google-spreadsheet/answer/Ranjith-Kumar-339?srid=2YCg
From what I've found, the other two answers on this post are exactly correct, all you need to do is replace this:
/edit#gid=
with:
/export?format=xlsx&gid=
This works just fine although I did find that I had to keep looking up this string and copying it. Instead, I made a quick Javascript snippet that does all the work for you:
Just run the code snippet below and drag the link it creates into your bookmarks bar. I know this is a little hacky but for some reason, stackoverflow doesn't want me injecting javascript into the links I provide.
Export Sheet as Excel
I've tested this on the latest versions of Chrome, Safari, and Firefox. They all work although you might have to get a little creative about how you make your bookmarks.
when you see every Google spreadsheet url looks like this
https://docs.google.com/spreadsheets/d/1D5vzPaOJOx402RAEF41235qQTOs28_M51ee5glzPzj0/edit#gid=1078561300
In every spreadsheet URL we can see: /edit#gid=
this is generally the default mode.
/edit#gid=
just replace it with:
/export?format=xlsx&gid=
it will download the single spreadsheet from the workbook
I am able to download all sheets of a spreadsheet.
Just remove anything after
/edit?
and replace with
/export?format=xlsx
for Excel
or
/export?format=pdf
for PDF
Please use any_value() function before the column because field(column) have more than one value for one id(group by).
like-
select any_value(phone_no) from user_details group by user_id.
here one user_id have more than one phone number so query confused which choose.
You can do this by clicking on the down arrow near the sheet name to bring up the options, and then selecting "Copy to -> New spread sheet", then click the "Open spread sheet" in the pop up that comes up after.
You can use my code:
function emailAsExcel() {
var config = {
to: "name#gmail.com",
subject: "your text",
body: "your text"
};
var ui = SpreadsheetApp.getUi();
if (!config || !config.to || !config.subject || !config.body) {
throw new Error('Configure "to", "subject" and "body" in an object as
the first parameter');
};
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheetId = spreadsheet.getId();
var file = Drive.Files.get(spreadsheetId);
var url = 'https://docs.google.com/spreadsheets/d/'+spreadsheetId+'/export?
format=xlsx&gid=numberSheetID to email';
var token = ScriptApp.getOAuthToken();
var response = UrlFetchApp.fetch(url, {
headers: {
'Authorization': 'Bearer ' + token
};
});
var fileName = (config.fileName || spreadsheet.getName()) + '.xlsx';
var blobs = [response.getBlob().setName(fileName)];
if (config.zip) {
blobs = [Utilities.zip(blobs).setName(fileName + '.zip')];
}
GmailApp.sendEmail(
config.to,
config.subject,
config.body,
{
attachments: blobs
}
);
}

Resources