2
votes

How do I use appendText() or insertText() for a Google Doc script and maintain formatting?

I want to format the middle portion (group2) of appended strings with italics, while leaving the other parts (group1, group3) as normal text. For example: Hi my name is Nate.

I can bring in "Hi" and append "my name is" and it formats correctly. When I try to append (or insert) "Nate," "Nate" is italicized as well.Between operators +, appendText(), and insertText(), I'm not having much luck.

Below is the relevant portion of the script. Below that, is the entire thing.

How can I append 3 strings together, and only format the middle portion?

NOTE: I commented-out the things I tried (trial1, trial2, etc.). I also started HERE and used it as a guide.

Thanks for any help you can offer!

RELEVANT PART:

  if (author1 != "") {

      var group1 = author1+author2+author3;
      var group2 = title2Italics+containerItalics;
      var group3 = contribution1+contribution2+contribution3+version+number+publisher+pubDate+location;

    //Only using the calculations below to determine the offset for insertText
      var group1Length = group1.length;
      var group2Length = group2.length;
      var offset = group1Length+group2Length
      Logger.log(group1Length);
      Logger.log(group2Length);
      Logger.log(offset);

  //Determines if italicizing is necessary
     if (group2.length > 0) {

      var addG1 = body.insertParagraph(0,group1)
      var addG2 = addG1.appendText(group2);
      var formatItalics = addG2.editAsText().setItalic(true);

      //var trial1 = addG2.editAsText().setItalic(true) + group3;       //does not return the contents of "group3"
      //var trial2 = formatItalics + group3;                            //does not return the contents of "group3"
      //var trial3 = formatItalics.insertText(offset,group3);           //Error: "Index (18) must be less than or equal to the content length (6)."
      //var trial4 = formatItalics.insertText(group2Length, group3);    //formats "group3" as well
      //var trial5 = formatItalics.appendText(group3);                  //formats "group3" as well

    }

    //If italicizing is NOT necessary
    else {

      var cite = body.insertParagraph(0,group1 + group3);

    } //ELSE STATEMENT ENDS HERE

  } //FIRST IF STATEMENT ENDS HERE

ENTIRE SCRIPT:

function mlaBibTest() {

  // Sheet Information
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.setActiveSheet(ss.getSheetByName('test'));
  var startRow = 3;  
  var startCol = 21; 
  var numRows = sheet.getLastRow()-1;  
  var numCols = 14; 

  var dataRange = sheet.getRange(startRow, startCol, numRows, numCols);

// Document information
  var doc = DocumentApp.openById('13MlHq_uoO1rUF0RfdF_kBlLJjbGt4aDoOcSWef0V4zM');
  var body = doc.getBody();

  // Fetch values for each row in the SS Range.
  var cells = dataRange.getValues();

  for (var i = 0; i < cells.length; ++i) {

    var column = cells[i];
    var colU = column[0];
    var colV = column[1];           
    var colW = column[2];           
    var colX = column[3];           
    var colY = column[4];          
    var colZ = column[5];           
    var colAA = column[6];          
    var colAB = column[7];           
    var colAC = column[8];
    var colAD = column[9];
    var colAE = column[10];
    var colAF = column[11];
    var colAG = column[12];
    var colAH = column[13];


      var author1 = colU;
      var author2 = colV;
      var author3 = colW;
      var title1Quotes = colX;
      var title2Italics = colY;
      var containerItalics = colZ;
      var contribution1 = colAA;
      var contribution2 = colAB;
      var contribution3 = colAC;
      var version = colAD;
      var number = colAE;
      var publisher = colAF;
      var pubDate = colAG;
      var location = colAH;

  if (author1 != "") {

      var group1 = author1+author2+author3;
      var group2 = title2Italics+containerItalics;
      var group3 = contribution1+contribution2+contribution3+version+number+publisher+pubDate+location;

    //Only using the calculations below to determine the offset for insertText
      var group1Length = group1.length;
      var group2Length = group2.length;
      var offset = group1Length+group2Length
      Logger.log(group1Length);
      Logger.log(group2Length);
      Logger.log(offset);

  //Determines if italicizing is necessary
     if (group2.length > 0) {

      var addG1 = body.insertParagraph(0,group1)
      var addG2 = addG1.appendText(group2);
      var formatItalics = addG2.editAsText().setItalic(true);

      //var trial1 = addG2.editAsText().setItalic(true) + group3;       //does not return the contents of "group3"
      //var trial2 = formatItalics + group3;                            //does not return the contents of "group3"
      //var trial3 = formatItalics.insertText(offset,group3);           //Error: "Index (18) must be less than or equal to the content length (6)."
      //var trial4 = formatItalics.insertText(group2Length, group3);    //formats "group3" as well
      //var trial5 = formatItalics.appendText(group3);                  //formats "group3" as well

    }

    //If italicizing is NOT necessary
    else {

      var cite = body.insertParagraph(0,group1 + group3);

    } //ELSE STATEMENT ENDS HERE

  } //FIRST IF STATEMENT ENDS HERE

 } //FOR LOOP ENDS HERE

SpreadsheetApp.flush();

} // FUNCTION ENDS HERE
1
Thank you. Just what I needed to set one part of a paragraph as a link and not the whole paragraph. I actually looked for days to find this: var addG1 = body.insertParagraph(0,group1) var addG2 = addG1.appendText(group2); let text = addG2.asText(); text.setLinkUrl(offset, lengthOfText, URL); - aNewb

1 Answers

2
votes

This is a simple example of doing what you asked. It's important to remember that setItalics(true) sets a persistent setting for all new text to be italic, so we have to set it back to false after.

var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragraph = body.insertParagraph(0, ""); //add paragparh at top of body.
var text1 = paragraph.appendText("Not Italics ");
var text2 = paragraph.appendText("Italics ");
text2.setItalic(true); //Everything after and including this will be italics
var text3 = paragraph.appendText("Not Italics");
text3.setItalic(false); //Everything after and including this will not be italics

>Not Italics Italics Not Italics

So it's easier if you set italics as you build the paragraph.