2

I have a small updating script that, up to now, has been working flawlessly. It scrapes a remote CSV on a github page and updates cells in the spreadsheet. Now, when I run it, it simply says "Unexpected Error" followed by one of the csv files. It seems to fail on a random CSV file, rather than the same one each time. Does anyone know if there is a solution or what the issue it? Or how to get actually useful information to find the reason for the error? A quick run with the debugger suggested it's failing at the UrlFetchApp line, but I am not sure.

function onOpen() {
  SpreadsheetApp.getUi().createMenu('Refresh Enlir Data/Clear User Data')
  .addItem('Refresh Enlir Data', 'refreshEnlirData')
  .addItem('Clear User Data', 'clearUserData')
  .addToUi();
}


function clearUserData() {
  var sheet = SpreadsheetApp.getActive().getSheetByName('Raw User Data');
  sheet.getRange('D1:I').clearContent();
  sheet.getRange('K1:P').clearContent();
  sheet.getRange('R:X').clearContent();
  sheet.getRange('Z1:AC').clearContent();
  sheet.getRange('AE1:AH').clearContent();
}


function refreshEnlirData() {
  updateDatabase("https://raw.githubusercontent.com/BaconCatBug/BCBCSVStorage/master/SoulBreaks.csv", 'Enlir Data!B1:W')
  updateDatabase("https://raw.githubusercontent.com/BaconCatBug/BCBCSVStorage/master/Commands.csv", 'Enlir Data!AM1:BE')
  updateDatabase("https://raw.githubusercontent.com/BaconCatBug/BCBCSVStorage/master/Abilities.csv", 'Enlir Data!DU1:FM')
  updateDatabase("https://raw.githubusercontent.com/BaconCatBug/BCBCSVStorage/master/Magicite.csv", 'Enlir Data!FY1:ID')
  updateDatabase("https://raw.githubusercontent.com/BaconCatBug/BCBCSVStorage/master/LimitBreaks.csv", 'Enlir Data!IU1:JM')
}


function updateDatabase(fetchCSV, range) {
  const spreadsheetId = SpreadsheetApp.getActive().getId();
  var r = UrlFetchApp.fetch(fetchCSV);
  var arr = csv2array(r.getContentText());
  var request = {
    'valueInputOption': 'USER_ENTERED',
    'data': [{
      'range': range,
      'majorDimension': 'ROWS',
      'values': arr}]
  };
  
  var response = Sheets.Spreadsheets.Values.batchUpdate(request, spreadsheetId);
}


function csv2array(data) {
  var array = [];
  var rows = data.split("\n");
  for(var row in rows) {
    rows[row] = rows[row].replace(/","/g, "@"); 
    rows[row] = rows[row].replace(/"/g, ""); 
    array.push(rows[row].split("@"));
  }
  return array;
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
BaconCatBug
  • 137
  • 9
  • Have you actually managed to retrieve one of these CSV? It's failing for me every time. Since it used to work before, have you done any change to these CSV recently? – Iamblichus Dec 09 '20 at 14:12
  • I was seeing this kind of error on a script I wrote and that has been running for a long time with no errors I tried to debug it and I didn’t have any success and now I haven’t seen the error for two weeks. So I’m kind of thinking it’s a Google problem maybe if you wait a while it’ll go away by itself – Cooper Dec 09 '20 at 16:46

2 Answers2

1

Same for me. I've found that the error only happens when the extension of the file is ".csv". If you change the extension in ".txt", it works.

1

This seems to be a bug.

I am also newly having the problem as of 3 days ago (Dec 7) with UrlFetchApp and a script that runs at night to fetch some daily stock info. The URL grabs the info via CSV format as it is supposed to via the API, and it is confirmed to work manually using importHTML in Google Sheets. Also posted here. This is a brand new issue that came out of nowhere since there were no changes. It was working like a champ for months. Google did something, but what?

Update: We are not alone. It is a Google issue. Wish I knew when it will get resolved as this is hurting me and countless others:

Anyone affected by this issue, please consider clicking the star on the top-left in order to help prioritizing it and to keep track of it.

Iamblichus
  • 18,540
  • 2
  • 11
  • 27