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;
}