Home > Back-end >  Convert [ UNIX (LF) UTF-8 ] to [Windows (CRLF) ANSI]
Convert [ UNIX (LF) UTF-8 ] to [Windows (CRLF) ANSI]

Time:10-25

I have a VB program that uses a database located in a text file. This file, when opened in Notepad, tells me the format is in "WINDOWS (CRLF) |ANSI".

  • I transferred the database into a Google Sheets, which can be easily updated by multiple users.

  • I have developed a Google Apps Script to extract the data from the Google Sheets into a text file on the Drive.

The problem is that when I retrieve the file from the Google Sheets spreadsheet, it is in "UNIX (LF) | UTF-8" format and when I designate this file as the source of the VB program, it does not display some characters properly ("é" ; "ô" etc.)

I tried the bash command : Set-Content C:\Output\file.txt -Encoding Ascii But I still see the "UTF-8" when I open it with Windows Notepad.

I am looking for a way to convert a text file encoded in UTF-8 recovered from a Google Drive to a file encoded in ANSI (Windows-1252). I take the solution no matter if it's in Bash script or in Google Apps Script (even if I believe that for Gscript it won't be possible)

The Google Apps Script code used to extract data from the sheet :

  • Function to convert data Sheet to a string.
/**
 * Conversion de la feuille Google Sheets en une chaîne de caractères.
 * Cette chaîne de car. peut servir à la création d'un fichier texte.
 * Le séparateur de ligne est un retour à la ligne (" \n ")
 * Le séparateur de colonne est une tabulation (" \t ")
 * 
 * @param {SpreadsheetApp.Sheet} sheet 
 * 
 * @return {String} txt
 */
function _getData( sheet ){
  var numRows = sheet.getDataRange().getNumRows();
  var data;
  var txt = "";

  try {
      data = sheet.getDataRange().offset( 1, 0, numRows - 1).getDisplayValues(); // récupération des valeurs de la feuille (hormis l'en-tête).
      txt = data.map(function (col) {return col.join('\t');}).join('\n'); // Création d'une chaîne de caractère unique. (Delimiter col : tab ; Delimiter row : backslash)
      
  }catch( err) {
    Logger.log(err);
    Browser.msgBox('Erreur : '   err);
  }
  finally{
    return txt;
  }
}


  • Creation of the text file from a blob
/**
 * 
 * Création d'un fichier au format DAT.
 * Le fichier sera situé dans le répertoire : 
 * 
 * 
 * @param {String} extractName
 * @param {String} data
 * 
 * @return {DriveApp.File} file
*/

function createDatFile( extractName, data ){
  var blob;
  var directory;
  var fileName = extractName   ".DAT";
  var file;
  
  try{
    directory = DriveApp.getFolderById(''); // Répertoire Drive Partagé où l'extraction sera effectuée.
    blob = Utilities.newBlob( '' , MimeType.PLAIN_TEXT).setDataFromString( data ); // Création d'un Blob de données à partir d'une chaîne de caractères.
    
    // TODO Supprimer le fichier s'il existe déjà.

    file = DriveApp.createFile( fileName, blob.getDataAsString() ).moveTo( directory ); // Création d'un fichier au format ".DAT" dans un répertoire.

    return file;

  }catch(err){
    Logger.log("Erreur : "   err);
    Browser.msgBox("Erreur : "   err)
  }
}

  • Main
function exportToTextFile() {
  var activeSheet;
  var extractName = "SRC_DATA";
  var data;
  
  activeSheet = SpreadsheetApp.getActive().getActiveSheet();
  
  data = _getData( activeSheet ); 

  createDatFile( extractName, data);

  return 
}

CodePudding user response:

I believe your goal is as follows.

  • You want to create a text file with the character code of windows-1252.

When your script is modified, how about the following modification?

From:

blob = Utilities.newBlob( '' , MimeType.PLAIN_TEXT).setDataFromString( data );

// TODO Supprimer le fichier s'il existe déjà.

file = DriveApp.createFile( fileName, blob.getDataAsString() ).moveTo( directory );

To:

blob = Utilities.newBlob("", MimeType.PLAIN_TEXT, fileName).setDataFromString(data, "windows-1252"); // or "CP1252"
file = DriveApp.createFile(blob).moveTo(directory);

References:

  • Related