0
var csvUrl = "http://services.runescape.com/m=clan-hiscores/members_lite.ws?clanName=divine%2089";

var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();

var csvData = Utilities.parseCsv(csvContent);

datasheet.getRange(1, 6, csvData.length, csvData[0].length).setValues(csvData);

The above gets a list in csv format and it outputs fine into google sheets. The problem is that it replaces all spaces with an unknown character that I am not sure how to remove easily.

The character is: �

Is there an easy way to take the data before it gets outputted to the sheet and replace � with a regular space?

Aaron
  • 3,135
  • 20
  • 51
  • 78
  • If you can create a list of characters to keep (all alpha-nnumeric characters and spaces), see [this thread](http://stackoverflow.com/questions/6555182/remove-all-special-characters-except-space-from-a-string-using-javascript) – Karl_S Feb 17 '17 at 14:50

1 Answers1

4

You can replace in the string like so:

var stringy = '��SOMETHING��'
var replacedStringy = stringy.replace(/�/g, ' ');  //replaces all encounters

for your script

function myFunction() {
  var csvUrl = "http://services.runescape.com/m=clan-hiscores/members_lite.ws?clanName=divine%2089";

  var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();

  var csvData = Utilities.parseCsv(csvContent);
  for(i in csvData){
    for(j in csvData[i]){
      csvData[i][j]=csvData[i][j].replace(/�/g, ' ');
    }
  }

  SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(1, 6, csvData.length, csvData[0].length).setValues(csvData);
}
  • 1
    Can you explain what the i and j are referring to in the csvData? – Aaron Feb 17 '17 at 16:12
  • 1
    Sure! What you're getting back in csvData is an array object, you can check them in detail: https://www.w3schools.com/js/js_arrays.asp The idea is simple your array is constructed like [[apple, banana, cherry], [car,bike,lamp]] the position array[0] = [apple, banana, cherry] the position array[0][0] = apple What I did is cycle through all the positions and replaced the string inside – Juan Diego Antezana Feb 17 '17 at 16:30