2021年3月26日星期五

Apply text syle to a regex pattern in google sheet cells

I would like to get parts of strings (by regex) into a specific text style, but I can't manage the loop and always get errors. Could you please help me ? Thank you so much.

In the first row is the original text, and in the second under, is the desired text style. enter image description here

Here is the sheet (french parameters) https://docs.google.com/spreadsheets/d/1vq0Ai_wEr3MamEQ-kMsXW7ZGg3RxrrkE5lITcYjO-rU/edit?usp=sharing

function NomsStyleBotanique(){    const classeur = SpreadsheetApp.getActive();  // var Feuille = classeur.getSheetByName('Feuille 1');     var ligne = classeur.getCurrentCell().getRow();    var colonne = classeur.getCurrentCell().getColumn();    var range = classeur.getActiveRange();      var richTextValues = range.getRichTextValues().map(([a]) => {    var text = a.getText();    var pos = 0;        var myregEx = /,/g;    var Noms = text.split(myregEx);        var textStyleNomPlante = SpreadsheetApp.newTextStyle()          .setFontSize(10)          .setForegroundColor("black")          .setFontFamily("Times New Roman")          .setItalic(false)          .build();        var textStyleNomAuteur = SpreadsheetApp.newTextStyle()          .setFontSize(10)          .setForegroundColor("#616399")     // ("grey")          .setFontFamily("Times New Roman")          .setItalic(true)          .build();      var nbPhrases = [];    var i =0;       while (Noms){ i++; nbPhrases.push(Noms[i]); // SpreadsheetApp.getUi().alert(Noms[i]);         for (var i=0;i<nbPhrases.length;i++){        var myarr = Noms[i].split(" ");    var Espace1 = myarr[0].length+1;    var Espace2 = myarr[1].length+1;      if (Noms[i]){      if ((Noms[i].indexOf("subsp") > 1) || (Noms[i].indexOf("var.") > 1)){      var Espace3 = myarr[2].length+1;      var Espace4 = myarr[3].length+1;      pos = Espace1+Espace2+Espace3+Espace4; }      else {   pos = Espace1+Espace2;  } // pos = text.match(new RegExp(/\\s/, 'g'))[2];       var position = pos;      if (position > -1){        var temp = a.getTextStyle(0, position - 1);        return [          SpreadsheetApp.newRichTextValue()          .setText(Noms[i])          .setTextStyle(0, position - 1, textStyleNomPlante)          .setTextStyle(position, Noms[i].length, textStyleNomAuteur)          .build()        ];      }      return [SpreadsheetApp.newRichTextValue().setText(Noms[i]).setTextStyle(Noms[i].getTextStyle()).build()];     }    }   }  } // fin boucle  );  range.setRichTextValues(richTextValues);  }  
https://stackoverflow.com/questions/66825970/apply-text-syle-to-a-regex-pattern-in-google-sheet-cells March 27, 2021 at 08:38AM

没有评论:

发表评论