1

Regex: remove TAB \t tab var regex = /\s[A-Za-z]+/g do not work

selectFirstEmptyRow function () {

  var ss = SpreadsheetApp.openByUrl ("https://docs.google.com/spreadsheets/d/---ID-----edit#gid=395019283");
  var date = Utilities.formatDate(new Date() ss.getSpreadsheetTimeZone(), "d" + "- 0" + "M" + "-" + "y");
  var sheet = ss.getSheetByName(date);
  var regex = /[^0-9]*/g; // extract the string before digital channel
  var doc = DocumentApp.getActiveDocument().getText()
  var result = RegExp.exec(doc);
 // * Extract white \ s match any white space character [\ r \ n \ t \ f]
  var regex = /\s[A-Za-z]+/g; // extract the spaces in front of and behind "Name Surname"
  RegExp.exec var result = (result);
  sheet.setActiveSelection(sheet getRange ("B" + getFirstEmptyRowWholeRow())).setValue(result);
   Logger.log(result.getText);

I can not remove a tab \t and newlines \n with the syntax

var regex = /\s[A-Za-z]+/g;

There remains a line preceding the string "Name Surname" when I insert it into the Spreadsheet. After analysis it appears that the concerns are the tabs \t, it is not deleted.

I try to extract from a "text document" a string (which is always at the top of the document until the first numerical chain) and put in a "spreadsheet document 'Spreadsheet at the site of the first box vacuum column "B".

The handling of this string in the same "text document" did not cause me any problem for the update of age with the syntax

 var regex = /[^0-9]*/g; // extract the string before a digit

The string from the document:

var str = '\n\n\n\Surname NAME\n34 Years\n\n......... .. "

Here is the complete script:

/// var result = result.replace(/^[\r\n]+|.|[\r\n]+$/g, "");// extrait les espaces devant et derriere Nom Prenom GAS D'ONT WORK

 /////    Facturer  Acte      ////

     function FacturerActe() {

      var regexp = /[^0-9]*/g ;// extrait la chaine de caractère avant la chaine numérique
      var doc = DocumentApp.getActiveDocument().getText();
      var result = regexp.exec(doc);
      var PrenomNom = new RegExp(result,"gm"); 
      Logger.log(PrenomNom.getText);

      var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/19rWt8JEGbYM29-W4tI2gj6peXR3hjvj51FxDFt2gFkU/edit#gid=395019283");
      var date = Utilities.formatDate(new Date(), ss.getSpreadsheetTimeZone() , "d"+"-"+"mm"+"-"+"y");
      var sheet = ss.getSheetByName(date);
      ss.setActiveSheet(sheet);

      var cell = sheet.getRange("A40");
      cell.setNote("Aujourd'hui est un nouveau jour ! Nous sommes le :"+date);

      selectFirstEmptyRow(); // Place le curseur sur la premiere ligne Vide de la Colonne "B"
    }

     //* Placez le curseur de l'utilisateur actuel dans la première cellule de la première ligne vide.
     //*
    function selectFirstEmptyRow()  { 

      var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/19rWt8JEGbYM29-W4tI2gj6peXR3hjvj51FxDFt2gFkU/edit#gid=395019283");
      var date = Utilities.formatDate(new Date(), ss.getSpreadsheetTimeZone() , "d"+"-"+"mm"+"-"+"y");
      var sheet = ss.getSheetByName(date);
      var regexp = /[^0-9]*/g ;// extrait la chaine de caractère avant la chaine numérique

      var doc = DocumentApp.getActiveDocument().getText();
      var result = regexp.exec(doc);
      var regexp = /\s[A-Z a-z]+/g ;// extrait les espaces devant et derriere Nom Prenom
    //* Extrait les blancs

      var result = regexp.exec(result);

    ///  var result = result.replace(/^[\r\n]+|\.|[\r\n]+$/g, "");// extrait les espaces devant et derriere Nom Prenom  GAS D'ONT WORK

      sheet . setActiveSelection (sheet.getRange("B" + getFirstEmptyRowWholeRow())).setValue(result);
       Logger.log(result.getText);
    }

    /**
     * " Trouve la première ligne vide la Colonne "B" " de checker de Mogsdad.
     */ 
    function getFirstEmptyRowWholeRow ()  { 

      var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/19rWt8JEGbYM29-W4tI2gj6peXR3hjvj51FxDFt2gFkU/edit#gid=395019283");
      var date = Utilities.formatDate(new Date(), ss.getSpreadsheetTimeZone(), "d"+"-"+"mm"+"-"+"y");
      var sheet = ss.getSheetByName(date);
      var range = sheet.getDataRange(); 
      var values = range.getValues(); 
      var row =  1 ; 
      for  (var row = 1; row < values.length; row ++)  { 
        if  (!values[ row ].join(""))  break ; 
      } 

      return  (row + 1); 

    }

     /////   Fin   Facturer  Acte      ////
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Lolowr
  • 81
  • 1
  • 3
  • 9
  • I don't see any instance created for the regex variable as in this SO question: http://stackoverflow.com/questions/17573598/google-apps-script-regular-expression-to-get-the-last-name-of-a-person. Try changing it and see if that works. – KRR Mar 23 '15 at 22:08
  • Also your regex is wrong. az should be a-z etc – Zig Mandel Mar 26 '15 at 14:00
  • Zig ! Error because of the auto translate on stackoverflow.com .The Regex is : var regexp = /\s[A-Z a-z]+/g ; but this Other Regex work better var regexp = /[^a-z\s-]+[A-Z\s-]+/g; – Lolowr Mar 26 '15 at 18:09

1 Answers1

1

As per Using regular expressions to search for data in your spreadsheet, you can match any newline and tab using [\t\r\n] pattern. Since you have more than one at a stretch, you should add a quantificator +. Also, since you are looking to capture 2 lines, you should add the [\t\r\n]+ to the pattern, too.

This will let you capture the 2 lines with Name and Age in between the newlines:

[\r\n\t]+(.+[\r\n\t]+.+)[\t\r\n]+

And you can later remove the newline characters:

var result = str.replace(/[\r\n\t]+(.+[\r\n\t]+.+)[\t\r\n]+/, '$1');

Here is what it looks like on regex101.com.

alert("'" + "\n\t\n\nNew line\n\t\nSecond 34 line\n\t\n....".replace(/[\r\n\t]+(.+[\r\n\t]+.+)[\t\r\n]+/, '$1') + "'")
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563