title: Google script copy text formatted from Google Docs to Spreadsheet keep bold/underlined... format
body: <p>I have a Google Doc with a text inside and I did a script so that if I select the text it will copy to a Google Spreadsheet in a cell.I arrived to copy the text but i don't know how to do to keep the same styles colors and the problem is that i don't get it how to set bold/italic..</p>
  
  <p>This is what I did so far:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var SPREADSHEET_ID = "....."; // = assign your spreadsheet id to this variable
  var SHEET_NAME = "Feuille 1"; // = assign your sheet name to this variable
  var COORD_LIGNE = 5 ;
  var COORD_COLONNE = 4 ;
  
  function onOpen(e) {
    DocumentApp.getUi()
    .createMenu('Test')
    .addItem('Copier la selection du texte', 'Copy')
    .addToUi();
  }
  
  function Copy() {
    var ss = DocumentApp.getActiveDocument();
    var selection = ss.getSelection();
    var result = "";
    var fontColor, background , font,  fontWeight ,TextAlignment  ;
    
    var ok_underline, ok_italis , ok_bold, ok_strikethrough ;
    //Proceed only if something is selected, otherwise selection will be null &gt;&gt;&gt;&gt; pour voir le style/font/couleur on test pour 1 seul élément
    if (selection) {
      
      var elements = selection.getRangeElements();
      for (var i = 0; i &lt; elements.length; i++) {
        var element = elements[i];
        
        // Only modify elements that can be edited as text; skip images and other non-text elements. ??? on n'a pas fait les cas italique et bold etc. ici &gt;&gt;&gt;&gt;&gt;  element.getElement().asText().setUnderline(underline)
        // Qui sont des méthodes booleans &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;   isUnderline(offset)       isItalic()        isBold()   isStrikethrough()   ???? TextAlignment ne marche pas ???
        if (element.getElement().editAsText) {
          result += element.getElement().asText().getText();
          // get formatting
          fontColor = element.getElement().asText().getForegroundColor();
          background = element.getElement().asText().getBackgroundColor();
          font = element.getElement().asText() .getFontFamily();
          fontWeight = element.getElement().asText().getFontSize();
          TextAlignment = element.getElement().asText().getTextAlignment();
          
          if(element.getElement().asText().isUnderline() )
            ok_underline = 1;
          
          if(element.getElement().asText().isItalic() )
            ok_italis = 1;
          
          if(element.getElement().asText().isBold() )
            ok_bold = 1;
          
          if(element.getElement().asText().isStrikethrough() )
            ok_strikethrough = 1;
        }
      }
      
      var tss = SpreadsheetApp.openById(SPREADSHEET_ID);
      var ts = tss.getSheetByName(SHEET_NAME);
      
      var trange = ts.getRange(COORD_LIGNE, COORD_COLONNE); //la première cellule A1 première ligne + première colonne 
      trange.setBackground(background).setFontColor(fontColor).setFontFamily(font).setFontSize(fontWeight).setValue(result);
      /** on va bien encadrer la valeur dans la cellule &gt;&gt;&gt;&gt; on va redimensioner la colonne **/
      /**  vérification pour mettre text underline / italic / bold / strikethrough   **/
      if ( ok_underline ) 
      if ( ok_italis ) 
      if ( ok_bold ) 
      if ( ok_strikethrough ) 
  /**  i don't know how to set it in fonction of the ok variable  **/
      ts.autoResizeColumn(COORD_COLONNE); 
    }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><a href="https://i.stack.imgur.com/wl64O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wl64O.png" alt="Exported text from Google Doc to a cell in Google Spreadsheet"></a>
  Any idea is great!Thank you very much.</p>
  
  <p>Edit : Thanks you very much Signo it was very useful this is my code edited so far :</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>        if(element.getElement().asText().isUnderline() )
            ok_underline = 1;
           Logger.log("Value of ok_underline is  " + ok_underline);
          if(element.getElement().asText().isItalic() )
            ok_italis = 1;
          
          if(element.getElement().asText().isBold() )
            ok_bold = 1;
          
          if(element.getElement().asText().isStrikethrough() )
            ok_strikethrough = 1;
        }
      }
      
      var tss = SpreadsheetApp.openById(SPREADSHEET_ID);
      var ts = tss.getSheetByName(SHEET_NAME);
      
      var trange = ts.getRange(COORD_LIGNE, COORD_COLONNE); //la première cellule A1 première ligne + première colonne 
      trange.setBackground(background).setFontColor(fontColor).setFontFamily(font).setFontSize(fontWeight).setValue(result);
      /** on va bien encadrer la valeur dans la cellule &gt;&gt;&gt;&gt; on va redimensioner la colonne **/
      /**  vérification pour mettre text underline / italic / bold / strikethrough   **/
      if ( ok_underline ) {
        trange.setFontLine("underline");
         Logger.log("Call setFontLine() method ");
      }</code></pre>
  </div>
  </div>
  </p>
  
  <p>and this is my result. The undeline doesn't work where would be the problem? <a href="https://i.stack.imgur.com/PkW5B.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PkW5B.png" alt="The undeline doesn&#39;t work where would be the problem?"></a></p>