0

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: enter image description here 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.

  • Can I ask you about your situation? How are you using your script of `myFunction()`? For example, when you directly run the function `myFunction()`, does such an error occur? When I tested your showing script, no error occurs and an image is created. So, first, I would like to confirm the method for correctly replicating your current issue. – Tanaike Feb 17 '23 at 12:51
  • Yes, right I receive this error on directly running this function from script editor. –  Feb 17 '23 at 12:55
  • Thank you for replying. Unfortunately, I cannot still replicate your situation. I apologize for this. First, I would like to try to correctly replicate your situation. – Tanaike Feb 17 '23 at 12:58
  • Thank you, please run it 2,3 times, you will get the error, I have also updated my question. –  Feb 17 '23 at 13:03
  • Thank you for replying. From your reply, I could replicate your situation. From this situation, I proposed an answer. Please confirm it. But, if that was not your expected direction, I apologize. – Tanaike Feb 17 '23 at 13:53

1 Answers1

0

I could confirm your situation. In this case, how about retrying the request? When this is reflected in your script, it becomes as follows.

Modified script:

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"));
}
  • When this script is run, when an error occurs, the script is retied. When the image blob is retrieved, the retry is finished. In this case, the sample number of retries is 3. And, in this sample script, you can see the process in the log.
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • so it means if the script is failed to get the screenshot 3 times then it would not try it again, right? is it ok if we increase the retry number? –  Feb 17 '23 at 14:07
  • @Roomi Thank you for replying. About `so it means if the script is failed to get the screenshot 3 times then it would not try it again, right?`, it's yes. About `is it ok if we increase the retry number?`, I think that it's ok. And also, how about adjusting `Utilities.sleep(5000)`? In my case, it seems that the script works with 2 retries. – Tanaike Feb 18 '23 at 00:28