UrlFetchApp.fetch(URL).getContentText() all of a sudden not working despite no changes

1.2k views Asked by At

I have a very simple Google Script that grabs a CSV file from the internet and puts it into a Google Doc. There is more to it before and after, but here is where the issue arises:

  var csvUrl = "https://data.cdc.gov/resource/muzy-jte6.csv?$limit=6000";
  var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();

It runs automatically every night, and has run without an issue for the last six months. All of a sudden, it no longer works. The script editor provides no reason. The link is perfectly valid and still works. I tried it with a different CSV link from a different website, and it had the same issue.

When I run the script, all it says is:

Exception: Unexpected error: https://data.cdc.gov/resource/muzy-jte6.csv?$limit=6000 (line 8, file "Code") Dismiss

As I mentioned, the script has worked flawlessly well over 150 times since the summer, and it has not changed not has the document changed nor has the API link. My only guess is that there was some permission change on my Google account, but the app still has all the permissions it needs from my Google account.

Please help me understand why the script no longer works and how I can get it working again.

1

There are 1 answers

0
Tanaike On

I had the same situation. At that time, I could noticed that when the built-in function of Google Spreadsheet is used for the URL, the values can be retrieved. In that case, as the current workaround, I used the following flow.

  1. Put a formula of =IMPORTDATA(URL).
  2. Retrieve the values from the sheet.

When above flow is reflected to your URL of https://data.cdc.gov/resource/muzy-jte6.csv?$limit=6000, it becomes as follows.

Sample script:

function myFunction() {
  const url = "https://data.cdc.gov/resource/muzy-jte6.csv?$limit=6000";  // This is your URL.

  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  sheet.clear();
  const range = sheet.getRange("A1");
  range.setFormula(`=IMPORTDATA("${url}")`);
  SpreadsheetApp.flush();
  const values = sheet.getDataRange().getValues();
  range.clear();
  console.log(values)
}
  • In this sample script, "Sheet1" is used. So please modify it for your actual situation.
  • When above script is run, the values from the URL are retrieved as 2 dimensional array for values.
  • In this answer, I used IMPORTDATA. But for each situation, other functions might be suitable. In that case, please check them.

Note:

  • This is the current workaround. So when this issue was removed, I think that you can use your original script.

References: