Exception: Limit Exceeded: URLFetch URL Length on GOOGLE APPS SCRIPT [duplicate] - google-apps-script

I am pulling data from an API which unfortunately has very big urls/end points.
Is there any way to shorten urls in order to bypass the Exception: Limit Exceeded: URLFetch URL Length error?
I found this: https://github.com/amitwilson/GoogleAppsScript/blob/master/LinkShortner but I don't know how to use it properly since most of the api calls i make are generated dynamically.
This is an example link (letters after "cursor=" is a cursor key which is different every time):
https://api.x.immutable.com/v1/orders?include_fees=false&status=filled&buy_token_address=0xacb3c6a43d15b907e8433077b6d38ae40936fe2c&order_by=updated_timestamp&direction=desc&user=0x72a2ffa8be361d4a5d575c55b7382d8c6317f77d&page_size=200&cursor=eyJvcmRlcl9pZCI6MTk0NTk2NzUyLCJzZXFfbnVtIjowLCJzdGF0dXMiOiJmaWxsZWQiLCJzdGFya19rZXkiOiIweDA3MWU2NjU0NTMzMGJiOGY5NDQ4ZWU1YTM3YTI5ODViZGYzYjQ1NzYwNDA5ODE4YmEwYTY0NmUxNjU4NTdhMWIiLCJ2YXVsdF9pZCI6MCwiZXRoZXJfa2V5IjoiMHg3MmEyZmZhOGJlMzYxZDRhNWQ1NzVjNTViNzM4MmQ4YzYzMTdmNzdkIiwic2VsbF90b2tlbl90eXBlIjoiRVRIIiwic2VsbF90b2tlbl9pZCI6IiIsInNlbGxfY2xpZW50X3Rva2VuX2lkIjoiIiwic2VsbF90b2tlbl9hZGRyZXNzIjoiIiwic2VsbF90b2tlbl9kZWNpbWFscyI6MTgsInNlbGxfcXVhbnRpdHkiOiIyODQzMDAwMDAwMDAwMDAwIiwic2VsbF9hc3NldF9uYW1lIjpudWxsLCJzZWxsX2Fzc2V0X2ltYWdlX3VybCI6bnVsbCwic2VsbF9jb2xsZWN0aW9uX25hbWUiOm51bGwsInNlbGxfY29sbGVjdGlvbl9pY29uX3VybCI6bnVsbCwiYnV5X3Rva2VuX3R5cGUiOiJFUkM3MjEiLCJidXlfY2xpZW50X3Rva2VuX2lkIjoiMjI0ODUzMTMiLCJidXlfd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VE5sWWpCbE16YzBPVEpqTUdWaU5UbGxOVGdpTENBaWNtVmphWEJwWlc1MFgzTjBZWEpyWDJ0bGVTSTZJQ0l3ZURBMk1UY3haVEpoWlRRMU5qTXlNVE15TWpaak5XWm1aV1JpT1RSaE9UVXlOV0ppWm1Zell6bGtZMkpoWm1KaFpURTVabUk0TjJOa1ltSXpOMk5sTVdVaWZWMD0ifQ

In your situation, for example, how about the following sample script?
Sample script:
Before you use this script, please set apiKey and yourDynamicLinkDomain. Your yourDynamicLinkDomain might be like https://###.page.link.
const apiKey = "###"; // Please set your API key.
const yourDynamicLinkDomain = "###"; // Please set your dynamic link domain.
function getShortUrl_(longUrl) {
const url = "https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=" + apiKey;
const options = {
payload: JSON.stringify({
dynamicLinkInfo: {
dynamicLinkDomain: yourDynamicLinkDomain,
link: longUrl,
},
}),
contentType: "application/json",
};
const res = UrlFetchApp.fetch(url, options);
const { shortLink } = JSON.parse(res.getContentText());
return shortLink;
}
// Please run this function.
function main() {
// do something for retrieving the long URL in your actual script.
const longUrl = "###"; // It supposes that your long URL is put to longUrl.
const shortUrl = getShortUrl_(longUrl); // Here, the long URL is shortned. You can use this.
// If "options" is required to be used, please use this.
const options = {
};
const res = UrlFetchApp.fetch(shortUrl, options);
}
In this sample script, it supposes that by your current script, longUrl is obtained. And, this sample script converts from the long URL to the short URL with the function getShortUrl_. And, you can use this short URL using UrlFetchApp.
Note:
This sample script supposes that you have already been able to use Firebase Dynamic Links API. Please be careful about this.
If you have never been able to be used Firebase Dynamic Links API, please check the official document. And, the following flow might be useful.
please check the following flow.
Please create a new Firebase project and link it to your Google Cloud Platform Project. Ref
At this time, please set dynamicLinkDomain at "Dynamic Links".
Please enable Firebase Dynamic Links API at the API console.
Please create your API key from your Google Cloud Platform Project.
Please use this API in the above script.
When I tested this script, I could confirm that the long URL is converted to the short URL and the short URL can be used with UrlFetchApp.
If I misunderstood about but I don't know how to use it properly since most of the api calls i make are generated dynamically., please tell me.
References:
Create Dynamic Links with the REST API
Class UrlFetchApp
Added:
From your following reply,
Hey I made a sample sheet so you can see how I get the longurl it is highlighted with yellow color in 202 row. docs.google.com/spreadsheets/d/… The API works like this: it can pull 200 rows, then on the last row of each call it shows this cursor ID which must be called again in next row in order to show the next 200 rows.
Check the sample sheet on my previous reply. Longurl is highlighted with YELLOW color.
When I saw your sample Spreadsheet, it seems that you are using the long URL as =importjson(CONCATENATE("https://api.x.immutable.com/v1/orders?include_fees=false&status=filled&buy_token_address=0xacb3c6a43d15b907e8433077b6d38ae40936fe2c&order_by=updated_timestamp&direction=desc&user=0x8604808B824C4444Fe4dF3f94850ACd584C5aD7d&page_size=200&cursor=",W202),"","noTruncate,noHeaders"). But unfortunately, from your question and your reply, I couldn't notice that you are using it like that.
In this case, it is required to use my sample script by modifying as follows.
Sample script:
Please copy and paste the following script to the script editor of your Spreadsheet and save the script.
function getShortUrl(longUrl) {
const apiKey = "###"; // Please set your API key.
const yourDynamicLinkDomain = "###"; // Please set your dynamic link domain.
const url = "https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=" + apiKey;
const options = {
payload: JSON.stringify({
dynamicLinkInfo: {
dynamicLinkDomain: yourDynamicLinkDomain,
link: longUrl,
},
}),
contentType: "application/json",
};
const res = UrlFetchApp.fetch(url, options);
const { shortLink } = JSON.parse(res.getContentText());
return shortLink;
}
When this script is used for your sample Spreadsheet, please put the following formula to the cell "A203".
=importjson(getShortUrl(CONCATENATE("https://api.x.immutable.com/v1/orders?include_fees=false&status=filled&buy_token_address=0xacb3c6a43d15b907e8433077b6d38ae40936fe2c&order_by=updated_timestamp&direction=desc&user=0x8604808B824C4444Fe4dF3f94850ACd584C5aD7d&page_size=200&cursor=",W202)),"","noTruncate,noHeaders")
By this formula, the long URL retrieved with CONCATENATE("https://api.x.immutable.com/v1/orders?include_fees=false&status=filled&buy_token_address=0xacb3c6a43d15b907e8433077b6d38ae40936fe2c&order_by=updated_timestamp&direction=desc&user=0x8604808B824C4444Fe4dF3f94850ACd584C5aD7d&page_size=200&cursor=",W202) is shorten by shortenUrl(). And, use the shorten URL with importjson.
Note:
This sample script supposes that you have already been able to use Firebase Dynamic Links API. Please be careful about this.
When I tested the above script and formula to your sample Spreadsheet, I confirmed that the values are retrieved.

Related

POST request with very long URLs in Google Appscript [URLFetchApp.fetch] [duplicate]

I am pulling data from an API which unfortunately has very big urls/end points.
Is there any way to shorten urls in order to bypass the Exception: Limit Exceeded: URLFetch URL Length error?
I found this: https://github.com/amitwilson/GoogleAppsScript/blob/master/LinkShortner but I don't know how to use it properly since most of the api calls i make are generated dynamically.
This is an example link (letters after "cursor=" is a cursor key which is different every time):
https://api.x.immutable.com/v1/orders?include_fees=false&status=filled&buy_token_address=0xacb3c6a43d15b907e8433077b6d38ae40936fe2c&order_by=updated_timestamp&direction=desc&user=0x72a2ffa8be361d4a5d575c55b7382d8c6317f77d&page_size=200&cursor=eyJvcmRlcl9pZCI6MTk0NTk2NzUyLCJzZXFfbnVtIjowLCJzdGF0dXMiOiJmaWxsZWQiLCJzdGFya19rZXkiOiIweDA3MWU2NjU0NTMzMGJiOGY5NDQ4ZWU1YTM3YTI5ODViZGYzYjQ1NzYwNDA5ODE4YmEwYTY0NmUxNjU4NTdhMWIiLCJ2YXVsdF9pZCI6MCwiZXRoZXJfa2V5IjoiMHg3MmEyZmZhOGJlMzYxZDRhNWQ1NzVjNTViNzM4MmQ4YzYzMTdmNzdkIiwic2VsbF90b2tlbl90eXBlIjoiRVRIIiwic2VsbF90b2tlbl9pZCI6IiIsInNlbGxfY2xpZW50X3Rva2VuX2lkIjoiIiwic2VsbF90b2tlbl9hZGRyZXNzIjoiIiwic2VsbF90b2tlbl9kZWNpbWFscyI6MTgsInNlbGxfcXVhbnRpdHkiOiIyODQzMDAwMDAwMDAwMDAwIiwic2VsbF9hc3NldF9uYW1lIjpudWxsLCJzZWxsX2Fzc2V0X2ltYWdlX3VybCI6bnVsbCwic2VsbF9jb2xsZWN0aW9uX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SW0xaGEyVnlJaXdnSW05eVpHVnlYMmxrSWpvZ01UazBOVGsyTnpVeUxDQWljbVZqYVhCcFpXNTBYMlYwYUdWeVgydGxlU0k2SUNJd2VEVmpNell6WVRKaFlUZzJPV1F3WXpVeU1XTm1ZVE5sWWpCbE16YzBPVEpqTUdWaU5UbGxOVGdpTENBaWNtVmphWEJwWlc1MFgzTjBZWEpyWDJ0bGVTSTZJQ0l3ZURBMk1UY3haVEpoWlRRMU5qTXlNVE15TWpaak5XWm1aV1JpT1RSaE9UVXlOV0ppWm1Zell6bGtZMkpoWm1KaFpURTVabUk0TjJOa1ltSXpOMk5sTVdVaWZWMD0ifQ
In your situation, for example, how about the following sample script?
Sample script:
Before you use this script, please set apiKey and yourDynamicLinkDomain. Your yourDynamicLinkDomain might be like https://###.page.link.
const apiKey = "###"; // Please set your API key.
const yourDynamicLinkDomain = "###"; // Please set your dynamic link domain.
function getShortUrl_(longUrl) {
const url = "https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=" + apiKey;
const options = {
payload: JSON.stringify({
dynamicLinkInfo: {
dynamicLinkDomain: yourDynamicLinkDomain,
link: longUrl,
},
}),
contentType: "application/json",
};
const res = UrlFetchApp.fetch(url, options);
const { shortLink } = JSON.parse(res.getContentText());
return shortLink;
}
// Please run this function.
function main() {
// do something for retrieving the long URL in your actual script.
const longUrl = "###"; // It supposes that your long URL is put to longUrl.
const shortUrl = getShortUrl_(longUrl); // Here, the long URL is shortned. You can use this.
// If "options" is required to be used, please use this.
const options = {
};
const res = UrlFetchApp.fetch(shortUrl, options);
}
In this sample script, it supposes that by your current script, longUrl is obtained. And, this sample script converts from the long URL to the short URL with the function getShortUrl_. And, you can use this short URL using UrlFetchApp.
Note:
This sample script supposes that you have already been able to use Firebase Dynamic Links API. Please be careful about this.
If you have never been able to be used Firebase Dynamic Links API, please check the official document. And, the following flow might be useful.
please check the following flow.
Please create a new Firebase project and link it to your Google Cloud Platform Project. Ref
At this time, please set dynamicLinkDomain at "Dynamic Links".
Please enable Firebase Dynamic Links API at the API console.
Please create your API key from your Google Cloud Platform Project.
Please use this API in the above script.
When I tested this script, I could confirm that the long URL is converted to the short URL and the short URL can be used with UrlFetchApp.
If I misunderstood about but I don't know how to use it properly since most of the api calls i make are generated dynamically., please tell me.
References:
Create Dynamic Links with the REST API
Class UrlFetchApp
Added:
From your following reply,
Hey I made a sample sheet so you can see how I get the longurl it is highlighted with yellow color in 202 row. docs.google.com/spreadsheets/d/… The API works like this: it can pull 200 rows, then on the last row of each call it shows this cursor ID which must be called again in next row in order to show the next 200 rows.
Check the sample sheet on my previous reply. Longurl is highlighted with YELLOW color.
When I saw your sample Spreadsheet, it seems that you are using the long URL as =importjson(CONCATENATE("https://api.x.immutable.com/v1/orders?include_fees=false&status=filled&buy_token_address=0xacb3c6a43d15b907e8433077b6d38ae40936fe2c&order_by=updated_timestamp&direction=desc&user=0x8604808B824C4444Fe4dF3f94850ACd584C5aD7d&page_size=200&cursor=",W202),"","noTruncate,noHeaders"). But unfortunately, from your question and your reply, I couldn't notice that you are using it like that.
In this case, it is required to use my sample script by modifying as follows.
Sample script:
Please copy and paste the following script to the script editor of your Spreadsheet and save the script.
function getShortUrl(longUrl) {
const apiKey = "###"; // Please set your API key.
const yourDynamicLinkDomain = "###"; // Please set your dynamic link domain.
const url = "https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=" + apiKey;
const options = {
payload: JSON.stringify({
dynamicLinkInfo: {
dynamicLinkDomain: yourDynamicLinkDomain,
link: longUrl,
},
}),
contentType: "application/json",
};
const res = UrlFetchApp.fetch(url, options);
const { shortLink } = JSON.parse(res.getContentText());
return shortLink;
}
When this script is used for your sample Spreadsheet, please put the following formula to the cell "A203".
=importjson(getShortUrl(CONCATENATE("https://api.x.immutable.com/v1/orders?include_fees=false&status=filled&buy_token_address=0xacb3c6a43d15b907e8433077b6d38ae40936fe2c&order_by=updated_timestamp&direction=desc&user=0x8604808B824C4444Fe4dF3f94850ACd584C5aD7d&page_size=200&cursor=",W202)),"","noTruncate,noHeaders")
By this formula, the long URL retrieved with CONCATENATE("https://api.x.immutable.com/v1/orders?include_fees=false&status=filled&buy_token_address=0xacb3c6a43d15b907e8433077b6d38ae40936fe2c&order_by=updated_timestamp&direction=desc&user=0x8604808B824C4444Fe4dF3f94850ACd584C5aD7d&page_size=200&cursor=",W202) is shorten by shortenUrl(). And, use the shorten URL with importjson.
Note:
This sample script supposes that you have already been able to use Firebase Dynamic Links API. Please be careful about this.
When I tested the above script and formula to your sample Spreadsheet, I confirmed that the values are retrieved.

Extracting data from web page using Cheerio Library

I am trying to scrape very small information from a webpage using Cheerio and Google Apps Script.
I want to get the performance number from this webpage:
Following is the code snippet that I am using to get it:
function LinkResult(){
var url ='https://pagespeed.web.dev/report?url=http%3A%2F%2Fwww.juicecoldpressed.com%2F';
var result = UrlFetchApp.fetch(url);
var content = Cheerio.load(result.getContentText())
var item = content(".lh-gauge__percentage").text()
Logger.log(item)
}
As I run, this code does not show any output in the variable item. Surely there is something which I am missing, can you please guide me? Thank you.
Issue and workaround:
In this case, I'm worried that your goal might not be able to be directly achieved using the URL of https://pagespeed.web.dev/report?url=http%3A%2F%2Fwww.juicecoldpressed.com%2F and Cheerio. Because the HTML data retrieved from UrlFetchApp.fetch(url) is different from that on the browser. And, it seems that the value is calculated using a script.
Fortunately, in your situation, I thought that your values can be retrieved using PageSpeed Insights API. In this answer, I would like to propose achieving your goal using PageSpeed Insights API.
Usage:
1. Get Started with the PageSpeed Insights API.
Please check the official document for using PageSpeed Insights API. In this case, it is required to use your API key. And, please enable PageSpeed Insights API at the API console.
2. Sample script.
function myFunction() {
const apiKey = "###"; // Please set your API key.
const url = "http://www.juicecoldpressed.com/"; // Please set URL.
const apiEndpoint = `https://www.googleapis.com/pagespeedonline/v5/runPagespeed?key=${apiKey}&url=${encodeURIComponent(url)}&category=performance`;
const strategy = ["desktop", "mobile"];
const res = UrlFetchApp.fetchAll(strategy.map(e => ({ url: `${apiEndpoint}&strategy=${e}`, muteHttpExceptions: true })));
const values = res.reduce((o, r, i) => {
if (r.getResponseCode() == 200) {
const obj = JSON.parse(r.getContentText());
o[strategy[i]] = obj.lighthouseResult.categories.performance.score * 100;
} else {
o[strategy[i]] = null;
}
return o;
}, {});
console.log(values);
}
3. Testing.
When this script is run, you can see the returned value of { desktop: ##, mobile: ## } at the log. The values (the unit is %.) of desktop and mobile are the values for the desktop and the mobile, respectively.
Reference:
Get Started with the PageSpeed Insights API

Exception: Limit Exceeded: URLFetch URL Length

I am pulling data from an API which unfortunately has very big urls/end points.
Is there any way to shorten urls in order to bypass the Exception: Limit Exceeded: URLFetch URL Length error?
I found this: https://github.com/amitwilson/GoogleAppsScript/blob/master/LinkShortner but I don't know how to use it properly since most of the api calls i make are generated dynamically.
This is an example link (letters after "cursor=" is a cursor key which is different every time):
https://api.x.immutable.com/v1/orders?include_fees=false&status=filled&buy_token_address=0xacb3c6a43d15b907e8433077b6d38ae40936fe2c&order_by=updated_timestamp&direction=desc&user=0x72a2ffa8be361d4a5d575c55b7382d8c6317f77d&page_size=200&cursor=eyJvcmRlcl9pZCI6MTk0NTk2NzUyLCJzZXFfbnVtIjowLCJzdGF0dXMiOiJmaWxsZWQiLCJzdGFya19rZXkiOiIweDA3MWU2NjU0NTMzMGJiOGY5NDQ4ZWU1YTM3YTI5ODViZGYzYjQ1NzYwNDA5ODE4YmEwYTY0NmUxNjU4NTdhMWIiLCJ2YXVsdF9pZCI6MCwiZXRoZXJfa2V5IjoiMHg3MmEyZmZhOGJlMzYxZDRhNWQ1NzVjNTViNzM4MmQ4YzYzMTdmNzdkIiwic2VsbF90b2tlbl90eXBlIjoiRVRIIiwic2VsbF90b2tlbl9pZCI6IiIsInNlbGxfY2xpZW50X3Rva2VuX2lkIjoiIiwic2VsbF90b2tlbl9hZGRyZXNzIjoiIiwic2VsbF90b2tlbl9kZWNpbWFscyI6MTgsInNlbGxfcXVhbnRpdHkiOiIyODQzMDAwMDAwMDAwMDAwIiwic2VsbF9hc3NldF9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6Ilczc2lZVzF2ZFc1MElqb2dJakk0TkRNd01EQXdNREF3TURBd0lpd2dJbVpsWlY5MGVYQmxJam9nSW0xaGEyVnlJaXdnSW05eVpHVnlYMmxrSWpvZ01UazBOVGsyTnpVeUxDQWljbVZqYVhCcFpXNTBYMlYwYUdWeVgydGxlU0k2SUNJd2VEVmpNell6WVRKaFlUZzJPV1F3WXpVeU1XTm1ZVE5sWWpCbE16YzBPVEpqTUdWaU5UbGxOVGdpTENBaWNtVmphWEJwWlc1MFgzTjBZWEpyWDJ0bGVTSTZJQ0l3ZURBMk1UY3haVEpoWlRRMU5qTXlNVE15TWpaak5XWm1aV1JpT1RSaE9UVXlOV0ppWm1Zell6bGtZMkpoWm1KaFpURTVabUk0TjJOa1ltSXpOMk5sTVdVaWZWMD0ifQ
In your situation, for example, how about the following sample script?
Sample script:
Before you use this script, please set apiKey and yourDynamicLinkDomain. Your yourDynamicLinkDomain might be like https://###.page.link.
const apiKey = "###"; // Please set your API key.
const yourDynamicLinkDomain = "###"; // Please set your dynamic link domain.
function getShortUrl_(longUrl) {
const url = "https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=" + apiKey;
const options = {
payload: JSON.stringify({
dynamicLinkInfo: {
dynamicLinkDomain: yourDynamicLinkDomain,
link: longUrl,
},
}),
contentType: "application/json",
};
const res = UrlFetchApp.fetch(url, options);
const { shortLink } = JSON.parse(res.getContentText());
return shortLink;
}
// Please run this function.
function main() {
// do something for retrieving the long URL in your actual script.
const longUrl = "###"; // It supposes that your long URL is put to longUrl.
const shortUrl = getShortUrl_(longUrl); // Here, the long URL is shortned. You can use this.
// If "options" is required to be used, please use this.
const options = {
};
const res = UrlFetchApp.fetch(shortUrl, options);
}
In this sample script, it supposes that by your current script, longUrl is obtained. And, this sample script converts from the long URL to the short URL with the function getShortUrl_. And, you can use this short URL using UrlFetchApp.
Note:
This sample script supposes that you have already been able to use Firebase Dynamic Links API. Please be careful about this.
If you have never been able to be used Firebase Dynamic Links API, please check the official document. And, the following flow might be useful.
please check the following flow.
Please create a new Firebase project and link it to your Google Cloud Platform Project. Ref
At this time, please set dynamicLinkDomain at "Dynamic Links".
Please enable Firebase Dynamic Links API at the API console.
Please create your API key from your Google Cloud Platform Project.
Please use this API in the above script.
When I tested this script, I could confirm that the long URL is converted to the short URL and the short URL can be used with UrlFetchApp.
If I misunderstood about but I don't know how to use it properly since most of the api calls i make are generated dynamically., please tell me.
References:
Create Dynamic Links with the REST API
Class UrlFetchApp
Added:
From your following reply,
Hey I made a sample sheet so you can see how I get the longurl it is highlighted with yellow color in 202 row. docs.google.com/spreadsheets/d/… The API works like this: it can pull 200 rows, then on the last row of each call it shows this cursor ID which must be called again in next row in order to show the next 200 rows.
Check the sample sheet on my previous reply. Longurl is highlighted with YELLOW color.
When I saw your sample Spreadsheet, it seems that you are using the long URL as =importjson(CONCATENATE("https://api.x.immutable.com/v1/orders?include_fees=false&status=filled&buy_token_address=0xacb3c6a43d15b907e8433077b6d38ae40936fe2c&order_by=updated_timestamp&direction=desc&user=0x8604808B824C4444Fe4dF3f94850ACd584C5aD7d&page_size=200&cursor=",W202),"","noTruncate,noHeaders"). But unfortunately, from your question and your reply, I couldn't notice that you are using it like that.
In this case, it is required to use my sample script by modifying as follows.
Sample script:
Please copy and paste the following script to the script editor of your Spreadsheet and save the script.
function getShortUrl(longUrl) {
const apiKey = "###"; // Please set your API key.
const yourDynamicLinkDomain = "###"; // Please set your dynamic link domain.
const url = "https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=" + apiKey;
const options = {
payload: JSON.stringify({
dynamicLinkInfo: {
dynamicLinkDomain: yourDynamicLinkDomain,
link: longUrl,
},
}),
contentType: "application/json",
};
const res = UrlFetchApp.fetch(url, options);
const { shortLink } = JSON.parse(res.getContentText());
return shortLink;
}
When this script is used for your sample Spreadsheet, please put the following formula to the cell "A203".
=importjson(getShortUrl(CONCATENATE("https://api.x.immutable.com/v1/orders?include_fees=false&status=filled&buy_token_address=0xacb3c6a43d15b907e8433077b6d38ae40936fe2c&order_by=updated_timestamp&direction=desc&user=0x8604808B824C4444Fe4dF3f94850ACd584C5aD7d&page_size=200&cursor=",W202)),"","noTruncate,noHeaders")
By this formula, the long URL retrieved with CONCATENATE("https://api.x.immutable.com/v1/orders?include_fees=false&status=filled&buy_token_address=0xacb3c6a43d15b907e8433077b6d38ae40936fe2c&order_by=updated_timestamp&direction=desc&user=0x8604808B824C4444Fe4dF3f94850ACd584C5aD7d&page_size=200&cursor=",W202) is shorten by shortenUrl(). And, use the shorten URL with importjson.
Note:
This sample script supposes that you have already been able to use Firebase Dynamic Links API. Please be careful about this.
When I tested the above script and formula to your sample Spreadsheet, I confirmed that the values are retrieved.

Getting image url in google script

Trying to get image url in google script.
couldn’t find any function that is able to get the url from images that are not in a specific cell, images are located above the grid.
any ideas?
Issue and workaround:
On October 30, 2018, in order to manage the images on the cells in Spreadsheet, a new Class of OverGridImage has been added. Ref By this, the images on the cells got to be able to be managed. This class has the method of getUrl. The official document of this method says as follows.
Gets the image's source URL; returns null if the URL is unavailable. If the image was inserted by URL using an API, this method returns the URL provided during image insertion.
Namely, for example, when the following script is run, the URL of the image can be retrieved.
function sample1() {
const sheet = SpreadsheetApp.getActiveSheet();
// Put image from URL.
sheet.insertImage("https://stackoverflow.design/assets/img/logos/so/logo-stackoverflow.png", 1, 1);
// Retrieve image URL.
const images = sheet.getImages();
const url = images[0].getUrl();
console.log(url)
}
In your actual situation, if your images are put on the cells using the above script, the URLs can be retrieved by the above simple script. But, here, there is an important point. After the image was put using this script, when you manually move the image, the URL cannot be retrieved. I think that this is a bug.
And also, if you had manually put the images from the URL and your drive, unfortunately, the URL of the images cannot be retrieved. About this, it has already been reported to the Google issue tracker. Ref
If you had manually put the images from the URL and your drive, and when you want to retrieve the URLs of the images, it is required to use a workaround. In this case, I would like to propose to use this method of this answer. In this answer, my created Google Apps Script library is used.
Usage:
1. Install Google Apps Script library.
You can see the method for installing this library at here.
2. Enable Drive API.
In this case, Drive API is used. So, please enable Drive API at Advanced Google services.
3. Sample script.
function sample2() {
const sheetName = "Sheet1"; // Please set the sheet name.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(sheetName);
const images = sheet.getImages();
const obj = images.reduce((o, e) => {
const u = e.getUrl();
if (u) o[e.getAnchorCell().getA1Notation()] = u;
return o;
}, {});
const res = DocsServiceApp.openBySpreadsheetId(ss.getId()).getSheetByName(sheetName).getImages();
if (res.length == 0) return;
const urls = res.map(({ image, range }, i) => {
if (obj[range.a1Notation]) return obj[range.a1Notation];
const o = Drive.Files.insert({ title: `sample${i + 1}` }, image.blob);
const url = o.thumbnailLink.replace(/\=s\d+/, "=s1000");
DriveApp.getFileById(o.id).setTrashed(true);
return url;
});
console.log(urls)
}
4. Testing.
When the above script is run, the images are retrieved from "Sheet1" and retrieve the URLs of the images. For example, when there are images put with the image URL using the script, the URL can be retrieved.
Note:
In this workaround, in order to retrieve the URL of the image, the thumbnail link is used. This link is not permanent. Please be careful about this. If you are required to retrieve the permanent link, please create the retrieved image file blob as the file, and please publicly share them, and then, please retrieve the WebContentLink. By this, you can retrieve the permanent link of the image.
References:
DocsServiceApp
Related thread
How to access new 'in-cell-image' from google apps script?

How do get GIF from Google Docs using Apps Script?

I want to get the gif image from google docs. Using Apps Script, gif images are got as
InlineImage. But it's only static without animating.
My code
var doc = DocumentApp.openByUrl('url');
Logger.log(doc.getBody().getImages()[0]);
var encoded = Utilities.base64Encode(doc.getBody().getImages()[0].getBlob().getBytes());
Logger.log(encoded);
You want to retrieve the original images from Google Document.
In your case, you want to retrieve an animation GIF from Google Document.
You want to achieve this using Google Apps Script.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Unfortunately, it seems that the original images cannot be directly retrieved using getImages(). So in this answer, I use the method of documents.get in Google Docs API.
Flow:
The flow of this sample script is as follows.
Retrieve the object from Google Document using the method of documents.get in Google Docs API.
Retrieve the source information from the retrieved object.
The embeded original images can be retrieved from the property of inlineObjects.
Create the original images from the retrieved source information.
Sample script:
Before you use this script, please enable Google Docs API at Advanced Google services.
function myFunction() {
var documentId = "###"; // Please set Google Document ID.
// Retrieve the object from Google Document using the method of documents.get in Google Docs API.
var obj = Docs.Documents.get(documentId);
// Retrieve the source information from the retrieved object.
var inlineObjects = Object.keys(obj.inlineObjects).reduce(function(ar, e, i) {
var o = obj.inlineObjects[e].inlineObjectProperties.embeddedObject;
if (o.hasOwnProperty("imageProperties")) {
var res = UrlFetchApp.fetch(o.imageProperties.contentUri, {headers: {Authorization: "Baerer " + ScriptApp.getOAuthToken()}, muteHttpExceptions: true});
if (res.getResponseCode() == 200) ar.push(res.getBlob().setName("image" + (i + 1)));
}
return ar;
}, []);
// Create the original images from the retrieved source information.
inlineObjects.forEach(function(blob) {
var id = DriveApp.createFile(blob).getId();
Logger.log(id)
})
}
When you run the script, the image files are created to the root folder. And you can see the file IDs of them at the log. The filename is "image1", "image2",,, as the sample.
References:
Advanced Google services
Method: documents.get
InlineObject
If I misunderstood your question and this was not the direction you want, I apologize.