Extracting data from web page using Cheerio Library - google-apps-script

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

Related

Issue getting full page Screenshot of website using PageSpeedApi and Google Apps Script

I am trying to get the screenshot from this link to Google Drive using Google Apps Script. For that, I found this source to make it work. I tried to modify the script but the result is not exactly what I am looking to achieve. Here is the script:
function getScreenShot(){
const apiKey = "###";
const url1 = "http://www.amafruits.com";
const apiEndpoint = `https://www.googleapis.com/pagespeedonline/v5/runPagespeed?key=${apiKey}&url=${encodeURIComponent(url1)}&category=performance&strategy=mobile`;
const res = UrlFetchApp.fetch(apiEndpoint)
const obj = JSON.parse(res.getContentText());
const base64 = obj["lighthouseResult"]["audits"]["final-screenshot"][
"details"
]["data"]
.split(",")
.pop();
const blob = Utilities.newBlob(
Utilities.base64Decode(base64),
"image/jpeg",
"sample1.jpg"
);
const id = DriveApp.createFile(blob).getId();
console.log(id);
}
However, it gives the following output:
Ideally, I want to get the following screenshot from the website:
How do I get the full page screenshot instead of just one small portion? Any guidance would be much appreciated. Thank you for your time.
In your situation, how about the following sample script?
Sample script:
In this sample script, I retrieved the screenshot from https://pagespeed.web.dev/report?url=http%3A%2F%2Fwww.amafruits.com%2F using Charts Service.
function myFunction() {
const url = "https://pagespeed.web.dev/report?url=http%3A%2F%2Fwww.amafruits.com%2F"; // This URL is from your question.
const blob = Charts
.newTableChart()
.setDataTable(Charts.newDataTable().addColumn(Charts.ColumnType.STRING, "").addRow([`<meta http-equiv="refresh" content="0; URL='${url}'">`]).build())
.setOption('allowHtml', true)
.setDimensions(1000, 2000)
.build()
.getBlob();
DriveApp.createFile(blob.setName("sample.png"));
}
When this script is run, the whole page is retrieved as a PNG file. That image includes your expected situation. Unfortunately, if you want to retrieve only your showing image, it is required to edit the exported image.
Note:
In this case, when the image size is smaller than the HTML page, please adjust .setDimensions(1000, 2000), and test it again.
In this sample, it seems that all URLs cannot be used. Please be careful about this.
Reference:
Charts Service

Exception: Limit Exceeded: URLFetch URL Length on GOOGLE APPS SCRIPT [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=eyJvcmRlcl9pZCI6MTk0NTk2NzUyLCJzZXFfbnVtIjowLCJzdGF0dXMiOiJmaWxsZWQiLCJzdGFya19rZXkiOiIweDA3MWU2NjU0NTMzMGJiOGY5NDQ4ZWU1YTM3YTI5ODViZGYzYjQ1NzYwNDA5ODE4YmEwYTY0NmUxNjU4NTdhMWIiLCJ2YXVsdF9pZCI6MCwiZXRoZXJfa2V5IjoiMHg3MmEyZmZhOGJlMzYxZDRhNWQ1NzVjNTViNzM4MmQ4YzYzMTdmNzdkIiwic2VsbF90b2tlbl90eXBlIjoiRVRIIiwic2VsbF90b2tlbl9pZCI6IiIsInNlbGxfY2xpZW50X3Rva2VuX2lkIjoiIiwic2VsbF90b2tlbl9hZGRyZXNzIjoiIiwic2VsbF90b2tlbl9kZWNpbWFscyI6MTgsInNlbGxfcXVhbnRpdHkiOiIyODQzMDAwMDAwMDAwMDAwIiwic2VsbF9hc3NldF9uYW1lIjpudWxsLCJzZWxsX2Fzc2V0X2ltYWdlX3VybCI6bnVsbCwic2VsbF9jb2xsZWN0aW9uX25hbWUiOm51bGwsInNlbGxfY29sbGVjdGlvbl9pY29uX3VybCI6bnVsbCwiYnV5X3Rva2VuX3R5cGUiOiJFUkM3MjEiLCJidXlfY2xpZW50X3Rva2VuX2lkIjoiMjI0ODUzMTMiLCJidXlfdG9rZW5faWQiOiIweDU0ODRkZjA1NzM0OTkxNTg0NWNkNjM0YmVmNDAxNjY1YmE1YjAzZDEwNzE4YmU2ZTQxNmEyMTFhNmVhMGJkNzEiLCJidXlfdG9rZW5fYWRkcmVzcyI6IjB4YWNiM2M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V0ppWm1Zell6bGtZMkpoWm1KaFpURTVabUk0TjJOa1ltSXpOMk5sTVdVaWZWMD0ifQ
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.

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=eyJvcmRlcl9pZCI6MTk0NTk2NzUyLCJzZXFfbnVtIjowLCJzdGF0dXMiOiJmaWxsZWQiLCJzdGFya19rZXkiOiIweDA3MWU2NjU0NTMzMGJiOGY5NDQ4ZWU1YTM3YTI5ODViZGYzYjQ1NzYwNDA5ODE4YmEwYTY0NmUxNjU4NTdhMWIiLCJ2YXVsdF9pZCI6MCwiZXRoZXJfa2V5IjoiMHg3MmEyZmZhOGJlMzYxZDRhNWQ1NzVjNTViNzM4MmQ4YzYzMTdmNzdkIiwic2VsbF90b2tlbl90eXBlIjoiRVRIIiwic2VsbF90b2tlbl9pZCI6IiIsInNlbGxfY2xpZW50X3Rva2VuX2lkIjoiIiwic2VsbF90b2tlbl9hZGRyZXNzIjoiIiwic2VsbF90b2tlbl9kZWNpbWFscyI6MTgsInNlbGxfcXVhbnRpdHkiOiIyODQzMDAwMDAwMDAwMDAwIiwic2VsbF9hc3NldF9uYW1lIjpudWxsLCJzZWxsX2Fzc2V0X2ltYWdlX3VybCI6bnVsbCwic2VsbF9jb2xsZWN0aW9uX25hbWUiOm51bGwsInNlbGxfY29sbGVjdGlvbl9pY29uX3VybCI6bnVsbCwiYnV5X3Rva2VuX3R5cGUiOiJFUkM3MjEiLCJidXlfY2xpZW50X3Rva2VuX2lkIjoiMjI0ODUzMTMiLCJidXlfdG9rZW5faW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bE16YzBPVEpqTUdWaU5UbGxOVGdpTENBaWNtVmphWEJwWlc1MFgzTjBZWEpyWDJ0bGVTSTZJQ0l3ZURBMk1UY3haVEpoWlRRMU5qTXlNVE15TWpaak5XWm1aV1JpT1RSaE9UVXlOV0ppWm1Zell6bGtZMkpoWm1KaFpURTVabUk0TjJOa1ltSXpOMk5sTVdVaWZWMD0ifQ
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.

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=eyJvcmRlcl9pZCI6MTk0NTk2NzUyLCJzZXFfbnVtIjowLCJzdGF0dXMiOiJmaWxsZWQiLCJzdGFya19rZXkiOiIweDA3MWU2NjU0NTMzMGJiOGY5NDQ4ZWU1YTM3YTI5ODViZGYzYjQ1NzYwNDA5ODE4YmEwYTY0NmUxNjU4NTdhMWIiLCJ2YXVsdF9pZCI6MCwiZXRoZXJfa2V5IjoiMHg3MmEyZmZhOGJlMzYxZDRhNWQ1NzVjNTViNzM4MmQ4YzYzMTdmNzdkIiwic2VsbF90b2tlbl90eXBlIjoiRVRIIiwic2VsbF90b2tlbl9pZCI6IiIsInNlbGxfY2xpZW50X3Rva2VuX2lkIjoiIiwic2VsbF90b2tlbl9hZGRyZXNzIjoiIiwic2VsbF90b2tlbl9kZWNpbWFscyI6MTgsInNlbGxfcXVhbnRpdHkiOiIyODQzMDAwMDAwMDAwMDAwIiwic2VsbF9hc3NldF9uYW1lIjpudWxsLCJzZWxsX2Fzc2V0X2ltYWdlX3VybCI6bnVsbCwic2VsbF9jb2xsZWN0aW9uX25hbWUiOm51bGwsInNlbGxfY29sb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nlYMmxrSWpvZ01UazBOVGsyTnpVeUxDQWljbVZqYVhCcFpXNTBYMlYwYUdWeVgydGxlU0k2SUNJd2VEVmpNell6WVRKaFlUZzJPV1F3WXpVeU1XTm1ZVE5sWWpCbE16YzBPVEpqTUdWaU5UbGxOVGdpTENBaWNtVmphWEJwWlc1MFgzTjBZWEpyWDJ0bGVTSTZJQ0l3ZURBMk1UY3haVEpoWlRRMU5qTXlNVE15TWpaak5XWm1aV1JpT1RSaE9UVXlOV0ppWm1Zell6bGtZMkpoWm1KaFpURTVabUk0TjJOa1ltSXpOMk5sTVdVaWZWMD0ifQ
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.

Google Apps Scripts - Optimal way to replace text

I have tons of strings that I need to replace in a Google document. My performance to run my script is taking a huge hit and takes forever to run now. I am looking at how to optimize.
body.replaceText("oldvalue1",newValue1)
body.replaceText("oldvalue2",newValue2)
body.replaceText("oldvalue3",newValue3)
..
..
..
Is there a more optimal way to replace text in a Google Doc using google scripts?
As #Kos refers in the comments, the best approximation is using Google Docs API batchUpdates as Advanced Service inside Google Apps Script.
I leave you an example on how Docs API works as an Advanced Service. For the example I suppose you have an array of objects that contains the oldValue and newValue:
function batchUpdating(DocID = DocID) {
const replaceRules = [
{
toReplace: "oldValue1",
newValue: "newValue1"
},
...
]
const requestBuild = replaceRules.map(rule => {
var replaceAllTextRequest = Docs.newReplaceAllTextRequest()
replaceAllTextRequest.replaceText = rule.newValue
replaceAllTextRequest.containsText = Docs.newSubstringMatchCriteria()
replaceAllTextRequest.containsText.text = rule.toReplace
replaceAllTextRequest.containsText.matchCase = false
Logger.log(replaceAllTextRequest)
var request = Docs.newRequest()
request.replaceAllText = replaceAllTextRequest
return request
})
var batchUpdateRequest = Docs.newBatchUpdateDocumentRequest()
batchUpdateRequest.requests = requestBuild
var result = Docs.Documents.batchUpdate(batchUpdateRequest, DocID)
Logger.log(result)
}
Google Apps Script helps us handle the authorization flow, and gives us hints on how to construct our request. For example, Docs.newReplaceAllTextRequest(), helps us build the request for that service, giving us hints that it contains replaceText and containText. In any case, we could also supply the object directly to the request:
const requestBuild = replaceRules.map(rule => {
return {
replaceAllText:
{
replaceText: rule.newValue,
containsText: { text: rule.oldValue, matchCase: false }
}
}
})
To take in account
Each request is validated before being applied. If any request is not valid, then the entire request will fail and nothing will be applied.
If your script project uses a default GCP project created on or after April 8, 2019, the API is enabled automatically after you enable the advanced service and save the script project. If you have not done so already, you may also be asked to agree to the Google Cloud Platform and Google APIs Terms of Service as well.
Documentation:
ReplaceAllTextRequest
SubstringMatchCriteria
Array.forEach(obj => { body.replaceText(obj.old,obj.new)})
#kos way is probably a better way to go.