following my previous question, I wanted to get the screenshot of the webpage using Google Apps Script, thanks to Tanaike for the proposed script, it can get the webpage as required. However, after running the script for few times, I started to encounter the error saying: Error: We're sorry, a server error occurred. Please wait a bit and try again
After every run, I need to check the script whether it ran successfully or not, which is not suitable in my case. Following is the proposed script:
function getScreenShot() {
const url = "https://pagespeed.web.dev/report?url=http://www.cool.haus"; // 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"));
}
This is the eror which I receive when running the function from script editor:
I found the following sources (link1 , link2) which also addresses the same issue, any help to solve this issue would be greatly appreciated. Thank you.
I could confirm your situation. In this case, how about retrying the request? When this is reflected in your script, it becomes as follows.
function getScreenShot() {
const url = "https://pagespeed.web.dev/report?url=http://www.cool.haus"; // This URL is from your question.
let blob;
let retry = 3;
let n = 1;
while (n <= retry && !blob) {
console.log(`Try: ${n}`);
try {
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();
} catch ({ message }) {
console.log(message);
Utilities.sleep(5000); // This might not be required to be used.
}
n++;
}
if (!blob) {
console.log("Image blob couldn't be retrieved.");
return;
}
DriveApp.createFile(blob.setName("sample.png"));
}