Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix charset problems in .gs script?

I have a problem with charsets.

I parsed a csv file in google-app-engine and I'm posting to an uiapp table. But I checked special characters like áéíóú and those are not well displayed (?square symbol).

When I was setting up my code I played writing the string imported to a google docs document and it worked the same.

some advice please?

I search for:

  • a global charset definition to the code. or
  • string var transformation that makes the chars appear like I want to. (avoiding html &number definitions.
  • Is this related to the blob object?

The thing is important i come from spain and we need such characters.

app that get's a csv ';' delimited file and shows it's content

I post all my code, it's barely as the tutorial that is given.

function arreglaUrl(cadena){
  var texto = cadena[cadena.length - 2]
  if (texto == ''){
    cadena[cadena.length - 2] = 'Sin enlace';
  }
  else{
    cadena[cadena.length - 2] = '<center><a href=\"'+ texto + '\">Link.</a></center>' ;
  };

}

function parsedCSV(){

  var listaArchivos = DocsList.getFolderById('XXXXX').getFiles()

  for (var i = 0; i < listaArchivos.length; i++) {
    if (listaArchivos[i].getName() == 'baul.csv'){
      var origen = listaArchivos[i];
      };
  } 
  var texto = origen.getContentAsString();
  var arra = Utilities.parseCsv(texto,";");
  return(arra);
}



function doGet() {

  var datos = parsedCSV()


  var baul = Charts.newDataTable()
    for (i = 0; i < datos[0].length; i++){

        baul.addColumn(Charts.ColumnType.STRING, datos[0][i])   
    }

    for (i = 1; i < datos.length; i++){
      arreglaUrl(datos[i]) // this only makes some html i need to post some links
      baul.addRow(datos[i])

     }
    baul.build();



  var sectorFilter = Charts.newCategoryFilter()
      .setFilterColumnLabel("sector")
      .build();

  var tipoFilter = Charts.newCategoryFilter()
      .setFilterColumnLabel("tipo")
      .build();
  var searchFilter = Charts.newStringFilter()
      .setFilterColumnLabel("Titulo")
      .build();
  var searchDesc = Charts.newStringFilter()
      .setFilterColumnLabel("descripcion")
      .build();


  var tableChart = Charts.newTableChart().setOption('allowHtml', true).setDimensions(0,0)
      .build();

    var dashboard = Charts.newDashboardPanel()
      .setDataTable(baul)
      .bind([sectorFilter, tipoFilter, searchFilter, searchDesc], [tableChart])
      .build();

  var uiApp = UiApp.createApplication().setTitle('Baul de Recursos');
  var anchoTotal = '100%';
  dashboard.add(uiApp.createVerticalPanel()

                .add(uiApp.createHorizontalPanel()
                     .add(sectorFilter)
                     .add(tipoFilter)
                     .setSpacing(15)

                    )

                .add(uiApp.createHorizontalPanel()        
                     .add(searchFilter)
                     .add(searchDesc)
                     .setSpacing(15)                
                    )

                    .add(uiApp.createHorizontalPanel()
                         .add(tableChart).setBorderWidth(1).setHorizontalAlignment(UiApp.HorizontalAlignment.CENTER).setWidth(anchoTotal)
                        )

                );

  uiApp.add(dashboard);
  return uiApp;
}
like image 907
Antonio E. Avatar asked Oct 03 '22 10:10

Antonio E.


1 Answers

I found it, we need to get the content of the file first with a Blob object. This function is the one I use to parse some csv info into an array:

function parsedCSV(){
  
 //searching the file. This gets only one file in var origen
  var listaArchivos = DocsList.getFolderById('XXXXXXX').getFiles()
  
  for (var i = 0; i < listaArchivos.length; i++) {
    if (listaArchivos[i].getName() == 'baul.csv'){
      var origen = listaArchivos[i];
  };
}

// HERE IS THE GOOD DEFINITION OF CHAR:
var texto2= origen.getBlob().getDataAsString('ISO-8859-1');
// I put all the corrected text in an array

var arra = Utilities.parseCsv(texto2,";");
return(arra);
}

This is the solved thing: https://script.google.com/macros/s/AKfycbyHa-bLWBHBr3qifbvzxecqGgGUYX8mhyo-TKoyfGvy/exec

The trick:

var textVariableName = fileObjectVariableName.getBlob().getDataAsString('ISO-8859-1');
like image 164
3 revs Avatar answered Oct 17 '22 19:10

3 revs